在C中实现带有链表的堆栈

我在使用带有struct的链表实现Stack时遇到了麻烦。 程序编译很好,但是当我运行它时,它打印第一个元素,但然后将下一个节点作为NULL读取。 我认为将堆栈传递给push方法可能是一个错误但我不确定并且我没有成功修复它所以我在寻求你的帮助:

#include  #include  struct stackNode{ char data; struct stackNode *nextPtr; }; typedef struct stackNode StackNode; typedef StackNode *StackNodePtr; void convertToPostfix(char infix[], char postfix[]); int isOperator(char c); int precedence(char operator1, char operator2); void push(StackNodePtr *topPtr, char value); char pop(StackNodePtr *topPtr); char stackTop(StackNodePtr topPtr); int isEmpty(StackNodePtr topPtr); void printStack(StackNodePtr topPtr); int main(){ convertToPostfix(NULL, NULL); return 0; } void convertToPostfix(char infix[], char postfix[]){ StackNode stack = {'(', NULL}; StackNodePtr stackPtr = &stack; push(stackPtr, 'a'); //printf("%s\n", stackPtr->data); printStack(&stack); } void push(StackNodePtr *topPtr, char value){ StackNode *node; node=(StackNodePtr)malloc(sizeof(StackNodePtr)); node->data=value; node->nextPtr=*topPtr; *topPtr=node; } void printStack(StackNodePtr topPtr){ if(topPtr == NULL){ printf("%s\n", "NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO"); return; } printf("%c\n", topPtr->data); printStack(topPtr->nextPtr); } 

任何帮助,将不胜感激。

谢谢

我能看到几个问题:

1) printStack(&stack); 应该是printStack(stackPtr); 因为你将stackPtr地址stackPtr给push函数。

2)

 node = (StackNodePtr)malloc(sizeof(StackNodePtr)); 

应该:

 node = malloc(sizeof(StackNode)); 

3)

 push(stackPtr, 'a'); 

应该:

 push(&stackPtr, 'a'); 

因为你需要传递顶部指针的地址。

这是不正确的:

 node=(StackNodePtr)malloc(sizeof(StackNodePtr)); 

因为它只为struct stackNode*分配内存(对于任何指针类型通常为4字节),它应该为struct stackNode分配内存(至少5个字节):

 node = malloc(sizeof(StackNode)); 

请参阅我是否转换了malloc的结果?

 #include  #include  struct node { int data; struct node * link; }; void push(struct node **, int); int pop(struct node **); void display(struct node *); void printMenu(); int main() { struct node * p; p = NULL; int data, ch, data1; //char choice[10]; do { printMenu(); printf("Enter your choice\n"); scanf("%d", &ch); switch (ch) { case 1: printf("Enter the element to be pushed\n"); scanf("%d", &data); push(&p, data); break; case 2: data1 = pop(&p); if (data1 != -1000) printf("The popped element is %d\n", data1); break; case 3: printf("The contents of the stack are"); display(p); printf("\n"); break; default: return 0; } } while (1); return 0; } void printMenu() { printf("Choice 1 : Push\n"); printf("Choice 2 : Pop\n"); printf("Choice 3 : Display\n"); printf("Any other choice : Exit\n"); } void push(struct node **q, int num) { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp->link = (*q); temp->data = num; (*q) = temp; } void display(struct node *q) { //Fill in the code here struct node *temp = q; if (temp == NULL) printf(" {}"); while (temp != NULL) { printf(" %d", temp->data); temp = temp->link; } } int pop(struct node **q) { //Fill in the code here struct node *temp; int item; if (*q == NULL) { printf("Stack is empty\n"); return -1000; } temp = *q; item = temp->data; *q = (*q)->link; free(temp); return item; }