鱼C论坛

 找回密码
 立即注册
查看: 2632|回复: 6

C++单链表问题

[复制链接]
发表于 2014-7-13 19:28:25 | 显示全部楼层 |阅读模式
1鱼币
最近在写单链表,但是运行老是出问题,希望大神可以写一个带head,tail,curr的单向链表,有insert功能就行了.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-7-13 19:28:26 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<string.h>
  4. #define LEN sizeof(struct student)
  5. struct student *create();//创建链表
  6. struct student *del(struct student *head, int num);//删除链表中数字 (链表头指针,要删除的学号)
  7. struct student *insert(struct student *head, struct student *head_2);//网链表中加元素(链表头指针,要加的那个的)
  8. void print(struct student *head);//输出链表
  9. struct student *clear(struct student *head);//清空链表
  10. void change(struct student *head, int num, float score);//更改链表数据

  11. struct student
  12. {
  13.         long int num;
  14.         float score;
  15.         struct student *next;
  16. };
  17. int n;
  18. char ch;
  19. int main()
  20. {
  21.         struct student *head, head_2;
  22.         int num, i;
  23.         float score;
  24.         printf("请先建立一个链表!(要结束请在number处输入0)\n");
  25.         head = create();
  26. s:        fflush(stdin);
  27.         printf("---------------------------------------------------------\n");
  28.         printf("请选择要的操作:\n");
  29.         printf("1.打印记录     ");
  30.         printf("2.插入记录     ");
  31.         printf("3.删除记录     \n");
  32.         printf("4.更改记录     ");
  33.         printf("5.清除记录     ");
  34.         printf("6.退出程序     \n");
  35.         printf("---------------------------------------------------------\n");
  36.         //scanf("%d", &i);
  37.         scanf_s("%d", &i);
  38.         switch (i)//根据用户输入的不同数字来执行不同的代码
  39.         {
  40.         case 1:
  41.                 print(head);
  42.                 goto s;
  43.                 break;
  44.         case 2:
  45.         s4 : fflush(stdin);
  46.                 printf("what number are you want to insert:");
  47.                 scanf_s("%d", &head_2.num);
  48.                 while ((ch = getchar()) != '\n')
  49.                 {
  50.                         if ((ch >= 48 && ch <= 57) || ch == 46)
  51.                         {
  52.                                 ;
  53.                         }
  54.                         else
  55.                         {
  56.                                 printf("你的输入有误!\n");
  57.                                 goto s4;
  58.                         }
  59.                 }
  60.         s5:                fflush(stdin);
  61.                 printf("what score are you want to insert:");
  62.                 scanf_s("%f", &head_2.score);
  63.                 while ((ch = getchar()) != '\n')
  64.                 {
  65.                         if ((ch >= 48 && ch <= 57) || ch == 46)
  66.                         {
  67.                                 ;
  68.                         }
  69.                         else
  70.                         {
  71.                                 printf("你的输入有误!\n");
  72.                                 goto s5;
  73.                         }
  74.                 }
  75.                 head = insert(head, &head_2);
  76.                 goto s;
  77.                 break;
  78.         case 3:
  79.         s6 : fflush(stdin);
  80.                 printf("please putinto the number you want to delete:");
  81.                 scanf_s("%ld", &num);
  82.                 while ((ch = getchar()) != '\n')
  83.                 {
  84.                         if ((ch >= 48 && ch <= 57) || ch == 46)
  85.                         {
  86.                                 ;
  87.                         }
  88.                         else
  89.                         {
  90.                                 printf("你的输入有误!\n");
  91.                                 goto s6;
  92.                         }
  93.                 }
  94.                 head = del(head, num);
  95.                 goto s;
  96.                 break;
  97.         case 4:
  98.         s7 : fflush(stdin);
  99.                 printf("Please putinto the number you want to change:");
  100.                 scanf_s("%d", &num);
  101.                 while ((ch = getchar()) != '\n')
  102.                 {
  103.                         if ((ch >= 48 && ch <= 57) || ch == 46)
  104.                         {
  105.                                 ;
  106.                         }
  107.                         else
  108.                         {
  109.                                 printf("你的输入有误!\n");
  110.                                 goto s7;
  111.                         }
  112.                 }
  113.         s8:        fflush(stdin);
  114.                 printf("Please putinto the score you want to save:");
  115.                 scanf_s("%f", &score);
  116.                 while ((ch = getchar()) != '\n')
  117.                 {
  118.                         if ((ch >= 48 && ch <= 57) || ch == 46)
  119.                         {
  120.                                 ;
  121.                         }
  122.                         else
  123.                         {
  124.                                 printf("你的输入有误!\n");
  125.                                 goto s8;
  126.                         }
  127.                 }
  128.                 change(head, num, score);
  129.                 break;
  130.         case 5:
  131.                 head = clear(head);
  132.                 break;
  133.         case 6:
  134.                 return 0;
  135.                 break;
  136.         default:
  137.                 printf("对不起,你的输入有误,请重新输入!");
  138.                 goto s;
  139.                 break;
  140.         }

  141.         goto s;
  142. }
  143. struct student *create()
  144. {
  145.         struct student *head;
  146.         struct student *p1, *p2;
  147. s:        fflush(stdin);
  148.         head = NULL;
  149.         p1 = p2 = (struct student *)malloc(LEN);
  150.         printf("Please purinto student's munber:");
  151.         scanf_s("%ld", &p1->num);
  152.         while ((ch = getchar()) != '\n')
  153.         {
  154.                 if ((ch >= 48 && ch <= 57) || ch == 46)
  155.                 {
  156.                         ;
  157.                 }
  158.                 else
  159.                 {
  160.                         printf("你的输入有误!\n");
  161.                         goto s;
  162.                 }
  163.         }
  164. s1:        printf("Please putinto student's score:");
  165.         fflush(stdin);
  166.         scanf_s("%f", &p1->score);
  167.         while ((ch = getchar()) != '\n')
  168.         {
  169.                 if ((ch >= 48 && ch <= 57) || ch == 46)
  170.                 {
  171.                         ;
  172.                 }
  173.                 else
  174.                 {
  175.                         printf("你的输入有误!\n");
  176.                         goto s1;
  177.                 }
  178.         }
  179.         n = 0;
  180.         while (p1->num)
  181.         {
  182.                 n++;
  183.                 if (n == 1)
  184.                 {
  185.                         head = p1;
  186.                 }
  187.                 else
  188.                 {
  189.                         p2->next = p1;
  190.                 }
  191.                 p2 = p1;
  192.                 p1 = (struct student *)malloc(LEN);
  193.         s2:        fflush(stdin);
  194.                 printf("Please purinto student's munber:");
  195.                 scanf_s("%ld", &p1->num);
  196.                 while ((ch = getchar()) != '\n')
  197.                 {
  198.                         if ((ch >= 48 && ch <= 57) || ch == 46)
  199.                         {
  200.                                 ;
  201.                         }
  202.                         else
  203.                         {
  204.                                 printf("你的输入有误!\n");
  205.                                 goto s2;
  206.                         }
  207.                 }
  208.                 if (p1->num == 0)
  209.                 {
  210.                         goto e;
  211.                 }
  212.         s3:        fflush(stdin);
  213.                 printf("Please putinto student's score:");
  214.                 scanf_s("%f", &p1->score);
  215.                 while ((ch = getchar()) != '\n')
  216.                 {
  217.                         if ((ch >= 48 && ch <= 57) || ch == 46)
  218.                         {
  219.                                 ;
  220.                         }
  221.                         else
  222.                         {
  223.                                 printf("你的输入有误!\n");
  224.                                 goto s3;
  225.                         }
  226.                 }
  227.         }
  228. e:        p2->next = NULL;
  229.         return head;
  230. }

  231. void print(struct student *head)
  232. {
  233.         struct student *p;
  234.         p = head;
  235.         printf("共有%d条记录:\n", n);
  236.         if (head)
  237.         {
  238.                 do
  239.                 {
  240.                         printf("%ld号学生的成绩是:%f\n", p->num, p->score);
  241.                         p = p->next;
  242.                 } while (p);
  243.         }
  244. }

  245. struct student *del(struct student *head, int num)
  246. {
  247.         struct student *p1, *p2;
  248.         p2 = head;
  249.         if (NULL == head)
  250.         {
  251.                 printf("The List is NULL!\n");
  252.                 goto END;
  253.         }


  254.         p1 = head;
  255.         while (p1->num != num&&p1->next != NULL)
  256.         {
  257.                 p2 = p1;
  258.                 p1 = p1->next;
  259.         }

  260.         if (num == p1->num)
  261.         {
  262.                 if (p1 == head)
  263.                 {
  264.                         head = p1->next;
  265.                         printf("Delete NO.%d succeed!\n", num);
  266.                         n--;
  267.                 }
  268.                 else
  269.                 {
  270.                         p2->next = p1->next;
  271.                         printf("Delete NO.%d succeed!\n", num);
  272.                         n--;
  273.                 }
  274.         }
  275.         else
  276.         {
  277.                 printf("NO.%d not been found!\n", num);
  278.         }
  279. END:
  280.         return head;
  281. }

  282. struct student *insert(struct student *head, struct student *head_2)
  283. {
  284.         struct student *p0, *p1, *p2;
  285.         p0 = head_2;
  286.         p1 = head;
  287.         p2 = head;
  288.         if (p1 == NULL)
  289.         {
  290.                 head = p0;
  291.                 p0->next = NULL;
  292.         }
  293.         else
  294.         {
  295.                 while (p0->num >p1->num && p1->next != NULL)
  296.                 {
  297.                         p2 = p1;
  298.                         p1 = p1->next;
  299.                 }
  300.                 if (p0->num <= p1->num)
  301.                 {
  302.                         if (p1 == head)
  303.                         {
  304.                                 head = p0;
  305.                         }
  306.                         else
  307.                         {
  308.                                 p2->next = p0;
  309.                         }
  310.                         p0->next = p1;
  311.                 }
  312.                 else
  313.                 {
  314.                         p1->next = p0;
  315.                         p0->next = NULL;
  316.                 }
  317.         }
  318.         n++;
  319. e:        return head;
  320. }

  321. struct student *clear(struct student *head)
  322. {
  323.         head = NULL;
  324.         n = 0;
  325.         printf("以清除全部记录!\n");
  326.         return head;
  327. }

  328. void change(struct student *head, int num, float score)
  329. {
  330.         struct student *p1, *p2;
  331.         if (head == NULL)
  332.         {
  333.                 printf("This is a NULL!\n");
  334.                 goto end_2;
  335.         }
  336.         p1 = head;
  337.         while (p1->num != num&&p1->next != NULL)
  338.         {
  339.                 p2 = p1;
  340.                 p1 = p1->next;
  341.         }
  342.         if (p1->num == num)
  343.         {
  344.                 p1->score = score;
  345.         }
  346.         else
  347.         {
  348.                 printf("NO.%d not been found!", num);
  349.         }
  350. end_2:
  351.         printf("");
  352. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-7-14 12:59:52 | 显示全部楼层
去年刚学的时候写的 运行没问题就是算法........
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-7-14 15:42:17 | 显示全部楼层
牡丹,你让我们情何以堪啊,只能敬佩!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-7-14 19:25:34 | 显示全部楼层
发个C++版本的
  1. #include <iostream>
  2. using namespace std;

  3. struct Node;
  4. class List {
  5. public:
  6.         typedef Node* Iterator;
  7.         List();
  8.         ~List();
  9.         Iterator begin()const{ return _first; }
  10.         Iterator end()const{ return 0; }
  11.         Iterator find( int number );
  12.         void insert( Iterator iter, int number );
  13. private:
  14.         Iterator _first;
  15. };

  16. void init( List& list );
  17. void print( const List& list );

  18. int main() {
  19.         List list;
  20.         init( list );
  21.         print( list );
  22.         List::Iterator it = list.find( 5 );
  23.         if( it ) list.insert( it, 100 );
  24.         print( list );
  25. }


  26. struct Node {
  27.         int _data;
  28.         Node* _next;
  29.         Node( int data, Node* next = 0 ):_data(data), _next(next){}
  30.         List::Iterator& next() { return  _next; }
  31. };

  32. List::List():_first(0) {}
  33. List::~List() {
  34.         while( _first ) {
  35.                 Iterator tmp = _first->next();
  36.                 delete _first;
  37.                 _first = tmp;
  38.         }
  39. }

  40. List::Iterator List::find( int number ) {
  41.         Iterator tmp = _first;
  42.         while( tmp && tmp->_data != number ) tmp = tmp->next();
  43.         return tmp;
  44. }

  45. void List::insert( List::Iterator iter, int number ) {
  46.         Iterator tmp = new Node( number, iter );
  47.         if( iter == _first ) { //在表头加
  48.                 _first = tmp;
  49.         }  else {        //不在表头加
  50.                 Iterator pos = _first;
  51.                 while( pos->next() != iter ) pos = pos->next();
  52.                 pos->next() = tmp;
  53.         }
  54. }

  55. void init( List& list ) {
  56.         for( int i = 10; i>0; --i ) list.insert( list.begin() , i );
  57. }

  58. void print( const List& list ) {
  59.         for( List::Iterator it = list.begin(); it != list.end(); it = it->next() )
  60.                 cout<<it->_data<<" ";
  61.         cout<<endl;
  62. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-7-15 11:48:36 | 显示全部楼层
有大神回复了我们感觉只有有copy了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-7-18 10:12:13 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-10 14:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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