鱼C论坛

 找回密码
 立即注册
查看: 1323|回复: 1

[技术交流] 链表 作业,帮我看下有无错误

[复制链接]
发表于 2015-4-3 13:21:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 haiouda 于 2015-4-3 13:28 编辑
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #define LNK  sizeof(struct Student) //定义一个宏,长度为结构的长度



  4. int n;      //全局变量

  5. struct Student         //声明结构
  6. {
  7.         long int num;
  8.         double score;
  9.         struct Student *next;
  10. };



  11. struct Student *inpu(void)     //建立链表函数
  12. {
  13.         struct Student *p1,*p2,*head;
  14.     head=NULL;
  15.         p1=p2= (struct Student *)malloc(LNK);  //开辟一个新单元
  16.         
  17.         printf("输入学号:");        
  18.         scanf("%d",&p1->num);
  19.         printf("输入成绩:");
  20.         scanf("%lf",&p1->score);
  21.         
  22.         while( p1->num)
  23.         {
  24.                 n+=1;
  25.                 if(n==1) head=p1;
  26.                 else        p2->next=p1;
  27.                
  28.         p2=p1;
  29.                 p1=(struct Student *)malloc(LNK);     //开辟一个新单元
  30.                 printf("输入学号:");        
  31.                 scanf("%d",&p1->num);
  32.                 printf("输入成绩:");
  33.                 scanf("%lf",&p1->score);
  34.                                                                
  35.         }
  36.         
  37.         p2->next=NULL;        
  38.     return head ;
  39. }

  40. void print(struct Student *head)    //输出链表函数
  41. {
  42.         struct Student *p;
  43.         
  44.         printf("这个链表共有%d个结点:\n",n);
  45.         
  46.         if(head!=NULL)
  47.         {
  48.                 p=head;
  49.                 do
  50.                 {
  51.                         printf("学号:%d 成绩:%5.2lf\n",p->num,p->score);
  52.                         
  53.                         p=p->next;
  54.                         
  55.                 } while(p!=NULL);
  56.         }
  57.         
  58. }


  59. struct Student *insert( struct Student *head)   //插入一个结点
  60. {
  61.         struct Student *p0,*p1,*p2;
  62.         p0=(struct Student *)malloc(LNK);
  63.         
  64.         printf("输入要插入的学号:");        
  65.         scanf("%d",&p0->num);
  66.         printf("输入成绩:");
  67.         scanf("%lf",&p0->score);
  68.         p0->next=NULL;
  69.         
  70.         
  71.         p1=p2=head;
  72.         
  73.     if(head== NULL)
  74.         {
  75.                 p0=head;
  76.                 p0->next=NULL;
  77.                
  78.                
  79.         }
  80.         else
  81.         {
  82.                 while(p0->num > p1->num  && p1->next!=NULL )
  83.                 {
  84.                         p2=p1;
  85.                         p1=p1->next;
  86.                 }
  87.                
  88.                
  89.         if(p0->num <=  p1->num)
  90.                 {
  91.                         
  92.                         if(p1 == head        )         head=p0;
  93.                         else
  94.                         {
  95.                                 
  96.                                 p2->next=p0;
  97.                                 
  98.                         }
  99.                         
  100.                         p0->next=p1;
  101.                         
  102.                 }
  103.                
  104.                 else
  105.                 {
  106.                         p1->next=p0;
  107.                         p0->next=NULL;
  108.                 }
  109.                
  110.                
  111.                
  112.                
  113.                
  114.         }
  115.         
  116.         
  117.         n=n+1;        
  118.         
  119.         
  120.         
  121.         return head;
  122. }


  123. struct Student *del( struct Student *head)    //删除学号
  124. {
  125.         
  126.         struct Student *p1,*p2;
  127.         long int num;        
  128.         printf("输入要删除的学号:");        
  129.         scanf("%d",&num);
  130.         
  131.         p1=p2=head;
  132.         
  133.         if( head == NULL)
  134.         {
  135.                 printf("这是一个空链表!");
  136.                 goto END;
  137.         }
  138.         else
  139.         {
  140.                 while( num != p1->num  )
  141.                 {
  142.                         p2=p1;
  143.                         p1=p1->next;
  144.                         if (p1->next==NULL) break;
  145.                         
  146.                 }
  147.                
  148.                 if( p1==head)
  149.                 {
  150.                         head=p1->next;
  151.                         
  152.                 }
  153.                 else if(p1->next == NULL)
  154.                 {
  155.                         if (num==p1->num)        p2->next =NULL;
  156.                         else
  157.                         {
  158.                                 printf("输入错误!\n");
  159.                                 goto END;
  160.                         }
  161.                         
  162.                         
  163.                 }
  164.                
  165.                 else
  166.                 {
  167.                         p2->next= p1->next;
  168.                 }
  169.                
  170.                
  171.                
  172.         }
  173.         
  174.         
  175.         n=n-1;
  176.         
  177. END:        
  178.         return head;
  179.         
  180. }






  181. int main()
  182. {
  183.     struct Student *head,*p;
  184.         int n;
  185.         head=inpu();

  186.         
  187.    
  188.         
  189.         do{
  190.                 printf("n=1时打印链表;n=2时插入;n=3时删除一个学号;n==0退出程序;n=");
  191.                
  192.                 scanf("%d",&n);
  193.                
  194.                
  195.                
  196.                
  197.                 if (n==0) break;    //退出
  198.                
  199.                
  200.                
  201.                 switch(n)
  202.                 {
  203.                         
  204.                         
  205.                 case 1:        {         p=head; print(p);        break; }        //打印
  206.                         
  207.                 case 2: {         p=head;  head=insert(p); break;} // 添加
  208.                         
  209.                 case 3:        {         p=head; head=del(p);        break;}  // 删除
  210.                         
  211.                 default:  
  212.                         printf("输入错误!请重新输入\n");
  213.                         
  214.                         
  215.                         
  216.                 }
  217.                
  218.                
  219.         }while(1);
  220.         
  221.         
  222.         return 0;
  223. }
复制代码

还有木有错误了?


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-4-23 10:16:47 | 显示全部楼层
```我的思路是用C++写的... 先定义一个结构体.. 在写一个类...在类里面定义头指针``和添加删除插入的函数``!! 构造函数里把头指针赋值为NULL...  创建链表函数里..判断头指针是否为NULL``.
如果不为空``就在末尾添加一个节点.. 思路都差不多```用类写的话``不用定义全局..!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-19 16:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表