| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1115 人关注过本帖
标题:大家帮忙了~~~链表问题
只看楼主 加入收藏
s_k_y
Rank: 1
等 级:新手上路
帖 子:113
专家分:0
注 册:2008-2-2
结帖率:44.44%
收藏
 问题点数:0 回复次数:6 
大家帮忙了~~~链表问题
这是一个动态链表的程序,但是执行结果不对。
 实现的功能是
      1创建空链表
      2插入数据
      3打印数据
     
   问题是插入数据后,打印不出全部的插入数据。      
   大家帮改改了。
#include "stdlib.h"
#include "malloc.h"

struct node
{
    int data;
    struct node *next;
   
};



struct node *create_list(void)
{
   
  struct node *head;
  clrscr();
  head=(struct node*)malloc(sizeof(struct node));   
    if(head!=NULL)
    {
      printf("successful");
      head->next=NULL;   
    }
    else
         printf("fail");

     printf("\nInput any key to return menu...");
   getch();
   menu();
        return head;
}

struct node *insert_list(struct node *head,int value)
{
   
   struct node *n,*p,*q;
   n=(struct node *)malloc(sizeof(struct node));   

     n->data=value;
     p=head;
     if(head==NULL)
     {
       head=n;
       n->next=NULL;
     }
     else
         {
             while((p->next!=NULL)&&(p->data<value))
               {q=p;p=p->next;}
              if(p->data>=value)
                  { if(p==head)
                       {
                           n->next=head;
                           head=n;                              
                       }
                 else
                     {
                          q->next=n;
                          n->next=p;
                     }
                  }
              else
                  {
                    p->next=n;
                    n->next=NULL;                          
                  }
         }
     printf("\nInput any key to return menu...");
   getch();
   menu();            
     return head;   
}

void print_list(struct node *head)
{
     
     struct node *p;
     clrscr();
     p=head->next;
   if(head==NULL)
      {
           printf("the list is null!!");     
      }
    else
        {
            printf("print the numbers: ");
            while(p!=NULL)
            {
                printf(" %d",p->data);
                p=p->next;
               
            }
            
        }
   printf("\nInput any key to return menu...");
   getch();
   menu();   
}

menu()
{
      int ch,num;
       struct node *head;
        clrscr();
        printf("***********************\n");
        printf("1.create\n");
        printf("2.insert\n");      
        printf("3.print\n");
        printf("4.exit\n");
        printf("***********************\n");
      printf("Input your choose(1-4):");
      scanf("%d",&ch);
     while (ch<1 || ch>4)
    { printf("Your choose is error! Input again!\n");
       printf("Input your choose(1-4):");
       scanf("%d",&ch);
     }
      switch(ch)
      {
           case 1: head=create_list();break;
           case 2:
                   {
                      clrscr();
                     printf("please input the data:");
                     scanf("%d",&num);

                     head=insert_list(head,num);

                     break;
                    }
           case 3: print_list(head);break;
           case 4: exit(0);break;
      }
    getch();

}

int main(void)
{
   
  menu();
return 0;   
}
搜索更多相关主题的帖子: 链表 
2008-10-10 21:01
ec_月月
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-10-8
收藏
得分:0 
while(p!=NULL)//(p)试试看
2008-10-10 23:33
s_k_y
Rank: 1
等 级:新手上路
帖 子:113
专家分:0
注 册:2008-2-2
收藏
得分:0 
[bo][un]ec_月月[/un] 在 2008-10-10 23:33 的发言:[/bo]

while(p!=NULL)//(p)试试看

   试过,不行的。。。。。。
2008-10-11 14:16
missiyou
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:531
专家分:218
注 册:2007-10-9
收藏
得分:0 
看看你的程序,  getch();
   menu();
        return head;
menu是一个死循环,根本就不出来怎么返回地址,好像也是成功。但是,head是一个空的。
2008-10-11 19:28
missiyou
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:531
专家分:218
注 册:2007-10-9
收藏
得分:0 
你每个函数都有是这样,也都有menu 这个函数会出问题的
2008-10-11 19:29
s_k_y
Rank: 1
等 级:新手上路
帖 子:113
专家分:0
注 册:2008-2-2
收藏
得分:0 
我感觉也是这里的问题。
  我的意思是选择各项操作后还能够回到主菜单。
2008-10-14 23:04
hyl1987419
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-8-20
收藏
得分:0 
不懂!学习学习!
2008-10-15 12:48
快速回复:大家帮忙了~~~链表问题
数据加载中...
 
   



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

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