原题链接:数据结构-双向循环链表
解题思路:真是坑人啊!明明是对的就是提交了好几次。
注意事项:注意得有return 0;还有就是想开头说的开始必须得是空表。还有代码不能加注释的我就加了出错了。
参考代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | #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 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复