2017-06-10 20 views
-2

這是我用很喜歡列表棧的程序執行棧的程序不斷出現0

#include<stdio.h> 
    #include<conio.h> 
    #include<stdlib.h> 
    struct SNode 
    { 
     int data; 
     struct SNode* next; 
    }; 
    void push(struct SNode** top,int x) 
    { 
     struct SNode* temp=(struct SNode*)malloc(sizeof(SNode)); 
     { 
      struct SNode* holder=*top; 
      temp->data=x; 
      temp->next=(holder); 
      holder=temp; 
      printf("%d was Pushed",x); 
     } 
    } 

我不明白th​​is..i認爲有SPME問題就在這裏......

void pop(struct SNode** top) 
{ 
    struct SNode* temp; 
    struct SNode* holder=*top; 
    int x1; 
    if(holder==NULL) 
    { 
     printf("Stack is empty "); 
    } 

    temp=holder; 
    x1=holder->data; 
    holder=holder->next; 
    printf("%d was popped from the stack",x1); 
    free(temp); 
} 

看過來過它總是在堆棧的頂部顯示爲0

void peek(struct SNode* top) 
{ 
    printf("%d is at the top of the stack",top); 
} 
void main() 
{ 
    clrscr(); 
    int x,c,c1; 
    jump: 

這是我的標籤

printf("Do you want to Push/Pop/Peek an element ??\n"); 
      printf("1)Push\t2)Pop\t3)Peek\n"); 
      scanf("%d",&c); 
      struct SNode* top=NULL; 

開關殼體

switch(c) 
       { 

case 1: 


    { 
           printf("Enter Element :"); 
           scanf("%d",&x); 
           push(&top,x); 
           printf("\nDo you want to continue :1)Yes 2)No\n"); 
           scanf("%d",&c1); 
           if(c1==1) 
           { 
            goto jump; 
           } 
           else 
           { 
            printf("GGWP...."); 
           } 
          break; 

          } 

彈出

case 2: 
         { 
          pop(&top); 
          printf("\nDo you want to continue :1)Yes 2)No\n"); 
          scanf("%d",&c1); 
          if(c1==1) 
          { 
           goto jump; 
          } 
          else 
          { 
           printf("GGWP...."); 
          } 
          break; 

         } 

頂部堆疊的

  case 3: 
      { 
       peek(top); 
       printf("\nDo you want to continue :1)Yes 2)No\n"); 
       scanf("%d",&c1); 
       if(c1==1) 
       { 
        goto jump; 
       } 
       else 
       { 
        printf("GGWP...."); 
       } 
       break; 

      } 
        default: 
        { 
         printf("Enter the correct option ..."); 
         goto jump; 
         break; 
        } 
       } 
       getch(); 
      } 

任何幫助將不勝感激

+3

解決此類問題的正確工具是您的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,你應該[編輯]你的問題,以包含一個[Minimal,Complete,and Verifiable](http://stackoverflow.com/help/mcve)例子來重現你的問題,以及你在調試器中所做的觀察。 –

+7

請不要在沒有嚴肅推理的情況下使用'goto'! –

+0

首先,你的「pop()」實際上並沒有彈出(沒有賦值給「* top」)。因此,它會打印頂部元素並釋放它,但「頂部」仍然指向那個晃動的內存。 – axalis

回答

0

此代碼的工作......我做了一些失誤

#include<stdio.h> 
#include<conio.h> 
#include<stdlib.h> 
struct SNode 
{ 
    int data; 
    struct SNode* next; 
}; 
void push(struct SNode** top,int x) 
{ 
    struct SNode* temp=(struct SNode*)malloc(sizeof(SNode)); 
    { 

持有人將不能保持,你將不得不返回其值和捕捉它在無效的主要結構( ) // struct SNode * holder = * top;

temp->data=x; 
      temp->next=(*top); 
      *top=temp; 
      printf("%d was Pushed",temp->data); 
     } 
    } 
    void pop(struct SNode** top) 
    { 
     struct SNode* temp; 
     int x1; 
     if(*top==NULL) 
     { 
      printf("Stack is empty "); 
     } 
     //printf("%d",*top->data); 
     temp=*top; 
     x1=(*top)->data; 
     *top=(*top)->next; 
     printf("%d was popped from the stack",x1); 
     free(temp); 
    } 
    void peek(struct SNode* top) 
    { 
     printf("%d is at the top os the stack",top->data); 
    } 
    void main() 
    { 
     clrscr(); 
     int x,c,c1; 
     int i; 
     struct SNode* top=NULL; 
     int counter=0; 
     jump: 
     printf("Do you want to Push/Pop/Peek/Display an element ??\n"); 
     printf("1)Push\t2)Pop\t3)Peek\t4)Display\n"); 
     scanf("%d",&c); 
     switch(c) 
     { 
      case 1: 
      { 
       printf("Enter Element :"); 
       scanf("%d",&x); 
       push(&top,x); 
       counter++; 
       printf("\nDo you want to continue :1)Yes 2)No\n"); 
       scanf("%d",&c1); 
       if(c1==1) 
       { 
        goto jump; 
       } 
       else 
       { 
        printf("GGWP...."); 
       } 
      break; 

      } 
      case 2: 
      { 
       pop(&top); 
       counter--; 
       printf("\nDo you want to continue :1)Yes 2)No\n"); 
       scanf("%d",&c1); 
       if(c1==1) 
       { 
        goto jump; 
       } 
       else 
       { 
        printf("GGWP...."); 
       } 
       break; 

      } 
      case 3: 
      { 
       peek(top); 
       printf("\nDo you want to continue :1)Yes 2)No\n"); 
       scanf("%d",&c1); 
       if(c1==1) 
       { 
        goto jump; 
       } 
       else 
       { 
        printf("GGWP...."); 
       } 
       break; 

      } 
      case 4: 
      { 
       for(i=0;i<counter;i++) 
       { 
        int x2; 
        x2=(top)->data; 
        printf("%d ->",x2); 
        top=(top)->next; 

       } 
       printf("\nDo you want to continue :1)Yes 2)No\n"); 
       scanf("%d",&c1); 
       if(c1==1) 
       { 
        goto jump; 
       } 
       else 
       { 
        printf("GGWP...."); 
       } 
       break; 
      } 
      default: 
      { 
       printf("Enter the correct option ..."); 
       goto jump; 
       break; 

      } 
     } 
     getch(); 
    }