- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- struct student
- {
- int data;
- struct student *next;
- };
- // The linked list has a head node which hasn't data,
- // and assume there is two nodes which have the equal data
- struct student *CreateList(int count)
- {
- struct student *head = new struct student();
- head->next = NULL;
- struct student *pTemp=NULL;
- struct student *pCurrent=NULL;
- pCurrent = head;
- for (int i = 0; i < count; i++)
- {
- pTemp = new struct student();
- pTemp->data = i;
- pCurrent->next = pTemp;
- pCurrent = pCurrent->next;
- }
- pCurrent->next = NULL;
- return head;
- }
- // The linked list has a head node which hasn't data
- struct student *CreateRandomList(int count)
- {
- struct student *head = new struct student();
- head->next = NULL;
- struct student *pTemp=NULL;
- struct student *pCurrent=NULL;
- pCurrent = head;
- for (int i = 0; i < count; i++)
- {
- pTemp = new struct student();
- pTemp->data = rand();
- pCurrent->next = pTemp;
- pCurrent = pCurrent->next;
- }
- pCurrent->next = NULL;
- return head;
- }
- void Print(struct student *head)
- {
- struct student *pTemp = head->next;
- while (pTemp != NULL)
- {
- cout<<pTemp->data<<'/t';
- pTemp = pTemp->next;
- }
- cout<<endl;
- }
- // Delete the node which data = para(data)
- struct student *DelList(struct student *head, int data)
- {
- struct student *pCurrent = head;
- struct student *pTemp;
- // Assume && calculate from left to right,
- // else there is error on pCurrent->next->data if pCurrent->next is NULL
- while (pCurrent->next != NULL && pCurrent->next->data != data)
- {
- pCurrent = pCurrent->next;
- }
- if (!pCurrent->next)
- {
- cout<<"Not found!";
- }
- else
- {
- pTemp = pCurrent->next;
- pCurrent->next = pTemp->next;
- delete pTemp;
- pTemp = NULL;
- }
- return head;
- }
- struct student *InsList(struct student *head, int data)
- {
- struct student *pCurrent = head;
- // Assume && calculate from left to right,
- // else there is error on pCurrent->next->data if pCurrent->next is NULL
- while (pCurrent->next != NULL && pCurrent->next->data <= data)
- {
- pCurrent = pCurrent->next;
- }
- struct student *stu = new struct student();
- stu->data = data;
- stu->next = pCurrent->next;
- pCurrent->next = stu;
- return head;
- }
- struct student *RevList(struct student *head)
- {
- struct student *pPrev;
- struct student *pNext;
- struct student *pCurrent;
- pCurrent = head->next;
- pPrev = NULL;
- while (pCurrent->next != NULL)
- {
- pNext = pCurrent->next;
- pCurrent->next = pPrev;
- pPrev = pCurrent;
- pCurrent = pNext;
- }
- pCurrent->next = pPrev;
- head->next = pCurrent;
- return head;
- }
- // Revert partial list
- // Ex: 0, 1, 2, 3, 4, offset = 3, and then output: 3, 2, 1, 0, 4
- struct student *RevPartList(struct student *head, struct student *offset)
- {
- struct student *pPrev;
- struct student *pNext;
- struct student *pCurrent;
- pCurrent = head->next;
- pPrev = offset->next;
- while (pCurrent != offset)
- {
- pNext = pCurrent->next;
- pCurrent->next = pPrev;
- pPrev = pCurrent;
- pCurrent = pNext;
- }
- pCurrent->next = pPrev;
- head->next = pCurrent;
- return head;
- }
- struct student *SwapList(struct student *head, struct student *node1, struct student *node2)
- {
- struct student *pPrev1;
- struct student *pPrev2;
- struct student *pNode1;
- struct student *pNext2;
- struct student *pCurrent = head;
- int find = 0;
- while (pCurrent->next != NULL)
- {
- // Ensure pPrev1 is before pPrev2
- if ((pCurrent->next->data == node1->data) || (pCurrent->next->data == node2->data))
- {
- if (find == 0)
- {
- pPrev1 = pCurrent;
- pNode1 = pCurrent->next;
- find++;
- }
- else
- {
- pPrev2 = pCurrent;
- pNext2 = pCurrent->next->next;
- find++;
- }
- }
- if (find == 2)
- {
- break;
- }
- pCurrent = pCurrent->next;
- }
- if (find != 2)
- {
- cout<<"The input data aren't right"<<endl;
- return head;
- }
- if (pNode1 == pPrev2)
- {
- // 2 nodes adjoin and node1 before node2
- // swap from the first
- // Before: 0->1(pPrev1)->2(pNode1)->3->4(pNext2)
- // After: 0->1(PPrev1)->3->2(PNode1)->4(pNext2)
- pPrev1->next = pNode1->next;
- pNode1->next->next = pNode1;
- pNode1->next = pNext2;
- }
- else
- {
- // 2 nodes aren't adjoin
- // swap from the first
- // Before: 0->1(pPrev1)->2(pNode1)->3->4->5(pPrev2)->6->7(pNext2)->8
- // After: 0->1(pPrev1)->6->3->4->5(pPrev2)->2(pNode1)->7(pNext2)->8
- pPrev1->next = pPrev2->next;
- pPrev2->next->next = pNode1->next;
- pPrev2->next = pNode1;
- pNode1->next = pNext2;
- }
- return head;
- }
- // para: head->1->2->3->4->5->6->7->8->9
- // return: head->2->1->4->3->6->5->8->7->9
- struct student *SwapOddAndEvenNodes(struct student *head)
- {
- struct student *pPrev1;
- struct student *pNode1;
- struct student *pNext2;
- struct student *pCurrent = head;
- while (pCurrent->next->next != NULL)
- {
- // Set the points
- pPrev1 = pCurrent;
- pNode1 = pCurrent->next;
- pNext2 = pCurrent->next->next->next;
- // Swap odd and even nodes
- pPrev1->next = pNode1->next;
- pNode1->next->next = pNode1;
- pNode1->next = pNext2;
- // Reset the pCurrent
- pCurrent = pCurrent->next->next;
- }
- return head;
- }
- // Bubble sort
- void SortList(struct student *head)
- {
- struct student *p;
- struct student *pPrev;
- struct student *pCur;
- struct student *pNext;
- // Because we can't get the total nodes,
- // we will compare all nodes in one time sort
- for (p = head->next; p; p = p->next)
- {
- bool exchanged = false;
- for (pPrev = head, pCur = pPrev->next, pNext = pCur->next; pNext;)
- {
- if (pCur->data > pNext->data)
- {
- pPrev->next = pNext;
- pCur->next = pNext->next;
- pNext->next = pCur;
- exchanged = true;
- }
- else
- {
- pCur = pCur->next;
- }
- pPrev = pPrev->next;
- pNext = pCur->next;
- }
- if (!exchanged)
- {
- return;
- }
- }
- }
- // Just combine two lists
- struct student *CombineList(struct student *head1, struct student *head2)
- {
- struct student *p;
- for(p = head1; p->next; p = p->next);
- p->next = head2->next;
- return head1;
- }
- // Good method
- // 1. sort the two list seperatly, and the time complexity: n(n-1)/2
- // 2. merge the two list, and the time complexity: n + n
- // so the time complexity totally: n(n-1)/2 *2 + n + n = n^2 + n
- // Bad mehtod
- // 1. merge the two list, and the time complexity: n + n
- // 2. sort the two list seperatly, and the time complexity: 2n(2n-1)/2
- // so the time complexity totally: 2n(2n-1)/2 *2 + n + n = 2n^2 + n
- // its time complexity is more than above method
- struct student *MergeList(struct student *head1, struct student *head2)
- {
- // Use head1 as head
- struct student *head = head1;
- struct student *p = head;
- // Skip the head which hasn't data
- struct student *p1 = head1->next;
- struct student *p2 = head2->next;
- while (p1 && p2)
- {
- if (p1->data <= p2->data)
- {
- p->next = p1;
- p = p1;
- p1 = p1->next;
- }
- else
- {
- p->next = p2;
- p = p2;
- p2 = p2->next;
- }
- }
- p->next = p1?p1:p2;
- // Clean the head2
- delete(head2);
- return head;
- }
- void main()
- {
- struct student *head;
- struct student *head1;
- struct student *head2;
- struct student *stu;
- struct student *pNode1;
- struct student *pNode2;
- int data;
- head = CreateList(10);
- Print(head);
- char ch;
- do
- {
- cout<<"Please input what will you do."<<endl;
- cout<<"D for delete one node"<<endl;
- cout<<"I for insert one node"<<endl;
- cout<<"R for reverse the link"<<endl;
- cout<<"P for partial reverse the link"<<endl;
- cout<<"S for swip two nodes"<<endl;
- cout<<"O for swip even and odd nodes"<<endl;
- cout<<"M for merge two links"<<endl;
- cout<<"Q for quit"<<endl;
- cin>>ch;
- switch(ch)
- {
- case 'D':
- case 'd':
- cout<<"Which one delete:";
- cin>>data;
- head = DelList(head,data);
- cout<<"Print the current link"<<endl;
- Print(head);
- break;
- case 'I':
- case 'i':
- cout<<"Please input insert node -- data:";
- cin>>data;
- head = InsList(head,data);
- cout<<"Print the current link"<<endl;
- Print(head);
- break;
- case 'R':
- case 'r':
- cout<<"Reverse the link"<<endl;
- head = RevList(head);
- Print(head);
- break;
- case 'P':
- case 'p':
- cout<<"Please input the revert node offset:";
- cin>>data;
- stu = head->next;
- for (int i=0; i<data; i++)
- {
- stu = stu->next;
- }
- head = RevPartList(head, stu);
- Print(head);
- break;
- case 'S':
- case 's':
- cout<<"Swap 2 node. Please input 2 node -- data:"<<endl;
- pNode1 = new struct student();
- pNode2 = new struct student();
- cin>>pNode1->data;
- cin>>pNode2->data;
- head = SwapList(head, pNode1, pNode2);
- cout<<"Print the current link"<<endl;
- Print(head);
- break;
- case 'O':
- case 'o':
- cout<<"Swap odd and even nodes"<<endl;
- head = SwapOddAndEvenNodes(head);
- cout<<"Print the current link"<<endl;
- Print(head);
- break;
- case 'M':
- case 'm':
- head1 = CreateRandomList(5);
- Print(head1);
- head2 = CreateRandomList(5);
- Print(head2);
- cout<<"Sort the list head1"<<endl;
- SortList(head1);
- Print(head1);
- cout<<"Sort the list head2"<<endl;
- SortList(head2);
- Print(head2);
- cout<<"Merge two list with sorted sequence"<<endl;
- Print(MergeList(head1, head2));
- break;
- case 'Q':
- case 'q':
- break;
- default:
- cout<<"Error input! Please input again:";
- }
- }while(ch != 'Q' && ch != 'q');
- }
链表操作
最新推荐文章于 2006-02-14 16:36:00 发布