鱼C论坛

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

[技术交流] 零基础C视频-->57节 动态链表的创建与新的节点的插入!源码!

[复制链接]
发表于 2011-9-19 12:28:57 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 Y.H 于 2011-9-19 12:29 编辑

通常从一段代码的初稿(未优化)就能看作者的最初思路!


所以 我就把这一节的原初稿代码(未优化) 发出来:
个人觉得初稿一半都是作者最初的一个构思(觉得是最具有逻辑(当然啦不是逻辑最强的)的,因为那是一步一步按照最初的算法而来的) 优化后逻辑性更强:

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>

  4. struct student
  5. {
  6.       long int num;
  7.       long double score;
  8.       struct student *next;
  9. };

  10. void main()
  11. {
  12.       struct student *p1, *p2, *head, *p;
  13.       int n;
  14.       long int Temp_Num;
  15.       
  16.       n = 0;
  17.       head = NULL;
  18.       p1 = p2 = (struct student *)malloc(sizeof (struct student));
  19.       
  20.       printf ("input Num : ");
  21.       scanf ("%ld", &p1->num);
  22.       printf ("input Score : ");
  23.       scanf ("%lf", &p1->score);
  24.       
  25.       while (p1->num)
  26.       {
  27.             n++;

  28.             if (1 == n)
  29.             {
  30.                   head = p1;
  31.             }
  32.             else
  33.             {
  34.                   p2->next = p1;
  35.             }

  36.             p2 = p1;
  37.             p1 = (struct student *)malloc(sizeof (struct student));           //开辟新节点 使p1指向这个节点.
  38.             
  39.             printf ("input Num : ");                                          //读入一个num给p1所指的节点.
  40.             scanf ("%ld", &p1->num);
  41.             printf ("input Score : ");
  42.             scanf ("%lf", &p1->score);            
  43.       }
  44.       
  45.       p2->next = NULL;                                                        //p2->next = NULL
  46.       p = head;

  47.       printf ("\ninput %ld number\n\n", n);
  48.       printf ("Num\t\t\tScore\n");      

  49.       if (head)
  50.       {
  51.             do
  52.             {
  53.                   printf ("%ld\t\t\t%3.2lf\n", p->num, p->score);                  
  54.             }
  55.             while (p = p->next);
  56.       }
  57.       

  58.       
  59.       p1 = head;
  60.       p2 = head;
  61.       p = (struct student *)malloc(sizeof (struct student));
  62.       printf ("\n\nplease input insert Num: ");      //按照(num)数字前后顺序插入节点
  63.       scanf ("%ld", &p->num);
  64.       printf ("please input insert score: ");
  65.       scanf ("%lf", &p->score);

  66.       if (head == NULL)                                     //如果是空链表的情况.
  67.       {
  68.             head = p;
  69.             head->next = NULL;
  70.             n++;
  71.       }

  72.       else
  73.       {
  74.             while (p->num > p1->num && p1->next != NULL)                                             
  75.             {
  76.                   p2 = p1;
  77.                   p1 = p1->next;
  78.             }
  79.             if (p->num >= p1->num && p1->next == NULL)             //如果要插入的节点为最后面
  80.             {
  81.                   p1->next = p;
  82.                   p->next = NULL;
  83.                   n++;
  84.             }                                                     //如果要插入的节点为最后面


  85.             else if (p->num <= p2->num && p2 == head)              //如果要插入的节点排序为第一个
  86.             {
  87.                   p->next = p1;
  88.                   head = p;
  89.                   n++;
  90.             }                                                     //如果要插入的节点排序为第一个

  91.       
  92.             else if (p->num >= p2->num && p->num <= p1->num)        //如果要插入的节点在链表中间                           
  93.             {
  94.                   p2->next = p;
  95.                   p->next = p1;
  96.                   n++;
  97.             }                                                     //如果要插入的节点在链表中间
  98.       }
  99.       p1 = head;

  100.       printf ("\ninput %ld number\n\n", n);
  101.       printf ("Num\t\t\tScore\n");

  102.       

  103.       if (head)
  104.       {
  105.             do
  106.             {
  107.                   printf ("%ld\t\t\t%3.2lf\n", p1->num, p1->score);                  
  108.             }
  109.             while (p1 = p1->next);
  110.       }


  111.       system("pause");
  112. }
复制代码
最后哦 不要骂我这代码乱  其实我也懒的优化了!
这个主要还是放在插入的部分上!

