這個(gè)問(wèn)題要求我們“通過(guò)完成調(diào)用push函數(shù)至少三次的主函數(shù)來(lái)編寫程序,然后打印出更新的堆棧,然后調(diào)用pop函數(shù)并打印出更新的堆棧again.”
代碼告訴我編譯失敗的原因如下:第10行|{對(duì)我來(lái)說(shuō)沒(méi)有意義。我試圖刪除它,但它會(huì)產(chǎn)生其他錯(cuò)誤
此外,代碼給出了一個(gè)警告:“警告:數(shù)組‘stack’假定有一個(gè)元素”,我不知道這是什么意思。
以下是代碼:
#include <stdio.h>
#define STACK_EMPTY '0'
#define STACK_SIZE 20
char stack[], item;
int *top, max_size;
void
push(char stack[], char item, int *top, int max_size),
{
if (*top < max_size-1)
{
--(*top);
stack[*top] = item;
}
}
char
pop (char stack[], /* input/output - the stack */
int *top) /* input/output - pointer to top of stack */
{
char item; /* value popped off the stack */
if (*top >= 0)
{
item = stack[*top];
--(*top);
}
else
{
item = STACK_EMPTY;
}
return (item);
}
int
main (void)
{
char s [STACK_SIZE];
int s_top = -1; // stack is empty
if (*top <= -1)
{
item = STACK_EMPTY;
}
return (0);
}
問(wèn)題在于如何處理頂部指針。您遞減指針,即--top,而不是它所指向的值。此外,push應(yīng)該增加它,即++top。
---Here是正確的代碼----