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;
}

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值