呵呵呵 热烈欢迎捶我 但别骂我!


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-9-19 12:53:48 | 显示全部楼层
好吧 沙发 自己占了
这个又加了一个循环插入的功能 但是 没有检查输入是否正确的问题:
代码:

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>

  4. struct student
  5. {
  6.       long int num;
  7.       long double score;
  8.       struct student *next;
  9. };

  10. void main()
  11. {
  12.       struct student *p1, *p2, *head, *p;
  13.       int n;
  14.       long int Temp_Num;
  15.       
  16.       n = 0;
  17.       head = NULL;
  18.       p1 = p2 = (struct student *)malloc(sizeof (struct student));
  19.       
  20.       printf ("input Num : ");
  21.       scanf ("%ld", &p1->num);
  22.       printf ("input Score : ");
  23.       scanf ("%lf", &p1->score);
  24.       
  25.       while (p1->num)
  26.       {
  27.             n++;

  28.             if (1 == n)
  29.             {
  30.                   head = p1;
  31.             }
  32.             else
  33.             {
  34.                   p2->next = p1;
  35.             }

  36.             p2 = p1;
  37.             p1 = (struct student *)malloc(sizeof (struct student));           //开辟新节点 使p1指向这个节点.
  38.             
  39.             printf ("input Num : ");                                          //读入一个num给p1所指的节点.
  40.             scanf ("%ld", &p1->num);
  41.             printf ("input Score : ");
  42.             scanf ("%lf", &p1->score);            
  43.       }
  44.       
  45.       p2->next = NULL;                                                        //p2->next = NULL
  46.       p = head;

  47.       printf ("\ninput %ld number\n\n", n);
  48.       printf ("Num\t\t\tScore\n");      

  49.       if (head)
  50.       {
  51.             do
  52.             {
  53.                   printf ("%ld\t\t\t%3.2lf\n", p->num, p->score);                  
  54.             }
  55.             while (p = p->next);
  56.       }
  57.       

  58.       
  59.             p1 = head;
  60.             p2 = head;
  61.             p = (struct student *)malloc(sizeof (struct student));
  62.             printf ("\n\nplease input insert Num: ");      //按照(num)数字前后顺序插入节点
  63.             scanf ("%ld", &p->num);
  64.             printf ("please input insert score: ");
  65.             scanf ("%lf", &p->score);

  66.       while (1)
  67.       {
  68.             if (head == NULL)                                     //如果是空链表的情况.
  69.             {
  70.                   head = p;
  71.                   head->next = NULL;
  72.                   n++;
  73.             }

  74.             else
  75.             {
  76.                   while (p->num > p1->num && p1->next != NULL)                                             
  77.                   {
  78.                         p2 = p1;
  79.                         p1 = p1->next;
  80.                   }
  81.                   if (p->num >= p1->num && p1->next == NULL)             //如果要插入的节点为最后面
  82.                   {
  83.                         p1->next = p;
  84.                         p->next = NULL;
  85.                         n++;
  86.                   }                                                     //如果要插入的节点为最后面


  87.                   else if (p->num <= p2->num && p2 == head)              //如果要插入的节点排序为第一个
  88.                   {
  89.                         p->next = p1;
  90.                         head = p;
  91.                         n++;
  92.                   }                                                     //如果要插入的节点排序为第一个

  93.       
  94.                   else if (p->num >= p2->num && p->num <= p1->num)        //如果要插入的节点在链表中间                           
  95.                   {
  96.                         p2->next = p;
  97.                         p->next = p1;
  98.                         n++;
  99.                   }                                                     //如果要插入的节点在链表中间
  100.             }
  101.             p1 = head;

  102.             printf ("\ninput %ld number\n\n", n);
  103.             printf ("Num\t\t\tScore\n");

  104.       

  105.             if (head)
  106.             {
  107.                   do
  108.                   {
  109.                         printf ("%ld\t\t\t%3.2lf\n", p1->num, p1->score);                  
  110.                   }
  111.                   while (p1 = p1->next);
  112.             }
  113.    
  114.       
  115.             //这里实现多次插入.

  116.             printf ("如果要再一次插入请直接输入数据\n\n否则要退出请按数字 0 : ");
  117.             p1 = head;
  118.             p2 = head;
  119.             p = (struct student *)malloc(sizeof (struct student));
  120.             scanf ("%ld", &p->num);
  121.             printf ("请输入相应的分数: ");
  122.             scanf ("%lf", &p->score);
  123.             if (0 == p->num)
  124.             {
  125.                   printf ("\n退出\n");
  126.                   break;
  127.             }
  128.       }

  129.       system("pause");
  130. }
复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-4-20 04:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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