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

//插入元素(尾插法)
{
{
printf("链表为空！\n");
return 0;
}
int val;
printf("请输入要插入的元素：");
scanf("%d",&val);
list *newnode =  create_node();
newnode->date = val;
p = p->next;
newnode->pre = p;
newnode->next = p->next;
p->next = newnode;
return 1;
}
//创建链表(头插法)
int first = 0;
{
{
printf("链表为空！\n");
return 0;
}
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;
}

//打印双向循环链表
{
{
printf("链表为空！\n");
return 0;
}
{
printf("%d   ",ptr->date);
ptr = ptr->next;
}
}

{
{
printf("链表为空！\n");
return 0;
}
int val;
printf("请输入要删除的元素：");
scanf("%d",&val);
{
if(q->date == val)
{
{
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;
}

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

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


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

06-09
12-27 2949

08-03 639
05-21 5761
07-25 1万+
09-26 883
05-13 2145
11-06 157
11-19
03-13 8234
02-26
11-21 1155
09-13 2万+

¥2 ¥4 ¥6 ¥10 ¥20

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