原题链接:数据结构-双向循环链表
解题思路:真是坑人啊!明明是对的就是提交了好几次。
注意事项:注意得有return 0;还有就是想开头说的开始必须得是空表。还有代码不能加注释的我就加了出错了。
参考代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct Line{
struct Line *prior;
int data;
struct Line *next;
}line;
line *initList1(line *head){
head=(line*)malloc(sizeof(line));
head->prior=head;
head->next=head;
// line *list1=head;
//
// //构成环形双向链表
// for(int i=1;i<n;i++){
// line *c=(line*)malloc(sizeof(line));
// c->next=NULL;
// c->prior=NULL;
//
// list1->next=c;
// c->prior=list1;
// list1=list1->next;
// }
//
// list1->next=head;
// head->prior=list1;
return head;
}
line *insertList1(line *head,int data,int add)
{
line *temp1=(line*)malloc(sizeof(line));
temp1->data=data;
temp1->prior=NULL;
temp1->next=NULL;
if(add==1)
{
line *t,*t1;
t=head;
while(t->next!=head){
t=t->next;
}
t->next=temp1;
temp1->prior=t;
temp1->next=head;
head->prior=temp1;
head=temp1;
}
else
{
line *body=head;
for(int i=1;i<add-1;i++){
body=body->next;
}
if(body->next==NULL)
{
body->next=temp1;
temp1->prior=body;
}
else
{
body->next->prior=temp1;
temp1->next=body->next;
body->next=temp1;
temp1->prior=body;
}
}
return head;
}
line *deleteList1(line *head,int add){
line *temp=head;
line *tail=head;
if(add==1)
{
line *t;
t=head;
while(temp->next!=tail){
temp=temp->next;
}
temp->next=t->next;
t->next->prior=temp;
free(t);
head=temp->next;
}
else
{
for(int i=1;i<add;i++){
tail=temp;
temp=temp->next;
}
tail->next=temp->next;
temp->next->prior=tail;
free(temp);
temp=tail->next;
}
return head;
}
void display(line *head)
{
line *temp;
line *tail;
temp=head;
tail=head;
while(temp->next!=tail){
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
int main(){
line *p=NULL;
p=initList1(p);
int n;
while(scanf("%d",&n)!=EOF){
if(n==1){
int m,elem;
scanf("%d",&m);
scanf("%d",&elem);
p=insertList1(p,elem,m);
}
else if(n==2){
int m1;
scanf("%d",&m1);
p=deleteList1(p,m1);
}
else{
display(p);
}
}
return 0;
}0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复