链表操作

  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4. struct student
  5. {
  6.     int data;
  7.     struct student *next;
  8. };
  9. // The linked list has a head node which hasn't data,
  10. // and assume there is two nodes which have the equal data
  11. struct student *CreateList(int count)
  12. {
  13.     struct student *head = new struct student();
  14.     head->next = NULL;
  15.     struct student *pTemp=NULL;
  16.     struct student *pCurrent=NULL;
  17.     pCurrent = head;
  18.     for (int i = 0; i < count; i++)
  19.     {
  20.         pTemp = new struct student();
  21.         pTemp->data = i;
  22.         pCurrent->next = pTemp;
  23.         pCurrent = pCurrent->next;
  24.     }
  25.     pCurrent->next = NULL;
  26.     return head;
  27. }
  28. // The linked list has a head node which hasn't data
  29. struct student *CreateRandomList(int count)
  30.     struct student *head = new struct student();
  31.     head->next = NULL;
  32.     struct student *pTemp=NULL;
  33.     struct student *pCurrent=NULL;
  34.     pCurrent = head;
  35.     for (int i = 0; i < count; i++)
  36.     { 
  37.         pTemp = new struct student();
  38.         pTemp->data = rand();
  39.         pCurrent->next = pTemp;
  40.         pCurrent = pCurrent->next;
  41.     }
  42.     pCurrent->next = NULL;
  43.     return head;
  44. }
  45. void Print(struct student *head)
  46. {
  47.     struct student *pTemp = head->next;
  48.     while (pTemp != NULL)
  49.     {
  50.         cout<<pTemp->data<<'/t';
  51.         pTemp = pTemp->next;
  52.     }
  53.     cout<<endl;
  54. }
  55. // Delete the node which data = para(data)
  56. struct student *DelList(struct student *head, int data)
  57. {
  58.     struct student *pCurrent = head;
  59.     struct student *pTemp;
  60.     // Assume && calculate from left to right, 
  61.     // else there is error on pCurrent->next->data if pCurrent->next is NULL
  62.     while (pCurrent->next != NULL && pCurrent->next->data != data)
  63.     {
  64.         pCurrent = pCurrent->next;
  65.     }
  66.     if (!pCurrent->next)
  67.     {
  68.         cout<<"Not found!";
  69.     }
  70.     else
  71.     {
  72.         pTemp = pCurrent->next;
  73.         pCurrent->next = pTemp->next;
  74.         delete pTemp;
  75.         pTemp = NULL;
  76.     }
  77.     return head;
  78. }
  79. struct student *InsList(struct student *head, int data)
  80. {
  81.     struct student *pCurrent = head;
  82.     // Assume && calculate from left to right, 
  83.     // else there is error on pCurrent->next->data if pCurrent->next is NULL
  84.     while (pCurrent->next != NULL && pCurrent->next->data <= data)
  85.     {
  86.         pCurrent = pCurrent->next;
  87.     }
  88.     struct student *stu = new struct student();
  89.     stu->data = data;
  90.     stu->next = pCurrent->next;
  91.     pCurrent->next = stu;
  92.     return head;
  93. }
  94. struct student *RevList(struct student *head)
  95. {
  96.     struct student *pPrev;
  97.     struct student *pNext;
  98.     struct student *pCurrent;
  99.     pCurrent = head->next;
  100.     pPrev = NULL;
  101.     while (pCurrent->next != NULL)
  102.     {
  103.         pNext = pCurrent->next;
  104.         pCurrent->next = pPrev;
  105.         pPrev = pCurrent;
  106.         pCurrent = pNext;
  107.     }
  108.     pCurrent->next = pPrev;
  109.     head->next = pCurrent;
  110.     return head;
  111. }
  112. // Revert partial list
  113. // Ex: 0, 1, 2, 3, 4, offset = 3, and then output: 3, 2, 1, 0, 4
  114. struct student *RevPartList(struct student *head, struct student *offset)
  115. {
  116.     struct student *pPrev;
  117.     struct student *pNext;
  118.     struct student *pCurrent;
  119.     pCurrent = head->next;
  120.     pPrev = offset->next;
  121.     while (pCurrent != offset)
  122.     {
  123.         pNext = pCurrent->next;
  124.         pCurrent->next = pPrev;
  125.         pPrev = pCurrent;
  126.         pCurrent = pNext;
  127.     }
  128.     pCurrent->next = pPrev;
  129.     head->next = pCurrent;
  130.     return head;
  131. }
  132. struct student *SwapList(struct student *head, struct student *node1, struct student *node2)
  133. {
  134.     struct student *pPrev1;
  135.     struct student *pPrev2;
  136.     struct student *pNode1;
  137.     struct student *pNext2;
  138.     struct student *pCurrent = head;
  139.     int find = 0;
  140.     while (pCurrent->next != NULL)
  141.     {
  142.         // Ensure pPrev1 is before pPrev2
  143.         if ((pCurrent->next->data == node1->data) || (pCurrent->next->data == node2->data))
  144.         {
  145.             if (find == 0)
  146.             {
  147.                 pPrev1 = pCurrent;
  148.                 pNode1 = pCurrent->next;
  149.                 find++;
  150.             }
  151.             else
  152.             {
  153.                 pPrev2 = pCurrent;
  154.                 pNext2 = pCurrent->next->next;
  155.                 find++;
  156.             }
  157.         }
  158.         if (find == 2)
  159.         {
  160.             break;
  161.         }
  162.         pCurrent = pCurrent->next;
  163.     }
  164.     if (find != 2)
  165.     {
  166.         cout<<"The input data aren't right"<<endl;
  167.         return head;
  168.     }
  169.     if (pNode1 == pPrev2)
  170.     {
  171.         // 2 nodes adjoin and node1 before node2
  172.         // swap from the first
  173.         // Before: 0->1(pPrev1)->2(pNode1)->3->4(pNext2)
  174.         // After:  0->1(PPrev1)->3->2(PNode1)->4(pNext2)
  175.         pPrev1->next = pNode1->next;
  176.         pNode1->next->next = pNode1;
  177.         pNode1->next = pNext2;
  178.     }
  179.     else
  180.     {
  181.         // 2 nodes aren't adjoin
  182.         // swap from the first
  183.         // Before: 0->1(pPrev1)->2(pNode1)->3->4->5(pPrev2)->6->7(pNext2)->8
  184.         // After:  0->1(pPrev1)->6->3->4->5(pPrev2)->2(pNode1)->7(pNext2)->8
  185.         pPrev1->next = pPrev2->next;
  186.         pPrev2->next->next = pNode1->next;
  187.         pPrev2->next = pNode1;
  188.         pNode1->next = pNext2;
  189.     }
  190.     return head;
  191. }
  192. // para:    head->1->2->3->4->5->6->7->8->9
  193. // return:  head->2->1->4->3->6->5->8->7->9
  194. struct student *SwapOddAndEvenNodes(struct student *head)
  195. {
  196.     struct student *pPrev1;
  197.     struct student *pNode1;
  198.     struct student *pNext2;
  199.     struct student *pCurrent = head;
  200.     while (pCurrent->next->next != NULL)
  201.     {
  202.         // Set the points
  203.         pPrev1 = pCurrent;
  204.         pNode1 = pCurrent->next;
  205.         pNext2 = pCurrent->next->next->next;
  206.         // Swap odd and even nodes
  207.         pPrev1->next = pNode1->next;
  208.         pNode1->next->next = pNode1;
  209.         pNode1->next = pNext2;
  210.         // Reset the pCurrent
  211.         pCurrent = pCurrent->next->next;        
  212.     }
  213.     return head;
  214. }
  215. // Bubble sort
  216. void SortList(struct student *head)
  217. {
  218.     struct student *p;
  219.     struct student *pPrev;
  220.     struct student *pCur;
  221.     struct student *pNext;
  222.     // Because we can't get the total nodes,
  223.     // we will compare all nodes in one time sort
  224.     for (p = head->next; p; p = p->next)
  225.     {
  226.         bool exchanged = false;
  227.         for (pPrev = head, pCur = pPrev->next, pNext = pCur->next; pNext;)
  228.         {
  229.             if (pCur->data > pNext->data)
  230.             {
  231.                 pPrev->next = pNext;
  232.                 pCur->next = pNext->next;
  233.                 pNext->next = pCur;
  234.                 exchanged = true;
  235.             }
  236.             else
  237.             {
  238.                 pCur = pCur->next;
  239.             }
  240.             pPrev = pPrev->next;
  241.             pNext = pCur->next;
  242.         }
  243.         if (!exchanged)
  244.         {
  245.             return;
  246.         }
  247.     }
  248. }
  249. // Just combine two lists
  250. struct student *CombineList(struct student *head1, struct student *head2)
  251. {
  252.     struct student *p;
  253.     for(p = head1; p->next; p = p->next);
  254.     p->next = head2->next;
  255.     return head1;
  256. }
  257. // Good method
  258. // 1. sort the two list seperatly, and the time complexity: n(n-1)/2
  259. // 2. merge the two list, and the time complexity: n + n
  260. // so the time complexity totally: n(n-1)/2 *2 + n + n = n^2 + n
  261. // Bad mehtod
  262. // 1. merge the two list, and the time complexity: n + n
  263. // 2. sort the two list seperatly, and the time complexity: 2n(2n-1)/2
  264. // so the time complexity totally: 2n(2n-1)/2 *2 + n + n = 2n^2 + n
  265. // its time complexity is more than above method
  266. struct student *MergeList(struct student *head1, struct student *head2)
  267. {
  268.     // Use head1 as head
  269.     struct student *head = head1;
  270.     struct student *p = head;
  271.     // Skip the head which hasn't data
  272.     struct student *p1 = head1->next;
  273.     struct student *p2 = head2->next;
  274.     while (p1 && p2)
  275.     {
  276.         if (p1->data <= p2->data)
  277.         {
  278.             p->next = p1;
  279.             p = p1;
  280.             p1 = p1->next;
  281.         }
  282.         else
  283.         {
  284.             p->next = p2;
  285.             p = p2;
  286.             p2 = p2->next;
  287.         }
  288.     }
  289.     p->next = p1?p1:p2;
  290.     // Clean the head2
  291.     delete(head2);
  292.     return head;
  293. }
  294. void main()
  295. {
  296.     struct student *head;
  297.     struct student *head1;
  298.     struct student *head2;
  299.     struct student *stu;
  300.     struct student *pNode1;
  301.     struct student *pNode2;
  302.     int data;
  303.     head = CreateList(10);
  304.     Print(head);
  305.     char ch;
  306.     do
  307.     {
  308.         cout<<"Please input what will you do."<<endl;
  309.         cout<<"D for delete one node"<<endl;
  310.         cout<<"I for insert one node"<<endl;
  311.         cout<<"R for reverse the link"<<endl;
  312.         cout<<"P for partial reverse the link"<<endl;
  313.         cout<<"S for swip two nodes"<<endl;
  314.         cout<<"O for swip even and odd nodes"<<endl;
  315.         cout<<"M for merge two links"<<endl;
  316.         cout<<"Q for quit"<<endl;
  317.         cin>>ch;
  318.         switch(ch)
  319.         {
  320.         case 'D':
  321.         case 'd':
  322.             cout<<"Which one delete:";
  323.             cin>>data;
  324.             head = DelList(head,data);
  325.             cout<<"Print the current link"<<endl;
  326.             Print(head);
  327.             break;
  328.         case 'I':
  329.         case 'i':
  330.             cout<<"Please input insert node -- data:";
  331.             cin>>data;
  332.             head = InsList(head,data);
  333.             cout<<"Print the current link"<<endl;
  334.             Print(head);
  335.             break;
  336.         case 'R':
  337.         case 'r':
  338.             cout<<"Reverse the link"<<endl;
  339.             head = RevList(head);
  340.             Print(head);
  341.             break;
  342.         case 'P'
  343.         case 'p':
  344.             cout<<"Please input the revert node offset:";
  345.             cin>>data;
  346.             stu = head->next;
  347.             for (int i=0; i<data; i++)
  348.             {
  349.                 stu = stu->next;
  350.             }
  351.             head = RevPartList(head, stu);
  352.             Print(head);
  353.             break;
  354.         case 'S':
  355.         case 's':
  356.             cout<<"Swap 2 node. Please input 2 node -- data:"<<endl;
  357.             pNode1 = new struct student();
  358.             pNode2 = new struct student();
  359.             cin>>pNode1->data;
  360.             cin>>pNode2->data;
  361.             head = SwapList(head, pNode1, pNode2);
  362.             cout<<"Print the current link"<<endl;
  363.             Print(head);
  364.             break;
  365.         case 'O':
  366.         case 'o':
  367.             cout<<"Swap odd and even nodes"<<endl;
  368.             head = SwapOddAndEvenNodes(head);
  369.             cout<<"Print the current link"<<endl;
  370.             Print(head);
  371.             break;
  372.         case 'M':
  373.         case 'm':
  374.             head1 = CreateRandomList(5);
  375.             Print(head1);
  376.             head2 = CreateRandomList(5);
  377.             Print(head2);
  378.             cout<<"Sort the list head1"<<endl;
  379.             SortList(head1);
  380.             Print(head1);
  381.             cout<<"Sort the list head2"<<endl;
  382.             SortList(head2);
  383.             Print(head2);
  384.             cout<<"Merge two list with sorted sequence"<<endl;
  385.             Print(MergeList(head1, head2));
  386.             break;
  387.         case 'Q':
  388.         case 'q':
  389.             break;
  390.         default:
  391.             cout<<"Error input! Please input again:";
  392.         }
  393.     }while(ch != 'Q' && ch != 'q');
  394. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值