C/C++ 循环双链表

数据结构-----C/C++实现 专栏收录该内容
9 篇文章 0 订阅

在循环链表的基础上将尾节点和头节点连接起来,逻辑上是个环状

#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
typedef struct double_list
{
    int date;
    struct double_list *pre;
    struct double_list *next;
}list;

//创建头结点
list* create_node()
{
    list *node = (list*)malloc(sizeof(list));
    if(node == NULL)
    {
        printf("创建头结点失败!\n");
        return NULL;
    }
    node->pre = node;
    node->next = node;
    return node;
}

//插入元素(尾插法)
int insert_list(list *head)
{
    if(head == NULL)
    {
        printf("链表为空!\n");
        return 0;
    }
    list *p = head;
    int val;
    printf("请输入要插入的元素:");
    scanf("%d",&val);
    list *newnode =  create_node();
    newnode->date = val;
    while(p->next != head)
        p = p->next;
    newnode->pre = p;
    newnode->next = p->next;
    p->next = newnode;
    head->pre = newnode;
    return 1;
}
//创建链表(头插法)
int first = 0;
int creat_by_head(list *head)
{
    if(head == NULL)
    {
        printf("链表为空!\n");
        return 0;
    }
    list *p = head;
    int val;
    printf("请输入要插入的元素(输入9999表示结束)\n");
    scanf("%d",&val);
    while(val!=9999)
    {
        list *newnode =  create_node();
        newnode->date = val;
        if(first==0)
        {
        p->next = newnode;
        newnode->pre = p;
        newnode->next = p;
        p->pre = newnode;
        first++;
        }
        else
        {
            newnode->next = p->next;
            newnode->pre = p;
            p->next = newnode;

        }
        scanf("%d",&val);
    }
    return 1;
}

//打印双向循环链表
int print_list(list *head)
{
    if(head == NULL)
    {
        printf("链表为空!\n");
        return 0;
    }
    list *ptr = head->next;//跳过头节点
    while(ptr != head)
    {
        printf("%d   ",ptr->date);
        ptr = ptr->next;
    }
}

int delete_list(list *head)
{
    if(head == NULL)
    {
        printf("链表为空!\n");
        return 0;
    }
    list *p = head;
    list *q = head->next;
    int val;
    printf("请输入要删除的元素:");
    scanf("%d",&val);
    while(p->next != head)
    {
        if(q->date == val)
        {
            if(q->next == head)
            {
                p->next = head;
                free(q);
                return 1;
            }
            q->next->pre = p;
            p->next = q->next;
            free(q);
            return 1;
        }
        p = p->next;
        q = q->next;
    }
    printf("没有找到该元素%d\n",val);
    return 0;
}

int update_list(list *head)
{
    if(head == NULL)
    {
        printf("链表为空!\n");
        return 0;
    }
    list *temp = head;
    int olddate,newdate;
    printf("请输入要修改的值:");
    scanf("%d",&olddate);
    printf("修改为:");
    scanf("%d",&newdate);
    while(temp->next != head)
    {
        if(temp->next->date == olddate)
        {
            temp->next->date = newdate;
            return 1;
        }
        temp = temp->next;
    }
    printf("没有找到要修改的元素!\n");
    return 0;
}

int seek_list(list *head)
{
    if(head == NULL)
    {
        printf("链表为空!\n");
        return -1;
    }
    int val,i=0;
    list *p = head;
    printf("请输入要查找的元素:");
    scanf("%d",&val);
    while(p->next != head)
    {
        i++;
        if(p->next->date == val)
        {
            printf("查找到该元素%d,位置为%d\n",val,i);
            return 1;
        }
        p = p->next;
    }
    printf("没有找到该元素!\n");
    return 0;
}
int main()
{
    list *head = create_node();
    printf(creat_by_head(head));   
    return 0;
}

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

梦里有个小宇宙

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值