首页 > 其他分享 >5-21打卡:双循环链表(无哨兵)练习

5-21打卡:双循环链表(无哨兵)练习

时间:2023-05-21 18:56:07浏览次数:37  
标签:node pre head 双循环 next 链表 打卡 data Node

#include<iostream>
using namespace std;

typedef struct Node {
    int data;
    Node* next;
    Node* pre;
}Node;

Node* initlist(int data)
{
    Node* node = new Node;
    node->data = data;
    node->next = node;
    node->pre = node;
    return node;
}

void headinsert(Node*& head, int data)
{
    Node* node = new Node;
    node->data = data;

    node->next = head;
    node->pre = head->pre;
    head->pre->next = node;
    head->pre = node;
    head = node;
}

void tailinsert(Node* head, int data)
{
    Node* node = new Node;
    node->data = data;

    node->pre = head->pre;
    node->next = head;
    head->pre->next = node;
    head->pre = node;
}

void printlist(Node* head)
{
    Node* node = head->next;
    cout << head->data << "->";
    while (node != head)
    {
        cout << node->data << "->";
        node = node->next;
    }
    cout << "NULL" << endl;
}

void nodedelete(Node*& head, int data) 
{
    Node* node = head;
    if (node->data == data)
    {
        node->pre->next = node->next;
        node->next->pre = node->pre;
        head = node->next; 
    }
    node = node->next;

    while (node != head)
    {

        if (node->data == data)
        {
            node->pre->next = node->next;
            node->next->pre = node->pre;
        }
        node = node->next;
    }
}


int main()
{
    Node* a = initlist(5);
    headinsert(a, 4);
    headinsert(a, 3);
    headinsert(a, 2);
    headinsert(a, 1);
    headinsert(a, 0);
    tailinsert(a, 7);
    tailinsert(a, 8);
    tailinsert(a, 9);
    tailinsert(a, 10);
    tailinsert(a, 11);
    printlist(a);
    nodedelete(a, 10);
    nodedelete(a, 0);
    nodedelete(a, 11);
    nodedelete(a, 4);
    printlist(a);
    return 0;
}

标签:node,pre,head,双循环,next,链表,打卡,data,Node
From: https://www.cnblogs.com/wlxdaydayup/p/17418975.html

相关文章

  • 5-20打卡:qt实现开启关闭窗口
    我想要实现:按下新建窗口,然后新建出一个窗口,这时按钮变成关闭窗口,然后我再点击关闭窗口按钮、那么新建的窗口关闭。//在Widget类里面声明一个QWidget的指针成员变量QWidget*w;Widget::Widget(QWidget*parent):QWidget(parent),ui(newUi::Widget){ui->set......
  • 5-19打卡:lambda表达式
    Lambda表达式是C++11引入的一种非常方便和强大的功能,它允许你创建简洁、匿名的函数对象。在许多情况下,Lambda表达式可以使代码更简洁、易读。接下来,我会详细介绍C++Lambda表达式的基本概念、语法以及一些使用场景。Lambda表达式基本概念Lambda表达式是一个匿名的内联......
  • 5.21打卡
      三、程序流程图 四、代码实现#include<bits/stdc++.h>usingnamespacestd;main(){longn,sum,i;while(scanf("%ld",&n)!=EOF){printf("ÔÚ1-%ldÖ®¼äµÄ½×ÌÝÊýΪ£º\n",n);sum=0;for(i=7;i<=n......
  • 第28天打卡
    问题: 源代码:#include<stdio.h>#include<math.h>intmain(){inta,b,c,count=0;for(a=1;a<=100;a++){c=(int)sqrt(a*a+b*b);if(c*c==a*a+b*b&&a+b>c&&b+c>a&&c<=100){printf("%4d%4d%4d ",a,b,c);cou......
  • c++打卡第三十二天
    以点类Point及平面图形类Plane为基础设计圆类Circle一、问题描述以点类Point及平面图形类Plane为基类公有派生圆类Circle,main(void)函数完成对其的测试。Point类结构说明:Point类的数据成员包括:①私有数据成员:X坐标x(double型),Y坐标y(double型)。Point类成员函数包括:①有参构造......
  • 每日打卡
    素数问题描述:求一个范围内的所有素数问题分析:素数指除了1和本身外没有因数的数,2是最小的素数,所以判断一个数是否为质数可以看从2到其平方根的范围内有没有其的因数,有整数平方根的数不可能是素数代码:#include<stdio.h>#include<math.h>intmain(){intstart,end,i,k,m,count......
  • 图解LeetCode——19. 删除链表的倒数第 N 个结点
    一、题目给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。二、示例2.1>示例1:【输入】head=[1,2,3,4,5],n=2【输出】[1,2,3,5]2.2>示例2:【输入】head=[1],n=1【输出】[]2.3>示例3:【输入】head=[1,2],n=1【输出】[1]提示:链......
  • #yyds干货盘点# LeetCode程序员面试金典:有序链表转换二叉搜索树
    题目:给定一个单链表的头节点 head ,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树。本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差不超过1。 示例1:输入:head=[-10,-3,0,5,9]输出:[0,-3,9,-10,null,5]解释:一个可能的答案是[0,-3,9,-1......
  • 5月19日打卡
    例5-1题目:作用域实例clude<iostream>usingnamespacestd;inti;intmain#in(){i=5;{inti;i=7;cout<<"i="<<i<<endl;}cout<<"i="<<i<<endl;retur......
  • 5.20打卡
      3.程序流程图4.代码实现#include<bits/stdc++.h>usingnamespacestd;intmain(){intt,a[5];longintk,i;for(i=95860;;i++){for(t=0,k=100000;k>=10;t++){a[t]=(i%k)/(k/10);k/=10;......