| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 440 人关注过本帖
标题:数结初学者的链栈,我实在找不出错在哪里,求大神帮忙
只看楼主 加入收藏
chuanglan
Rank: 2
等 级:论坛游民
威 望:2
帖 子:91
专家分:29
注 册:2012-8-14
结帖率:84.62%
收藏
已结贴  问题点数:10 回复次数:3 
数结初学者的链栈,我实在找不出错在哪里,求大神帮忙
我想知道Creat函数里面到底发生什么了,为什么调试的结果这么乱,帮忙看看,谢谢啦
#include <stdio.h>                           
#include<malloc.h>                           
  typedef struct node
    {                        
       int data;                                 
       struct node *next;                        
    }Node,*Linkstack;                                                                       

  void Creat_Stack(Linkstack S)               
  {                                            
     int n,i;
     Node *p = S;                                   
     printf("please input the max n:\n");         
     scanf("%d\n",&n);
     for(i  =0;i < n;i++)                             
      {
        p->next = (Node*)malloc(sizeof(Node));        
        scanf("%d ",&p->data);                     
        p = p->next;
      }                                            
     p = NULL;
     p = S->next;
     while(p)
       {
          printf("%3d",p->data);
          p = p->next;
       }
  }                                             
                              
  void Push_Stack(Linkstack S,int e)            

  {                                            
     Node *p;
     p = (Node *)malloc(sizeof(Node));                                   
     p->data=e;                                   
     p->next=S->next;                             
     S->next=p;                                   
  }                                                                                         

  int  Pop_stack(Linkstack S,int *e)           
  {                                            
      Node *p;                                    
      if(S->next==NULL)
       {
          puts("It's a empty stack");
          return 0;
       }                 
     p=S->next;                                   
     *e=p->data;                                 
     S->next=p->next;                             
     free(p);                                    
     return 1;                                    
  }                                             
                                          
  int Pf(Linkstack S)                          
  {                                            
     Node *p;                                    
     p=S->next;                                 
     printf("\n");                              
     while(p!=NULL)                              
     {                                         
       printf("%d",p->data);
       p=p->next;                              
     }                                         
   }                                                                                          

  void main()                                 
  {                                            
     Linkstack N;                                
     int e,a;
     N = (Node *)malloc(sizeof(Node));
     N->next = NULL;
     Creat_Stack(N);
   /*  printf("please input what you want to push e:\n");
     scanf("%d\n",&e);                                                                       
     Push_Stack( N,e);                           
      printf("after pushing,the Linkstack is:\n");
    Pf(N);                                      
     Pop_stack( N, &a);                          
     printf("after poping,the Linkstack is:\n");
     Pf(N);   */                                   
     getch();
   }  
push 跟 pop两个函数我调试过,是对的
搜索更多相关主题的帖子: include 
2012-11-04 14:58
chuanglan
Rank: 2
等 级:论坛游民
威 望:2
帖 子:91
专家分:29
注 册:2012-8-14
收藏
得分:0 
我找出来了,是指针用错了,指针真的得慎用,但是现在push 跟 pop函数没作用了
很奇怪#include <stdio.h>                           
#include<malloc.h>                           
  typedef struct node
    {                        
       int data;                                 
       struct node *next;                        
    }Node,*Linkstack;                                                                       

    void Creat_Stack(Linkstack *S)
  {                                             
     int n,i;  
     Node *p = *S;
     for(i=0;i<5;i++)
      {
        p->next=(Node*)malloc(sizeof(Node));
        p = p->next;
        scanf("%d ",&p->data);
      }                                             
     p->next = NULL;
     p = (*S)->next;
  }                                             
                                                
  void Push_Stack(Linkstack *S,int e)
  {                                             
     Node *p;
     p = (Node *)malloc(sizeof(Node));                                    
     p->data=e;                                    
     p->next=(*S)->next;
     (*S)->next=p;
  }                                                                                                                                                
int  Pop_stack(Linkstack *S,int *e)
  {                                             
      Node *p = *S;
      if((*S)->next==NULL)
       {
          puts("It's a empty stack");
          return 0;
       }                  
     p=(*S)->next;
     *e=p->data;                                    
     (*S)->next=p->next;
     free(p);                                       
     return 1;                                      
  }                                                
                                          
  int Pf(Linkstack S)                          
  {                                            
     Node *p;                                    
     p=S->next;                                 
     printf("\n");                              
     while(p!=NULL)                              
     {                                         
       printf("%4d",p->data);
       p=p->next;                              
     }                                         
   }                                                                                          

  void main()                                 
  {                                            
     Linkstack N;                                
     int e,a;
     N = (Node *)malloc(sizeof(Node));
     N->next = NULL;
     Creat_Stack(N);
   /*  printf("please input what you want to push e:\n");
     scanf("%d\n",&e);                                                                       
     Push_Stack( N,e);                           
      printf("after pushing,the Linkstack is:\n");
    Pf(N);                                      
     Pop_stack( N, &a);                          
     printf("after poping,the Linkstack is:\n");
     Pf(N);   */                                   
     getch();
   }
2012-11-04 15:56
爱闹的娃
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:3
帖 子:265
专家分:975
注 册:2011-10-23
收藏
得分:7 
LZ....你这是在写链表对吧???栈的链式写法不是这样子的....至少能让别人知道有栈顶和栈底指针...
2012-11-04 16:13
chuanglan
Rank: 2
等 级:论坛游民
威 望:2
帖 子:91
专家分:29
注 册:2012-8-14
收藏
得分:0 
呵呵,着程序不是我写的,我知道这是错误的写法,我只是想知道为什么会错,经过研究,是指针在作怪,现在的问题我还是没看出来,对了,栈顶跟栈底是顺序栈的,链栈没有这两个指针,
2012-11-04 21:28
快速回复:数结初学者的链栈,我实在找不出错在哪里,求大神帮忙
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018246 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved