原题链接:数据结构-双向循环链表
写了五六道链表题,终于能完全靠自己写出一份了
我没有按照题目要求来做,没有使用双向循环指针来做,我把需要判断的序号提前了一位,就免去了这个麻烦
下面粘代码:
#include<iostream>
using namespace std;
struct list
{
int value;
list* next;
};
void myshow(list* a);
void myinsert(list* a,int b,int c);
void mydelete(list* a,int b);//按题意来说只用写三个函数
int main()
{
short p;
int i, m;
list* head = (list*)malloc(sizeof(list));
head->next = NULL;
while (cin>>p)
{
if (!p)myshow(head);
if (p == 1) {
cin >> i >> m;
myinsert(head, i-1, m);
//将判断的序号提前一位可以免去双指针的麻烦
}
if (p == 2) {
cin >> i;
mydelete(head, i-1);
//将判断的序号提前一位可以免去双指针的麻烦
}//分三类情况来处理
}
return 0;
}
void myshow(list* a)
{
list* b = a;
while (b->next)
{
b = b->next;
cout << b->value << " ";
}
cout << endl;
}
void myinsert(list* a,int b,int c)
{
list* d = a;
while (b--)
{
d = d->next;
}
list* e = (list*)malloc(sizeof(list));
e->value = c;
e->next = d->next;
d->next = e;
}
void mydelete(list* a, int b)
{
list* c = a;
while(b--)
{
c = c->next;
}
list* d = c->next->next;
list* e = c->next;
c->next = d;
free(e);
}//没用双指针,代码明显变短了
欢迎吐槽,后续我再更新一下双指针的写法
9.9 分
1 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复