首页 > 其他 > 详细

数据结构-链表快排

时间:2016-01-07 18:07:12      阅读:162      评论:0      收藏:0      [点我收藏+]
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<string>
 4 
 5 struct Node{
 6     int num;
 7     Node* next;
 8 };
 9 
10 void Add(Node **head, int num){
11     Node *node = new Node;
12     node->num = num;
13     node->next = *head;
14     (*head) = node;
15 }
16 
17 void QuickSort(Node *head, Node *tail){
18     if(head == tail)
19         return;
20     Node *temp = head;
21     for(Node *idx=head->next;idx!=tail;idx=idx->next){
22         if(head->num > idx->num){
23             temp=temp->next;
24             std::swap(temp->num, idx->num);
25         }
26     }
27     std::swap(head->num, temp->num);
28     QuickSort(head, temp);
29     QuickSort(temp->next, tail);
30 }
31 
32 void Print(Node *head){
33     Node *cur = head;
34     while (cur!=NULL){
35         printf("%d ", cur->num);
36         cur=cur->next;
37     }
38     printf("\n");
39 }
40 
41 int main(){
42     Node *head = NULL;
43     Add(&head, 1);
44     Add(&head, 2);
45     Add(&head, 3);
46     Add(&head, 4);
47     Add(&head, 5);
48     Add(&head, 6);
49     Add(&head, 7);
50     Add(&head, 8);
51     Print(head);
52     QuickSort(head, NULL);
53     Print(head);
54     return 0;
55 }

 

数据结构-链表快排

原文:http://www.cnblogs.com/bysen32/p/5110656.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!