-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathQ2-1.CPP
266 lines (237 loc) · 4.89 KB
/
Q2-1.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/***************************************************
题目描述:
不用额外的辅助空间,删除一个未排序的链表中重复的元素
Date:2014-03-20
****************************************************/
/**************************************************************************************************************
以下为操作链表的算法,该链表为单链表。
链表以头指针为索引,头指针指向头节点,头节点指向首节点,以此类推,直到尾节点。
头节点中不存放数据,只存放指向首节点的指针,
设置头节点的目的是为了方便对链表的操作,如果不设置头节点,而是直接由头指针指向首节点,
这样在对头指针后的节点进行插入删除操作时就会与其他节点进行该操作时有所不同,便要作为一种特殊情况来分析
**************************************************************************************************************/
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *pNext;
}NODE,*PNODE;
PNODE create_list();
void traverse_list(PNODE);
bool is_empty(PNODE);
int length_list(PNODE);
void sort_list(PNODE);
bool insert_list(PNODE,int,int);
bool delete_list(PNODE,int,int *);
void clear_list(PNODE);
void remove(PNODE);
int main(void)
{
int len;
PNODE pHead = NULL;
//创建链表并遍历输出
pHead = create_list();
traverse_list(pHead);
//求链表长度,并输出
len = length_list(pHead);
if(!is_empty(pHead))
printf("the length of the list is:%d\n",len);
remove(pHead);
printf("After removing duplicate, ");
traverse_list(pHead);
//清空链表,遍历输出(无数据输出)
clear_list(pHead);
printf("After cleared,");
traverse_list(pHead);
return 0;
}
/*
创建一个链表,并返回头指针
*/
PNODE create_list()
{
int val;
PNODE pHead =(PNODE)malloc(sizeof(NODE));
PNODE pCurrent = pHead;
pCurrent->pNext = NULL;
if(NULL == pHead)
{
printf("pHead malloc failed!");
exit(-1);
}
printf("Input first data(q to quit):");
while(scanf("%d",&val)==1)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if(NULL == pNew)
{
printf("pNew malloc failed!");
exit(-1);
}
pNew->data = val;
pCurrent->pNext = pNew;
pNew->pNext = NULL;
pCurrent = pNew;
printf("Input next data(q to quit):");
}
return pHead;
}
/*
遍历链表
*/
void traverse_list(PNODE pHead)
{
PNODE pCurrent = pHead->pNext;
printf("now dataes in the list are:\n");
while(pCurrent != NULL)
{
printf("%d ",pCurrent->data);
pCurrent = pCurrent->pNext;
}
printf("\n");
return ;
}
/*
判断链表是否为空
*/
bool is_empty(PNODE pNode)
{
if(NULL == pNode->pNext)
return true;
else
return false;
}
/*
求链表长度,即节点总数(不计入头节点)
*/
int length_list(PNODE pNode)
{
int count = 0;
PNODE pCurrent = pNode->pNext;
while(pCurrent != NULL)
{
count++;
pCurrent = pCurrent->pNext;
}
return count;
}
/*
选择法对链表排序
*/
void sort_list(PNODE pHead)
{
PNODE p,q;
int temp;
for(p=pHead->pNext;p!=NULL;p=p->pNext)
for(q=p->pNext;q!=NULL;q=q->pNext)
{
if(p->data>q->data)
{
temp = p->data;
p->data = q->data;
q->data = temp;
}
}
return ;
}
/*
在第pos个节点的后面插入一个新的节点,该节点中的数据为val
*/
bool insert_list(PNODE pHead,int pos,int val)
{
int i = 0;
PNODE p = pHead;
//i为0时,p指向第0个节点(这里指没有实际数据的头节点,不计入链表节点总数),
//i为1时,p指向第1个节点,i为几,p就指向第几个节点
while(p!=NULL && i<pos)
{
p = p->pNext;
i++;
}
//当pos的值大于链表长度时,便会出现这种情况
if(i>pos || p==NULL)
return false;
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if(NULL == pNew)
{
printf("pNew malloc failed!");
exit(-1);
}
pNew->data = val;
pNew->pNext = p->pNext;
p->pNext = pNew;
return true;
}
/*
删除第pos个节点,并将删除的数据保存在pData指针所指向的位置
*/
bool delete_list(PNODE pHead,int pos,int *pData)
{
int i = 0;
PNODE p = pHead;
//p最终指向第pos个节点前面的节点
//如果下面两句分别改为while(p!=NULL && i<pos)和if(i>pos || p==NULL),则p最终指向第pos个节点,
//这样因为得不到第pos个节点前面的那个节点,因此无法将pos前后两个节点连结起来
while(p->pNext!=NULL && i<pos-1)
{
p = p->pNext;
i++;
}
//当pos的值大于链表长度时,便会出现这种情况
if(i>pos-1 || p->pNext==NULL)
return false;
PNODE q = p->pNext;
*pData = q->data;
p->pNext = p->pNext->pNext;
free(q);
q = NULL;
return true;
}
/*
清空链表,即使链表只剩下头节点(头节点中没有数据)
*/
void clear_list(PNODE pHead)
{
PNODE p = pHead->pNext;
PNODE r = NULL;
while(p != NULL)
{
r = p->pNext;
free(p);
p = r;
}
pHead->pNext = NULL;
return ;
}
/*
删除链表中重复的元素
*/
void remove(PNODE pHead)
{
if(!pHead)
return ;
PNODE p1;
PNODE p2;
PNODE p2_prior; //指向p2前面的一个元素
for(p1 = pHead->pNext ; p1 ; p1 = p1->pNext)
{
p2 = p1->pNext;
p2_prior = p1;
while(p2)
{
//如果当前元素需要删除,则在删除该元素后,p2需要跳过该元素
if(p1->data == p2->data)
{
p2_prior->pNext = p2->pNext;
free(p2);
p2 = p2_prior->pNext;
}
else
{ //如果当前元素不需要删除,则p2直接向后移动
p2_prior = p2;
p2 = p2->pNext;
}
}
}
}