C/C++教程

C++ 链表 冒泡排序

本文主要是介绍C++ 链表 冒泡排序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

定义链表结构,定义构造函数,链表内next为空时,表示为最后一个数据。

#include <iostream>
using namespace std;
/// <summary>
/// 链表结构
/// </summary>
struct ListNode
{
    int value;
    ListNode* next;
    //构造函数
    ListNode(int v, ListNode* n = NULL)
    {
        value = v;
        next = n;
    }
};

输出链表数据,链表指针后移,直到链表指针为NULL

/// <summary>
/// 输出链表数据
/// </summary>
/// <param name="ln"></param>
void Print(ListNode* ln)
{
    while (ln != NULL)
    {
        cout << ln->value << endl;
        ln = ln->next;
    }
}

获取链表长度

/// <summary>
/// 获取单链表长度
/// </summary>
/// <returns></returns>
int GetLength(ListNode* head)
{
    int count = 0;
    while (head!=NULL)
    {
        count++;
        head = head->next;
    }
    return count;
}

判断单链表是否为空

/// <summary>
/// 判断单链表是否为空
/// </summary>
/// <param name="head"></param>
/// <returns></returns>
bool IsEmpty(ListNode* head)
{
    if (head->next==NULL)
    {
        return true;
    }
    return false;
}

查找节点

/// <summary>
/// 查找节点
/// </summary>
/// <param name="head"></param>
/// <param name="data"></param>
/// <returns></returns>
ListNode* Find(ListNode* head, int data)
{
    if (head == NULL)
    {
        return NULL;
    }
    else
    {
        while (head!=NULL)
        {
            if (head->value==data)
            {
                return head;
            }
            head = head->next;
        }
        return NULL;
    }
}
/// <summary>
/// 插入节点,判断原链表是否是空链表,如果是,将head指向新增节点
/// 如果不是空链表,向链尾插入新节点
/// </summary>
/// <param name="head"></param>
/// <param name="data"></param>
/// <returns></returns>
void InsertNode(struct ListNode* head, ListNode* newNode)
{
    ListNode* p = head;
    if (p==NULL)
    {
        p = newNode;
    }
    else
    {
        while (p->next!=NULL)
        {
            p = p->next;
        }
        p->next = newNode;
    }
}
/// <summary>
/// 在制定位置插入数据
/// </summary>
/// <param name="head"></param>
/// <param name="newNode"></param>
/// <param name="num"></param>
void InsertNode(ListNode* head, ListNode* newNode, int num)
{
    if (num==0)
    {
       int i = head->value;
        head->value = newNode->value;
        newNode->value = i;

        ListNode* walk = head->next;
        
        head->next = newNode;
        newNode->next = walk;
    }
    else if (num<=GetLength(head))
    {
        ListNode* p = head;
        int i = 1;
        while (num>i)
        {
            p = p->next;
            i++;
        }
        newNode->next = p->next;
        p->next = newNode;
    }
    else
    {
        cout << "索引超出!!!" << endl;
    }
}
/// <summary>
/// 在尾部删除元素
/// </summary>
/// <param name="head"></param>
void Delete(ListNode* head)
{
    ListNode* p = head;
    ListNode* temp = NULL;
    if (head==NULL||head->next==NULL)
    {
        return;
    }
    else
    {
        while (p->next!=NULL)
        {
            temp = p;
            p = p->next;
        }
        delete p;
        p = NULL;
        temp->next = NULL;
    }
}
/// <summary>
/// 删除所有元素
/// </summary>
/// <param name="head"></param>
void DeleteAll(ListNode* head)
{
    ListNode* p = head->next;
    ListNode* temp = NULL;
    while (p!=NULL)
    {
        temp = p;
        p = p->next;
        temp->next = NULL;
        delete temp;
    }
    head->next = NULL;
    head->value = NULL;
}
/// <summary>
/// 线性表排序,冒泡排序,直接遍历链表
/// </summary>
/// <param name="head"></param>
void ListSort(ListNode* head)
{
    ListNode* first = head;
    int num = GetLength(head);
    for (int i = 0; i < num-1; i++)
    {
        for (int j = 0; j < num-i-1; j++)
        {
            if (first->value > first->next->value)
            {
                int temp = first->value;
                first->value = first->next->value;
                first->next->value = temp;
            }
            first = first->next;
        }
        first = head;
    }
}
int main()
{
    ListNode* three = new ListNode(3);
    ListNode* second = new ListNode(1, three);
    ListNode* head = new ListNode(2, second);
    ListNode* four = new ListNode(10);
    InsertNode(head, four,0);
    Delete(head);
    Print(head);
    cout << "排序" << endl;
    ListSort(head);
    Print(head);
    return 0;
}

这篇关于C++ 链表 冒泡排序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!