C++链表冒泡排序
来源:互联网 发布:刺客信条4低配优化 编辑:程序博客网 时间:2024/06/06 01:49
#include <iostream>using namespace std;//别问我为什么要写链表的冒泡排序。struct Node{ int data; Node *next; Node(int d = int()) :data(d), next(NULL){}};class List{public: List(int a[], int n) { first = NULL; for (int i = 0; i < n; i++) { if (first == NULL) { first = new Node(a[i]); } else { Node *s = new Node(a[i]); Node *p = first; while (p->next != NULL) { p = p->next; } s->next = p->next; p->next = s; } } } //冒泡排序 void Bul() { Node *ptr_one = first; Node *ptr_twe; Node *pr_one = NULL; Node *save = NULL; if (ptr_one == NULL || ptr_one->next == NULL)return; while (ptr_one != NULL && ptr_one->next != NULL) { Node *pr_twe = ptr_one; Node *m2 = NULL; save = ptr_one; for (ptr_twe = ptr_one->next; ptr_twe != NULL;) { if (ptr_one->data > ptr_twe->data) { if (pr_one == NULL) { Node *m1; Node *a; m1 = ptr_twe->next; m2 = ptr_one->next; first->next = m1; pr_twe->next = first; a = first; ptr_twe->next = m2; first = ptr_twe; save = ptr_twe; ptr_twe = a; ptr_one = first; } else { Swap(pr_one, pr_twe); save = pr_one->next; ptr_one = save; ptr_twe = pr_twe->next; } } pr_twe = ptr_twe; if (ptr_twe == NULL)continue; ptr_twe = ptr_twe->next; } pr_one = save; ptr_one = pr_one->next; } } void Swap(Node *prv1, Node *&prv2) { if (prv1->next == prv2) { Node *m = prv1->next; Node *n = prv2->next; m->next = n->next; n->next = m; prv1->next = n; prv2 = n; } else { Node *m1 = prv1->next; Node *save = m1->next; Node *m2 = prv2->next; m1->next = m2->next; prv2->next = m1; m2->next = save; prv1->next = m2; } } void Printf() { Node *p = first; while (p != NULL) { cout << p->data << " "; p = p->next; } cout << endl; }private: Node *first;};int main(){ int a[] = { 3, 10, 13, 102, 3, 5, 6, 1, 3, 4, 6, 7, 9, 1000, 3, 2, 4, 6, 66, 4, 3, 32, 454, 4, 99 }; List list(a, sizeof(a) / sizeof(int)); list.Bul(); list.Printf(); return 0;}
1 0
- 链表冒泡排序
- 链表冒泡排序
- 【C++】对双向链表进行冒泡排序
- C语言链表实现冒泡法排序
- C#:冒泡排序
- 冒泡排序 C数据结构
- 冒泡排序 C源码
- 冒泡排序(C#)
- 冒泡排序实例(C#)
- 【C#】[算法]冒泡排序
- C#--冒泡排序
- C语言冒泡排序
- 冒泡排序(C语言)
- C语言--冒泡排序
- 冒泡排序(C++)
- C语言-冒泡排序
- C语言冒泡排序
- 冒泡排序体会c
- 块匹配算法--全搜索
- 算法导论 第十一章:哈希表
- 黑马程序员-OC语言基础:面向对象语法 一
- 基于Halocn的图像拼接--龟兔赛跑
- Jni 简单实战 c调用java静态方法
- C++链表冒泡排序
- day01---Java基础语法--计算机知识点
- myeclipse快捷键使用
- Linux下安装mysql全过程,包括删除。
- 黑马程序员-OC语言基础:面向对象语法 二
- unity 获取某个文件夹下的所有图片
- linux c sscanf函数
- Http协议原理(一)
- java中生成javadoc文档