2012-05-17 125 views
2

我在使用链接列表和struct实现堆栈时遇到了问题。程序编译得很好,但是当我运行它时,它会打印第一个元素,然后将下一个节点作为NULL读取。我想可能是我的传球堆栈push方法的错误,但我不知道,我还没有成功地修复它,所以我要求你的帮助:在C中使用链接列表实现堆栈

#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("%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); 
} 

任何帮助不胜感激。

感谢

回答

2

的几个问题,我可以看到:

1)printStack(&stack);printStack(stackPtr);为您传递的stackPtr地址的推送功能。

2)

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

应该是:

node = malloc(sizeof(StackNode)); 

3)

push(stackPtr, 'a'); 

应该是:

push(&stackPtr, 'a'); 

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

+1

非常感谢,比我意识到的更多的问题,但现在修好了,谢谢你。 – nain33

+0

有没有我在堆栈或溢出:) – Jay

1

这是不正确的:

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

,因为它是唯一一个struct stackNode*(通常4个字节为任何指针类型)分配内存,当它应该为一个struct stackNode(至少5个字节)被分配存储器:

node = malloc(sizeof(StackNode)); 

-

Do I cast the result of malloc?

0
#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 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; 
} 
+0

这个问题的答案如何? – mpromonet

+1

虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的其他上下文可提高其长期价值。 – JAL