2012-04-24 86 views
1

我收到以下錯誤消息。 無法解決它。 Google搜索了很多。最後想到把它放在這裏。按鏈接列表執行堆棧

enter image description here

#include <stdio.h> 
#include <stdlib.h> 
#include <malloc.h> 
int stop; 
struct stack 
{ 
    int data; 
    struct stack *next; 
}; 
typedef struct stack *node, *top; 
//typedef struct stack *top; 
void push() 
{ 
    int i; 
    struct stack *x; 
    x = malloc(sizeof(struct stack)); 
    printf("\n Enter the element your want to insert"); 
    scanf("%d", &i); 
    x->data = i; 
    x->next = top; 
    top = x; 
} 
void pop() 
{ 
    int i; 
    if(top == NULL) 
    { 
     printf("\nStack is empty\n"); 
    } 
    else{ 
    i = top->data; 
    free(top); 
    top = top->next; 

    } 
} 
void display() 
{ 
    if(node != NULL) 
    { 
     printf("%d ", node->data); 
     node = node->next; 
    } 

} 
int main() 
{ 
    int ch; 
    while(1) 
    { 
     printf("\nEnter your option \n1. Insert(Push) \n2. Delete(Pop) \n3. Display : \n"); 
     scanf("%d", &ch); 
     switch(ch) 
     { 
      case 1: 
        push(); 
        break; 
      case 2: 
        pop(); 
        break; 
      case 3: 
        display(); 
        break; 
      default: 
        printf("Invalid Entery, Try Again"); 
     } 
    } 
return 0; 
} 

回答

1

您不能將變量聲明與typedef組合起來。如果您想爲struct stack創建別名,並調用它只是stack,修改代碼如下:

struct stack { 
    int data; 
    struct stack *next; 
}; 
typedef struct stack stack; 
stack *node, *top; 
+0

非常感謝... – 2012-04-24 13:52:44

+0

哇,這很混亂。在最後一行中,堆棧是指struct struct還是typedef struct stack stack?也許別名的另一個名字會更清晰。 – Deverill 2012-04-24 13:57:13

+0

@Deverill同樣的事情,但在最後一行'struct stack'被它的'typedef'-alias引用,而不是它的標籤。使用與標籤相同的別名是相對常見的做法。 – dasblinkenlight 2012-04-24 13:59:56

5

刪除typedef,一切都會好起來的。

3

你不希望一個新的類型:

typedef struct stack *node, *top; 

相反,你想新變量:

struct stack *node, *top;