#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;
}
//////////////////////////////////////////////////////////////////////////////////////
//2路归并排序。
Node *GetMidNode(Node* Start_Ptr)
{
Node *slow = Start_Ptr;
Node *fast = Start_Ptr;
if (Start_Ptr == NULL || Start_Ptr->next == NULL || Start_Ptr->next->next==NULL)return Start_Ptr;
while (fast != NULL)
{
if (fast->next == NULL)
{
break;
}
fast = fast->next->next;
slow = slow->next;
}
return slow;
}
void Merge()
{
Merge(first);
}
Node* Merge(Node *Start_Ptr)
{
if (Start_Ptr==NULL || Start_Ptr->next == NULL) return Start_Ptr;
else
{
Node *Mid_Ptr = GetMidNode(Start_Ptr);
Node *Next_Ptr = Mid_Ptr->next;
Mid_Ptr->next = NULL;
return Merage(Merge(Start_Ptr),Merge(Next_Ptr));
}
}
Node* Merage(Node *ptr1, Node *ptr2)
{
Node* p1 = ptr1;
Node *p2 = ptr2;
Node *p = new Node();
Node *save = p;
while (p1 != NULL && p2 != NULL)
{
if (p1->data > p2->data)
{
p->next=p2;
p = p2;
p2 = p2->next;
}
else
{
p->next = p1;
p = p1;
p1 = p1->next;
}
}
if (p1 == NULL)
{
p->next = p2;
}
if (p2 == NULL)
{
p->next = p1;
}
first = save->next;
delete save;
save = NULL;
return first;
}
//////////////////////////////////////////////////////////////////////////////////////////
//插入排序。
void Insert()
{
Node *ptr_first = first;
Node *pr_first;
Node *ptr_twe;
Node *pr_twe;
Node *save;
for (; ptr_first != NULL;pr_first = ptr_first)
{
save = ptr_first->next;
if (ptr_first == first)
{
ptr_twe = ptr_first;
ptr_twe->next = NULL;
first = ptr_twe;
}
else
{
Node *p = ptr_twe; Node *q = ptr_first;
while (p!=NULL&&p->data<q->data)
{
pr_twe = p;
p = p->next;
}
if (p == first)
{
//假设是第一个节点插入。
q->next = p;
ptr_twe = q;
first = ptr_twe; }
else if(p == NULL)
{
pr_twe->next = q;
q->next = NULL;
}
else
{
q->next = pr_twe->next;
pr_twe->next = q;
}
}
ptr_first = save;
}
}
private:
Node *first;
};
int main()
{
int a[] = { 6,7,2,1,0,7,5,23,4,5,423,4,100,-32,4,-3,1};
List list(a, sizeof(a) / sizeof(int));
//list.Merge();
//list.Bul();
list.Insert();
list.Printf();
return 0;
}

C++链表冒泡,归并,插入排序(纯指针)的更多相关文章

  1. 算法导论(第三版)Problems2(归并插入排序、数列逆序计算)

    讨论内容不说明,仅提供相应的程序. 2.1:归并插入排序θ(nlgn) void mergeInsertionSort(int a[], int l, int r, int k) { int m; & ...

  2. SDUT OJ 数据结构实验之链表四:有序链表的归并

    数据结构实验之链表四:有序链表的归并 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Desc ...

  3. SDUT-2119_数据结构实验之链表四:有序链表的归并

    数据结构实验之链表四:有序链表的归并 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 分别输入两个有序的整数序列(分别包 ...

  4. 浅谈归并排序:合并 K 个升序链表的归并解法

    在面试中遇到了这道题:如何实现多个升序链表的合并.这是 LeetCode 上的一道原题,题目具体如下: 用归并实现合并 K 个升序链表 LeetCode 23. 合并K个升序链表 给你一个链表数组,每 ...

  5. leetcode:Merge Two Sorted Lists(有序链表的归并)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  6. 环形链表II 142 使用快慢指针(C++实现)

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  7. 数据结构实验之链表四:有序链表的归并(SDUT 2119)

    #include <bits/stdc++.h> using namespace std; struct node { int data; struct node *next; }; st ...

  8. LeetCode--147.对链表进行插入排序

    题目描述: 插入排序的动画演示如上.从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示). 每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中. 插入排序算法 ...

  9. 通过数组初始化链表的两种方法:指向指针的引用node *&tail和指向指针的指针(二维指针)node **tail

    面试高频题:单链表的逆置操作/链表逆序相关文章 点击打开 void init_node(node *tail,char *init_array) 这样声明函数是不正确的,函数的原意是通过数组初始化链表 ...

随机推荐

  1. Java学习4_一些基础4_输入输出_16.5.7

    读取输入: 想从控制台进行输入,首先需要构造一个Scanner对象,并与“标准输入流”System.in关联. Scanner in=new Scanner(System.in); String na ...

  2. HDU_1227_Fast Food_动态规划

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1227 Fast Food Time Limit: 2000/1000 MS (Java/Others)   ...

  3. Windows下编译64位GSL

    GSL (GNU Scientific Library, http://www.gnu.org/software/gsl/)官方并没有提供编译好的Windows版本.首先要保证Windows是64位的 ...

  4. CAD如何直接打印,不出现打印对话框?

    主要用到函数说明: MxDrawXCustomFunction::Mx_Print 直接打印,不出现打印对话框,详细说明如下: 参数 说明 double ptLBx 打印的范围左下角x double ...

  5. 梦想MxWeb3D协同设计平台 2019.01.24更新

    SDK开发包下载地址:http://www.mxdraw.com/ndetail_10124.html1.  编写快速入门教程2.  重构前端代码,支持一个页面多个三维控件同时加载,或二维和三维同时加 ...

  6. Java必知必会的20种常用类库和API

    转载:https://blog.csdn.net/u011001084/article/details/79216958 个人感觉工具类对日常开发是很重要的,所以推荐一下这篇文章,虽然有的类库过时了 ...

  7. 14Oracle Database 高级事务,游标

    Oracle Database 高级事务,游标 隔离级别 脏读 不可重复读 虚读 读未提交 Read uncommitted 可以 可以 可以 读已提交 Read committed 不可以 可以 可 ...

  8. 100 道 Linux 笔试题,能拿 80 分就算大神!

    本套笔试题共100题,每题1分,共100分.(参考答案在文章末尾) 1. cron 后台常驻程序 (daemon) 用于: A. 负责文件在网络中的共享 B. 管理打印子系统C. 跟踪管理系统信息和错 ...

  9. enote笔记语言(3)(ver0.4)

    章节:enote笔记语言(3)     what&why(why not)&how&when&where&which:紫色,象征着神秘而又潜蕴着强大的力量,故取 ...

  10. libevent reference Mannual III--working with events

    FYI: http://www.wangafu.net/~nickm/libevent-book/TOC.html Working with events Libevent’s basic unit ...