| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 886 人关注过本帖
标题:链表的问题
只看楼主 加入收藏
s_k_y
Rank: 1
等 级:新手上路
帖 子:113
专家分:0
注 册:2008-2-2
结帖率:44.44%
收藏
 问题点数:0 回复次数:2 
链表的问题
这是一个动态链表的程序,但是执行结果不对。不如说插入数据,在打印。打印不出全部插入的数据。大家帮看看
#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-08 16:46
很远的那颗星
Rank: 2
等 级:新手上路
威 望:4
帖 子:544
专家分:0
注 册:2008-6-30
收藏
得分:0 
没见过这种写法,你的程序能运行吗?

Fighting~~~~~~~~
2008-10-08 17:15
s_k_y
Rank: 1
等 级:新手上路
帖 子:113
专家分:0
注 册:2008-2-2
收藏
得分:0 
可以,运行没问题。结果不对!
2008-10-08 18:47
快速回复:链表的问题
数据加载中...
 
   



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

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