鱼C论坛

 找回密码
 立即注册
查看: 4709|回复: 4

[萌新报道] 链表的创建、打印、插入和删除

[复制链接]
发表于 2016-6-21 10:33:09 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 ~风介~ 于 2016-6-21 19:55 编辑
  1. #include"stdafx.h"
  2. #include"malloc.h"
  3. #include "iostream"
  4. using namespace std;

  5. #define LEN sizeof(struct student)

  6. struct student *creat();       //创建链表
  7. void print(struct student *head);                 //打印链表
  8. struct student *del(struct student *head);   //删除链表
  9. struct student *insert(struct student *head, struct student *stu_2);    //插入链表

  10. int n;      //记录学生人数

  11. struct student
  12. {
  13.         int num;
  14.         float score;
  15.         struct student *next;
  16. };

  17. void main()                      //主函数
  18. {
  19.         struct student *stu, *p, *stu_2;

  20.         stu_2 = NULL;

  21.         stu = creat();
  22.         p = stu;
  23.         print(p);
  24.         print(insert(p, stu_2));
  25.         print(del(p));
  26.         /*
  27.         print(creat());

  28.         print(del(creat()));*/      //这样写之所以不对,是因为相当于由重新执行了一次creat(),所以应先把creat()赋给一个指针变量。

  29.         cout << "\n\n";
  30.         system("PAUSE");

  31. }

  32. /********************************创建链表**************************************/
  33. struct student *creat()
  34. {
  35.         struct student *p1, *p2;
  36.         struct student *head;
  37.         p1 = p2 = (struct student *)malloc(LEN);

  38.         cout << "the number of studnt is:";
  39.         cin >> p1->num;
  40.         cout << "the score is:";
  41.         cin >> p1->score;
  42.        
  43.         head = NULL;
  44.         n = 0;

  45.         while (p1->num)
  46.         {
  47.                 n++;
  48.                 if (1 == n)
  49.                 {
  50.                         head = p1;        //链头
  51.                 }
  52.                 else
  53.                 {
  54.                         p2->next = p1;
  55.                 }
  56.                 p2 = p1;

  57.                 p1 = (struct student *)malloc(LEN);//重新给p1开辟内存空间

  58.                 cout << "the number of student is:";
  59.                 cin >> p1->num;
  60.                 cout << "the score is:";
  61.                 cin >> p1->score;
  62.         }
  63.         p2->next = NULL;
  64.         return head;
  65. }

  66. /******************************打印数据**********************************/
  67. void print(struct student *head)
  68. {
  69.         struct student *p;
  70.         p = head;
  71.         cout << "There are " << n << " records" << endl;
  72.         /*if (NULL != head)
  73.         {
  74.                 do
  75.                 {
  76.                         cout << "The score of " << p->num << " is " << p->score << endl;
  77.                         p = p->next;                                //此处需要注意p指向下一个地址
  78.                 } while (NULL != p);
  79.         }*/
  80.         for(;p!=NULL;p=p->next)
  81.                 cout << "The score of " << p->num << " is " << p->score << endl;
  82. }

  83. /****************************************删除数据**************************************/
  84. struct student *del(struct student *head)
  85. {
  86.         struct student *p1, *p2;

  87.         int num;

  88.         p1 = p2 = head;

  89.         cout << "please inpute the delete num: ";
  90.         cin >> num;
  91.         if (head==NULL)
  92.         {
  93.                 cout << "\nThere is no records!" << endl;
  94.                 goto end;
  95.         }
  96.        
  97.         p1 = head;
  98.         while (p1->num != num && p1->next != NULL)
  99.         {
  100.                 p2 = p1;
  101.                 p1 = p1->next;
  102.         }
  103.         if (num == p1->num)
  104.         {
  105.                 if (p1 == head)
  106.                 {
  107.                         head = p1->next;
  108.                 }
  109.                 else
  110.                 {
  111.                         p2->next = p1->next;
  112.                 }

  113.                 cout << "Delete No: " << num << " succeed!" << endl;
  114.                 n = n - 1;
  115.         }
  116.         else
  117.         {
  118.                 cout<<num<<" not been found!\n";
  119.         }
  120.         end:
  121.         return head;
  122. }

  123. /************************************插入数据*******************************************/

  124. struct student *insert(struct student *head, struct student *stu_2)
  125. {
  126.         struct student *p1, *p2;
  127.         stu_2 = (struct student *)malloc(LEN);

  128.         /*为插入的数据赋值*/
  129.         cout << "The number of insert student is: " ;
  130.         cin >> stu_2->num ;
  131.         cout << "The score of insert student is: ";
  132.         cin >> stu_2->score;

  133.         p1 = p2 = head;
  134.         if (head == NULL)
  135.         {
  136.                 stu_2 = head;
  137.                 stu_2->next = NULL;
  138.         }
  139.         else
  140.         {
  141.                 while (p1->next != NULL&&stu_2->num > p1->num)
  142.                 {
  143.                         p2 = p1;
  144.                         p1 = p1->next;
  145.                 }
  146.                 if (stu_2->num <= p1->num)
  147.                 {
  148.                         if (head == p1)
  149.                         {
  150.                                 head = stu_2;
  151.                                 stu_2->next = p1;
  152.                         }
  153.                         else
  154.                         {
  155.                                 p2->next = stu_2;
  156.                                 stu_2->next = p1;
  157.                         }
  158.                 }
  159.                 else
  160.                 {
  161.                         p1->next = stu_2;
  162.                         stu_2->next = NULL;
  163.                 }
  164.         }
  165.         n++;
  166.         cout << "The insert num is: " << stu_2->num << endl;
  167.         return head;
  168. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-6-21 19:57:27 | 显示全部楼层
这是我写的,可以交流下哦!
http://bbs.fishc.com/thread-46760-1-1.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-6-22 01:18:40 | 显示全部楼层
好好好好
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-6-22 01:19:11 | 显示全部楼层
好好好
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-6-22 10:37:19 | 显示全部楼层
新人路过
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-16 04:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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