为什么 pop 函数不是 运行 stack[*top--] 而是与 stack[*(top--)] 一起工作?
Why is the pop function not running with stack[*top--] but working with with stack[*(top--)]?
问题是我无法使用pop功能
int pop(int stack[],int *top,int item)
{
if(*top==-1) {
printf("Stack Underflow");
return 0;
}
return stack[(*top)--];
}
这里,如果我用stack[*top--]
好像不行!有什么不同?为什么main函数中的top变量没有递减?
int main()
{
int stack[4], top = -1, item, id, ch;
for(;;) {
printf("Enter your choice:\n1.push\n2.pop\n3.Print top element\n4.Print all elements\n5.Exit\n");
scanf("%d",&ch);
switch(ch) {
case 1:
printf("Enter the item to be pushed:\n");
scanf("%d",&item);
push(stack,&top,item);
break;
case 2:
id=pop(stack,&top,item);
printf("%d was popped\n",id);
break;
case 4:
print(stack,&top,item);
break;
case 5:
exit(0);
}
}
}
return stack[(*top)--];
here if I use [*top--]
(*top)--
获取 top
指向的对象并递减该对象。因为 *top
指向调用者的 top
,这是您要用来查找数组元素的值。然后 --
递减调用者的 top
,使其指向堆栈中的下一个最新项。
*top--
是*(top--)
,它自减top
,得到未自减值指向的对象。这也获得了调用者的 top
,但它递减了函数的局部 top
,而不是调用者的对象。因为函数的top
是递减的,所以不再指向调用者的top
.
(*top)--
所做的是:
- 取消引用
top
,即访问 top
指向的值。
- 减少该值。
*top--
所做的是:
- 递减
top
,即top
本身的值
- 取消引用该值。
除此之外,我认为如果您定义一个堆栈结构而不是使用原始数组和整数作为指针会更好。
#define STACK_CAPACITY 3 // Adjust it as you want
struct stack {
int items[STACK_CAPACITY];
int top;
};
void stack_init(struct stack *s)
{
s->top = -1;
}
int stack_push(struct stack *s, int item)
{
if (s->top == STACK_CAPACITY-1)
return 0; // fail: stack is full
s->items[++s->top] = item;
return 1; // success: item pushed
}
int stack_pop(struct stack *s, int *top)
{
if (s->top == -1)
return 0;
if (top != NULL) // if top is NULL, ignore it
*top = s->items[s->top];
s->top--;
return 1;
}
以下是使用方法:
int main()
{
struct stack s;
stack_init(&s);
if (!stack_push(&s, 1))
printf("Stack is full\n");
if (!stack_push(&s, 2))
printf("Stack is full\n");
if (!stack_push(&s, 3))
printf("Stack is full\n");
if (!stack_push(&s, 4))
printf("Stack is full\n");
if (!stack_push(&s, 5))
printf("Stack is full\n");
int item;
stack_pop(&s, &item);
printf("top = %d\n", item); // outputs 3
stack_pop(&s, NULL); // Ignore the top
stack_pop(&s, &item);
printf("top = %d\n", item); // outputs 1
if (!stack_pop(&s, NULL)) {
printf("Stack is empty: cannot pop\n");
}
}
另外,不要使用 scanf()
来读取用户输入。 fgets()
更安全。
在 C 中,所有后缀运算符的优先级都高于所有前缀(或中缀)运算符。这就是语言的定义方式。所以
*top--
等同于
*(top--)
如果你想要
(*top)--
你需要明确的括号。
问题是我无法使用pop功能
int pop(int stack[],int *top,int item)
{
if(*top==-1) {
printf("Stack Underflow");
return 0;
}
return stack[(*top)--];
}
这里,如果我用stack[*top--]
好像不行!有什么不同?为什么main函数中的top变量没有递减?
int main()
{
int stack[4], top = -1, item, id, ch;
for(;;) {
printf("Enter your choice:\n1.push\n2.pop\n3.Print top element\n4.Print all elements\n5.Exit\n");
scanf("%d",&ch);
switch(ch) {
case 1:
printf("Enter the item to be pushed:\n");
scanf("%d",&item);
push(stack,&top,item);
break;
case 2:
id=pop(stack,&top,item);
printf("%d was popped\n",id);
break;
case 4:
print(stack,&top,item);
break;
case 5:
exit(0);
}
}
}
return stack[(*top)--];
here if I use [*top--]
(*top)--
获取 top
指向的对象并递减该对象。因为 *top
指向调用者的 top
,这是您要用来查找数组元素的值。然后 --
递减调用者的 top
,使其指向堆栈中的下一个最新项。
*top--
是*(top--)
,它自减top
,得到未自减值指向的对象。这也获得了调用者的 top
,但它递减了函数的局部 top
,而不是调用者的对象。因为函数的top
是递减的,所以不再指向调用者的top
.
(*top)--
所做的是:
- 取消引用
top
,即访问top
指向的值。 - 减少该值。
*top--
所做的是:
- 递减
top
,即top
本身的值 - 取消引用该值。
除此之外,我认为如果您定义一个堆栈结构而不是使用原始数组和整数作为指针会更好。
#define STACK_CAPACITY 3 // Adjust it as you want
struct stack {
int items[STACK_CAPACITY];
int top;
};
void stack_init(struct stack *s)
{
s->top = -1;
}
int stack_push(struct stack *s, int item)
{
if (s->top == STACK_CAPACITY-1)
return 0; // fail: stack is full
s->items[++s->top] = item;
return 1; // success: item pushed
}
int stack_pop(struct stack *s, int *top)
{
if (s->top == -1)
return 0;
if (top != NULL) // if top is NULL, ignore it
*top = s->items[s->top];
s->top--;
return 1;
}
以下是使用方法:
int main()
{
struct stack s;
stack_init(&s);
if (!stack_push(&s, 1))
printf("Stack is full\n");
if (!stack_push(&s, 2))
printf("Stack is full\n");
if (!stack_push(&s, 3))
printf("Stack is full\n");
if (!stack_push(&s, 4))
printf("Stack is full\n");
if (!stack_push(&s, 5))
printf("Stack is full\n");
int item;
stack_pop(&s, &item);
printf("top = %d\n", item); // outputs 3
stack_pop(&s, NULL); // Ignore the top
stack_pop(&s, &item);
printf("top = %d\n", item); // outputs 1
if (!stack_pop(&s, NULL)) {
printf("Stack is empty: cannot pop\n");
}
}
另外,不要使用 scanf()
来读取用户输入。 fgets()
更安全。
在 C 中,所有后缀运算符的优先级都高于所有前缀(或中缀)运算符。这就是语言的定义方式。所以
*top--
等同于
*(top--)
如果你想要
(*top)--
你需要明确的括号。