2011-04-06 4 views
0

엄청난 양의 오류가 있습니다. 무엇이 잘못 되었습니까? typedef를 사용하지 않으려 고 시도했지만 그게 무슨 문제입니까? 아무도 나를 디버깅 도와 드릴까요?C 스택 오류가 발생했습니다.

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

당신은, 제대로 코드를하시기 바랍니다 포맷 할 수 있습니까? –

+1

그리고 오류가 무엇입니까? 정확한 오류 메시지를 게시하십시오. –

+0

사용중인 컴파일러, 사용중인 컴파일러 옵션 및보고있는 특정 오류 출력을 게시하십시오. – bta

답변

1

먼저 해제는 main 함수는 호출하는 기능 이하로해야 할 것입니다. 둘째, printf을 사용하려면 #import <stdio.h>해야합니다. 셋째, top은 글로벌 변수가 아니므로 display 함수 내에서 사용할 수 없습니다.

거기에서 일하십시오.

+2

'# import' 또는'# include' - 너무 많은 Java 버디! – corsiKa

+0

또한 코드가 올바르게 구성되어 있으면 그의 함수는'.h' 파일에 있으며 이것은 그의 코드 파일입니다. (참고로 if와 code의 외형에 의해 큰 if) – corsiKa

+2

'main' 함수는 호출하는 함수 아래에있을 필요는 없습니다. 위에서 다른 함수를 프로토 타입합니다 (헤더에 있어야합니다). 일반적으로 파일). –

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

+1

코드를 건너 뛰었습니다. 고마워요. – Marlon

관련 문제