【练习】c++删除链表倒数第K个结点和 插入排序算法将单链表递增排序

这篇博客介绍了如何在单链表中删除倒数第K个节点的C++实现,以及如何使用直接插入排序对链表进行升序排列。在删除操作中,通过双指针技巧定位目标节点并完成删除。在排序过程中,创建了一个临时有序链表,然后将原链表节点逐个插入到有序链表中。注意,输出排序结果时的遍历方式对结果展示有影响。
摘要由CSDN通过智能技术生成

删除单链表倒数第K个结点

运行结果:
在这里插入图片描述

在这里插入图片描述
代码:


struct node {
    int data;
    node* next;

};
//删除链表的倒数第k个结点
bool delet(node*& L, int k) {
    node* p, * q, * t;
    p = L;
    int i = 0;
    while (i < k) {
        i++;
        p = p->next;
    }
    if (p == NULL)return false;
    q = L;
    while (p->next != NULL) {
        p = p->next;
        q = q->next;
    }
    t = q->next;
    q->next = t->next;
    delete t;
    return true;
}

int main()
{
    srand(0);
    node* m = new node;

    m->data = rand() % 5;
    //cout << m->data;
    node* n = new node;
    n->data = rand() % 7;
    m->next = n;

    node* nn = new node;

    nn->data = rand() % 8;
    n->next = nn;
    node* jj = new node;
    jj->data = 100;
    jj->next = NULL;
    nn->next = jj;
    node* copy_m = m;
    cout << "第一个链表:";
    while (copy_m) {
        cout << copy_m->data<<",";
        copy_m = copy_m->next;
    }
    delet(m, 2);
   copy_m = m;
    cout << "\n删除结点后链表:";
    while (copy_m) {
        cout << copy_m->data<<",";
        copy_m = copy_m->next;
    }



}

直接插入将单链表递增排序

结果:
在这里插入图片描述

//直接插入排序
void sort(node*& L) {
    node* p = L->next->next;
  
    node* q;
    L->next->next = NULL;//构建只含一个数据结点的有序表
    node* pre = L;
    while (p != NULL) {
        pre = L;//注意每次最外面的while中都要初始化pre
        q = p->next;//q保存当前p结点的后继结点
        while (pre->next != NULL && pre->next->data < p->data)
            pre = pre->next;
        p->next = pre->next;//当pre->next->data>p->data 在pre后面插入p结点 因为p的数据更小 按递增排序
        pre->next = p;
        p = q;//p指向原来的后继结点
    }
}
int main()
{
    srand(0);
    node* begin=new node;
    begin->next = NULL;
    node* m = new node;

    m->data = rand() % 15;
    //cout << m->data;
      m->next= begin->next;
      begin->next = m;
    node* n = new node;
    n->data = rand() % 7;
  n->next= begin->next;
  begin->next = n;
    node* nn = new node;

    nn->data = rand() % 20;
    nn->next = begin->next;
    begin->next = nn;
    node* jj = new node;
    jj->data = 10;
    jj->next = begin->next;
    begin->next = jj;
    node* copy_m = begin;
    cout << "第一个链表:";
    while (copy_m->next) {
     
        cout << copy_m->next->data<<",";
        copy_m = copy_m->next;
    }
  sort(begin);
   copy_m = begin;
    cout << "\n排序结点后链表:";
    while (copy_m->next) {
 
        cout << copy_m->next->data<<",";
        copy_m = copy_m->next;
    }



}

注意,这里在自己初始化链表的时候发现:
只有写成

 while (copy_m->next) {
 
        cout << copy_m->next->data<<",";
        copy_m = copy_m->next;
    }

时才能正确输出排序后的结果,
如果写成

 while (copy_m) {
  copy_m = copy_m->next;
        cout << copy_m->data<<",";
       
    }

是无法得到排序后结果的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值