2012-04-24 2 views
1

아래 오류 메시지가 나타납니다. 해결할 수 없습니다. 봤 거든. 마침내 그것을 여기에 넣으려고 생각했다.연결된 목록으로 스택 구현

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

고맙습니다. 많이 ... –

+0

와우, 혼란 스럽습니다. 마지막 줄에서 스택은 struct stack 또는 typedef struct stack 스택을 참조합니까? 별칭의 다른 이름이 더 명확 할 수 있습니다. – Deverill

+0

@Deverill 같은 의미이지만, 마지막 줄의'struct stack'은 태그가 아니라'typedef'-ed 별칭에 의해 참조됩니다. 태그와 동일한 별칭을 사용하는 것이 상대적으로 일반적입니다. – dasblinkenlight

5

typedef를 제거하고 모두가 잘 될 것입니다.

3

당신은 새로운 유형의 원하지 않는 대신

typedef struct stack *node, *top; 

을, 당신이 원하는 새 변수 :

struct stack *node, *top; 
관련 문제