2012-05-17 124 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