-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path2-3-23.cpp
130 lines (86 loc) · 1.57 KB
/
2-3-23.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
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
#include<iostream>
#define MAXSIZE 100
using namespace std;
typedef struct LNode{
int data;
struct LNode* next;
}LNode, *LinkList;
int tag[MAXSIZE];
void fun(LinkList &L){
LinkList p=L->next,pre=L,q;
while(p){
if(tag[abs(p->data)]){
q=p;
p=p->next;
pre->next=p;
free(q);
}else{
tag[abs(p->data)]=1;
pre=p;
p=p->next;
}
}
}
void fun_start(LinkList &L){
LinkList s=L->next;
int count=0;
while(s){
count++;
s=s->next;
}
int n=0;
cin>>n;
for(int i=0;i<n;i++)tag[i]=0;
fun(L);
}
////////////////////main
//////////////////////
////////////////////
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;
}
LinkList tmp,pre,now;
//原地逆转算法:pre置初值NULL,now为当前节点,为头结点下一节点,迭代。
//分析单个节点情况,tmp保存当前节点next,当前now的next指向pre,pre迭代为当前节点now
//now迭代为tmp
pre=NULL;
now=L->next;
while(now){
tmp=now->next;
now->next=pre;
pre=now;
now=tmp;
}
//带头结点
L->next=pre;
fun_start(L);
//打印链表
//不带头结点
s=L->next;
while(s){
cout<<s->data<<" ";
s=s->next;
}
cout<<endl;
free(s);
LinkList p=L;
while(L){
p=L;
L=L->next;
free(p);
}
return 0;
}
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////