解题思路:创建链表删除所要求数据。
注意事项:此为头插法,最后用数组来重新输出
参考代码:
#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 人评分
WU-判定字符位置 (C++代码)浏览:1467 |
母牛的故事 (C语言代码)浏览:1007 |
字符串输入输出函数 (C语言代码)浏览:2564 |
Pascal三角 (C语言代码)浏览:705 |
淘淘的名单 (C语言代码)浏览:1291 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:810 |
多输入输出练习2 (C语言代码)浏览:1695 |
C语言程序设计教程(第三版)课后习题7.4 (C++代码)浏览:565 |
A+B for Input-Output Practice (I) (C语言代码)浏览:595 |
WU-C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:625 |