#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. 【OpenCV】像素操作的数字图像处理

    之前几天捣鼓matlab,用来处理数字图像,矩阵操作什么的,如果忘记线性代数就真的GG了. 在用了matlab被深深地吐槽之后,决定改用opencv,C++貌似也是处理数字图像的很好的工具 1. 在u ...

  2. 07Oracle Database 数据表

    Oracle Database 数据表 DDL 数据定义语言 -  建立数据库对象 create /alter/ drop/ truncate 创建表 Create table table_name( ...

  3. pl/sql编程语言

    –pl/sql编程语言–pl/sql编程语言是对sql语言的扩展,是的sql语言具有过程化编程的特性–pl/sql编程语言比一般的过程化编程语言,更加灵活高效–pl/sql编程语言主要用来编写存储过程 ...

  4. org.springframework.data.repository.config.RepositoryConfigurationSource.getAttribute(Ljava/lang/String;)Ljava/util/Optional;

    升级springboot到2.0时,碰到了一大堆问题,上面异常原因是jar版本冲突了,有的模块忘记更新版本了,统一一下版本就可以了

  5. 前端安全 xss

    整体的 XSS 防范是非常复杂和繁琐的,不仅需要在全部需要转义的位置,对数据进行对应的转义.而且要防止多余和错误的转义,避免正常的用户输入出现乱码. 虽然很难通过技术手段完全避免 XSS,但可以总结以 ...

  6. 洛谷——P3946 ことりのおやつ(小鸟的点心)

    P3946 ことりのおやつ(小鸟的点心) 题目太长,请去链接里看吧 注意细节:特判终点(即使困住又能怎样,到达就好了),特判高度 #include<bits/stdc++.h> #defi ...

  7. TestNG忽略测试

    用@Test(enabled = false) 声明需要被忽略执行的测试方法 package com.janson; import org.testng.annotations.Test; publi ...

  8. linux环境下时间的查看和修改

    查看日期和时间date 查看时区date -R 查看UTC时间date -u 修改日期[root@centos ~]# date -s 20181230Sun Dec 30 00:00:00 EST ...

  9. vticker.js--垂直滚动插件

    一.使用要求 列表必须是ul>li的格式 html代码 <div class=" myvticker'"> <ul> <li>1.新闻标题 ...

  10. java中通用权限管理设计(转)

    原文地址:http://www.cnblogs.com/a7345678/archive/2008/09/25/1298838.html 转自博客园暗夜精灵-鬼才阁 实现业务系统中的用户权限管理 B/ ...