--- list sorting algorithm and inversion ring Analyzing

1. reverse list

  Thought: forward traverse the list, each time a node removed, and then inserted in a head insert nodes manner, can reverse

 

2. Analyzing ring

  Idea: to define two pointers, then traverse the list, a pointer to a node time to go, time to go another two node pointer, if the list into the ring, then two pointers must have equal value cases; otherwise, the iteration completes normally.

 

  1 #include <iostream>
  2 
  3 using namespace std;
  4 
  5 struct Node
  6 {
  7     struct Node *next;
  8     int data;
  9 };
 10 
 11 Node *createNode(int x)
 12 {
 13     Node *p = NULL;
 14     
 15     p = new Node;
 16     p->next = NULL;
 17     p->data = x;
 18     
 19     return p;
 20 }
 21 
 22 void addNode(Node **head, Node *p)
 23 {
 24     Node *temp = NULL;
 25     if(*head == NULL)
 26     {
 27         *head = p;
 28     }
 29     else
 30     {
 31         temp = *head;
 32         while(temp->next != NULL)
 33         {
 34             temp = temp->next;
 35         }
 36         temp->next = p;
 37     }
 38 }
 39 
 40 void showList(Node *head)
 41 {
 42     while(head != NULL)
 43     {
 44         cout << head->data << "  ";
 45         head = head->next;
 46     }
 47     cout << endl;
 48 }
 49 
 50 void swap(int& a, int& b)
 51 {
 52     int tmp = b;
 53     b = a;
 54     a = tmp;
 55 }
 56 
 57 // 链表反转
 58 Node *reverse(Node *head)
 59 {
 60     Node *pCur = NULL;
 61     Node *pNext = NULL;
 62     
 63     if(head == NULL || head->next == NULL)
 64     {
 65         return head;
 66     }
 67     
 68     pCur = head->next;
 69     head->next = NULL;
 70     
 71     while(pCur != NULL)
 72     {
 73         pNext = pCur->next;
 74         pCur->next = head;
 75         head = pCur;
 76         pCur = pNext;
 77     }
 78     
 79     return head;
 80 }
 81 
 82 // 链表是否成环
 83 bool isCirCle(Node *head)
 84 {
 85     bool bIsCirCle = false;
 86     Node *pFast = NULL;
 87     Node *pSlow = NULL;
 88     
 89     if(head == NULL || head->next == NULL)
 90     {
 91         return bIsCirCle;
 92     }
 93     
 94     pFast = head;
 95     pSlow = head;
 96     while(pFast->next != NULL && pFast->next->next != NULL)
 97     {
 98         pFast = pFast->next->next;
 99         pSlow = pSlow->next;
100         
101         if(pFast == pSlow)
102         {
103             bIsCirCle = true;
104             break;
105         }
106     }
107     
108     return bIsCirCle;
109 }
110 
111 int main()
112 {
113     Node *head = NULL;
114     
115     addNode(&head, createNode(2));
116     addNode(&head, createNode(5));
117     addNode(&head, createNode(7));
118     addNode(&head, createNode(4));
119     addNode(&head, createNode(6));
120     addNode(&head, createNode(3));
121     addNode(&head, createNode(1));
122     addNode(&head, createNode(9));
123     addNode(&head, createNode(8));
124 
125     cout << "Sort Before:" << endl;
126     showList(head);
127     
128     //ListSort_1(&head);
129     //ListSort_2(&head);
130     ListSort_4(&head);
131     
132     cout << "Sort After:" << endl;
133     showList(head);
134     
135     cout << "revert:" << endl;
136     showList(reverse(head));
137     
138     cout << "list is circle: " << isCirCle(head) << endl;
139 
140     while(1);
141     return 0;
142 }

 

Guess you like

Origin www.cnblogs.com/chusiyong/p/11325107.html