2011-04-06 49 views
0

有大量的錯誤,嚴重出現了什麼問題? 我試過用它沒有typedef但是什麼問題?任何人都可以幫我調試這個請嗎?C堆棧錯誤鏈接LIst

struct node { 
    int info; 
    struct node *link; 
}; 

int main (void) 
{ 
    int choice; 
    struct node *top; 
    top = NULL; 
    while (1) { 
     printf("1.Push\n"); 
     printf("2.Pop\n"); 
     printf("3.Display\n"); 
     printf("4.Quit\n"); 
     printf("Enter your choice : "); 
     scanf("%d", &choice); 

     switch(choice) { 
      case 1: 
       push(); 
       break; 
      case 2: 
       pop(); 
       break; 
      case 3: 
       display(); 
       break; 
      case 4: 
       exit(1); 
      default: 
       printf("Wrong choice\n"); 
     } 
    } 
    return 0; 
} 

void push (void) 
{ 
    struct node *tmp; 
    int pushed_item; 
    tmp = malloc(sizeof(struct node)); 
    printf("Input the new value to be pushed on the stack : "); 
    scanf("%d", &pushed_item); 
    tmp->info = pushed_item; 
    tmp->link = top; 
    top = tmp; 
} 

void pop (void) 
{ 
    struct node *tmp; 
    if (top == NULL) 
     printf("Stack is empty\n"); 
    else { 
     tmp = top; 
     printf("Popped item is %d\n", tmp->info); 
     top = top->link; 
     free(tmp); 
    } 
} 

void display (void) 
{ 
    struct node *ptr; 
    ptr = top; 
    if (top == NULL) 
    printf("Stack is empty\n"); 
    else { 
     printf("Stack elements :\n"); 
     while (ptr != NULL) { 
      printf("%d\n", ptr->info); 
      ptr = ptr->link; 
     } 
    } 
} 
+2

你能正確地格式化代碼,請? – 2011-04-06 23:15:13

+1

你有什麼錯誤?請發佈準確的錯誤消息。 – 2011-04-06 23:15:39

+0

請發佈您正在使用的編譯器,您正在使用的編譯器選項以及您所看到的特定錯誤輸出。 – bta 2011-04-07 00:01:18

回答

1

首先,main函數將不得不低於它所調用的函數。其次,您需要#import <stdio.h>才能使用printf。第三,top不是一個全局變量,所以你不能在display函數中使用它。

從那裏開始工作。

+2

'#import'或'#include' - 太多的Java有好友! – corsiKa 2011-04-06 23:22:48

+0

另外,如果他的代碼結構正確,他的函數就在'.h'文件中,而這只是他的代碼文件。 (注意,這是一個if和代碼的外​​觀,如果是這樣的話)。 – corsiKa 2011-04-06 23:23:45

+2

main函數不必低於它所調用的函數 - 只是將上面的其他函數原型化(應該在頭文件中文件一般)。 – 2011-04-06 23:25:25

1

嘗試把這個片段在你的文件的頂部:

#include <stdio.h> 
#include <stdlib.h> 

void push(void); 
void pop(void); 
void display(void); 

struct node* top; // good catch by Borealid 
+0

您還需要#include 退出並免費。 – Jess 2011-04-07 00:29:55

+1

撇開代碼,謝謝。 – Marlon 2011-04-07 00:36:47