解题思路:创建链表删除所要求数据。
注意事项:此为头插法,最后用数组来重新输出
参考代码:
#include<iostream>
using namespace std;
struct Data
{
int a;
struct Data* next;
};
int main()
{
int n = 0,d=0,num,b[100000],i=0;
cin >> n ;
Data* h,*q,*v,*t,*e;
h=new Data;
h->next = NULL;
for (int i = 0; i <n; i++)
{
Data *p;
p = new Data;
cin >> p->a;
p->next = h->next;
h->next = p;
}
cin >> d;
t = h;
while(t->next!=NULL)
{
if (t->next->a == d&&t->next->next!=NULL) {
e = t->next; t->next = e->next; free(e);
}
else if (t->next->a == d && t->next->next == NULL) {
t->next = NULL; free(t->next->next);
}
else t = t->next;
}
v = h;
while(v->next!=NULL)
{
b[i] = v->next->a;
i++;
v = v->next;
}
for (int j = i-1; j >= 0; j--)
{
cout << b[j] << " ";
}
return 0;
}
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题9.4 (Java代码)浏览:1440 |
输出正反三角形 (C语言代码)浏览:853 |
WU-蓝桥杯算法提高VIP-企业奖金发放 (C++代码)浏览:1240 |
C语言程序设计教程(第三版)课后习题10.4 (C语言代码)浏览:577 |
字符逆序 (C语言代码)浏览:668 |
程序员的表白 (C语言代码)浏览:669 |
模拟计算器 (C语言代码)浏览:2343 |
C语言程序设计教程(第三版)课后习题6.7 (C语言代码)浏览:719 |
幸运数 (C++代码)浏览:2949 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:795 |