您的位置:程序门 -> c/c++ -> 新手乐园



各位高手!请帮小弟看看这程序为什么会出现死循环!谢谢了!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


各位高手!请帮小弟看看这程序为什么会出现死循环!谢谢了![已结贴,结贴人:kakainzaghi]
发表于:2007-06-21 22:41:43 楼主
#include <stdlib.h>
struct   list
{
      int   data;
      struct   list   *next;
};
typedef   struct   list   stack;
typedef   stack*   link;
link   top=null;

/*将数据压入堆栈*/

void   push(int   item)
{
      link   new_node;
      new_node=(link)malloc(sizeof(stack));
      new_node-> data=item;
      top=new_node;
      new_node-> next=top;
}
int   pop()
{
      int   temp;
      link   ding;
      if(top!=null)
      {
              ding=top;
              top=top-> next;
              temp=ding-> data;
              free(ding);
              return   temp;
        }
        else
              return   -1;
}
void   main()
{
    int   temp;
    int   select;
    link   point=null;
   
    printf( "\n1.input:\n ");
    printf( "2.output\n ");
    printf( "exit\n ");
    scanf( "%d ",&select);
   
    do
    {
          switch(select)
          {
              case   1:   printf( "input   a   data: ");
                              scanf( "%d ",&temp);
                              push(temp);
                              point=top;
                             
                           
                              while(point!=null)
                              {    
                                  printf( "%d\n ",point-> data);
                                  point=point-> next;
                                }
                               
                                break;
              case   2:   if((temp=pop())==-1)
                                  printf( "the   list   is   empty\n ");
                              else
                              {
                                    printf( "the   output   is   %d ",temp);
                                  point=top;
                                    while(point!=null)
                              {     printf( "the   afterdata   is%d\n ",point-> data);
                                  point=point-> next;
                                }
                                }  
                                break;
                  }
              printf( "\n1.input:\n ");
              printf( "2.output\n ");
              printf( "exit ");
              scanf( "%d ",&select);
            }while(select!=3);
          printf( "\nthank   you\n ");
                system( "pause ");  
     
}
     
发表于:2007-06-21 22:55:341楼 得分:15
void   push(int   item)
{
link   new_node;
new_node=(link)malloc(sizeof(stack));
new_node-> data=item;
new_node-> next=top;
top=new_node;

}
new_node-> next=top;
top=new_node;

这两句换一个位置就行了
发表于:2007-06-22 01:03:082楼 得分:5
上面的朋友说的对,你这样将链的头指向新结点,又将新结点的next指针指向链头,这就形成了一个指针之间的循环,只要按上面朋友所说,将   new_node-> next   =   top   与   top   =   new_node   相互换一下位置即可。
发表于:2007-06-22 08:35:153楼 得分:0
说的对!
发表于:2007-06-23 19:13:254楼 得分:0
太谢谢了!我已经可以运行出来了!谢谢!
发表于:2007-06-23 20:35:525楼 得分:0
就搞定了,太快了把


快速检索

最新资讯
热门点击