我在使用带有结构的链表实现堆栈时遇到问题。该程序编译良好,但是当我运行它时,它会打印第一个元素,但然后将下一个节点读取为 NULL。我认为我将堆栈传递给推送方法可能是错误,但我不确定,并且我没有成功修复它,所以我寻求您的帮助:
#include <stdio.h>
#include <stdlib.h>
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("%sn", 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("%sn", "NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
return;
}
printf("%cn", topPtr->data);
printStack(topPtr->nextPtr);
}
任何帮助将不胜感激。
谢谢
我可以看到的几个问题:
1) printStack(&stack);
在将stackPtr
地址传递给推送函数时应printStack(stackPtr);
。
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 <stdio.h>
#include <stdlib.h>
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 choicen");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the element to be pushedn");
scanf("%d", &data);
push(&p, data);
break;
case 2:
data1 = pop(&p);
if (data1 != -1000)
printf("The popped element is %dn", 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 : Pushn");
printf("Choice 2 : Popn");
printf("Choice 3 : Displayn");
printf("Any other choice : Exitn");
}
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 emptyn");
return -1000;
}
temp = *q;
item = temp->data;
*q = (*q)->link;
free(temp);
return item;
}