forked from Exely/WD-Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-3-2.cpp
59 lines (55 loc) · 947 Bytes
/
2-3-2.cpp
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
#include<iostream>
using namespace std;
typedef struct LNode{
int data;
struct LNode* next;
}LNode, *LinkList;
void fun(LinkList &L,int x){
LinkList p=L->next,pre=L,q;
while(p){
if(p->data==x){
q=p;
p=p->next;
pre->next=p;
free(q);
}else{
pre=p;
p=p->next;
}
}
}
int main(){
////////////////////////////////////////////////////////////
////////////////////建立链表////////////////////////////////
LinkList L,s;
int x=0;
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
cin>>x;
while(x!=9999){
s=(LinkList)malloc(sizeof(LNode));
s->data=x;
s->next=L->next;
L->next=s;
cin>>x;
}
//带头结点:
s=L->next;
//不带头结点:
//L=L->next;
//s=L;
////////////////////////////////////////////////////////////
//函数调用:返回链表
cin>>x;
fun(L,x);
s=L->next;
//打印链表:
while(s){
cout<<s->data<<" ";
s=s->next;
}
cout<<endl;
free(s);
free(L);
return 0;
}