-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearList.cpp
More file actions
266 lines (257 loc) · 5.96 KB
/
linearList.cpp
File metadata and controls
266 lines (257 loc) · 5.96 KB
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
#include "linearList.h"
#include<bits/stdc++.h>
using namespace std;
bool compare(float a,float b)
{
return b-a>1e-8;
}
// 顺序表打印
void sequenceList::print()
{
cout << curNumberOfItem << ":";
for (int i = 0; i < curNumberOfItem - 1; i++)
{
cout << myList[i] << ",";
}
cout << myList[curNumberOfItem - 1] << endl;
}
// 顺序表构建
sequenceList::sequenceList(const int &a, const int &b, float c[]) // 顺序表容量,初始化数组长度,初始化数组
{
maxCapcity = a;
myList = new float[a];
curNumberOfItem = b;
for (int s = 0; s < b; s++)
{
myList[s] = c[s];
}
}
sequenceList::~sequenceList() {}
bool sequenceList::addItem(const float &num) // 添加元素顺序表尾,成功返回true,失败返回false
{
if (myList != NULL && curNumberOfItem != maxCapcity)
{
myList[curNumberOfItem] = num;
curNumberOfItem++;
return true;
}
return false;
}
bool sequenceList::insertItem(const int &location, const float &target) // 插入元素target到location位置,成功返回true,失败返回false
{
if (myList != NULL && location <= curNumberOfItem && curNumberOfItem < maxCapcity && location > -1)
{
curNumberOfItem++;
for (int i = location; i < curNumberOfItem - 1; i++)
myList[location + 1] = myList[location];
myList[location] = target;
return true;
}
return false;
}
int sequenceList::deleteItem(const float &target) // 返回删除位置,找不到返回-1
{
for (int j = 0; j < curNumberOfItem; j++)
{
if (myList[j] == target)
{
curNumberOfItem--;
for (int i = j; i < curNumberOfItem; i++)
{
myList[i] = myList[i + 1];
}
return j;
}
}
return -1;
}
bool sequenceList::locate(const int &location, float &val)
{
if (location > -1 && location < curNumberOfItem)
{
val = myList[location];
return true;
}
return false;
}
int sequenceList::locate(const float &num)
{
for (int i = 0; i < curNumberOfItem; i++)
{
if (myList[i] == num)
return i;
}
return -1;
}
void sequenceList::reverse()
{
for (int i = 0, j = curNumberOfItem - 1; i <= j; i++, j--)
{
float s=myList[j];
myList[j]=myList[i];
myList[i]=s;
}
}
// 链表打印
void linkList::print()
{
curNode = firstNode;
cout << listSize << ":";
while (curNode != lastNode)
{
if (curNode->next == lastNode)
cout << curNode->next->data << endl;
else
{
cout << curNode->next->data << ",";
}
curNode = curNode->next;
}
}
linkList::linkList(const int &a, float b[])
{
listSize = a;
firstNode = new listNode;
firstNode->next=new listNode;
listNode *head = firstNode->next;
for (int i = 0; i < a - 1; i++)
{
head->data = b[i];
head->next = new listNode;
head = head->next;
}
head->data = b[a - 1];
lastNode = head;
}
linkList::~linkList() {}
bool linkList::headInsertItem(const float &num)
{
listSize++;
listNode *p = new listNode;
p->data = num;
p->next = firstNode->next;
firstNode->next = p;
return true;
}
bool linkList::tailInsertItem(const float &num)
{
listSize++;
listNode *p = new listNode;
p->data = num;
lastNode->next = p;
lastNode = p;
return true;
}
int linkList::insertItem(const int &location, const float &num)
{
if (location > -1 && location < listSize)
{
curNode = firstNode->next;
for (int i = 1; i <= location - 1; i++)
{
curNode = curNode->next;
}
listNode *target = new listNode;
target->data = num;
target->next = curNode->next;
curNode->next = target;
listSize++;
return location;
}
return -1;
}
int linkList::deleteItem(const float &num)
{
int ans = 0;
listNode *p = firstNode->next;
while (p->data == num)
{
listSize--;
firstNode->next = p->next;
p = firstNode->next;
ans=0;
return ans;
}
listNode *beforep = p;
p = p->next;
for (int i = 1; i < listSize - 1; i++, beforep = p, p = p->next)
{
while (p->data == num)
{
listSize--;
beforep->next = p->next;
p = p->next;
ans=i;
return ans;
}
}
while (lastNode->data == num)
{p->next = NULL;return listSize-1;}
return ans;
}
bool linkList::locate(const int &location, float &val)
{
if (location > -1 && location < listSize)
{
curNode = firstNode->next;
for (int i = 1; i <= location; i++)
{
curNode = curNode->next;
}
val=curNode->data;
return true;
}
return false;
}
int linkList::locate(const float& num)//按值查找
{
curNode=firstNode->next;
int i=0;
while(curNode->data!=num&&curNode->next!=NULL)
{
i++;
curNode=curNode->next;
}
if(curNode->next==NULL)return -1;
return i;
}
void linkList::ascendingOrder()
{
float *a=new float[listSize];
curNode=firstNode->next;
for(int i=0;i<listSize-1;i++,curNode=curNode->next)
{
a[i]=curNode->data;
}
a[listSize-1]=curNode->data;
sort(a,a+listSize,compare);
curNode=firstNode->next;
for(int i=0;i<listSize-1;i++,curNode=curNode->next)
{
curNode->data=a[i];
}
curNode->data=a[listSize-1];
delete[] a;
}
void linkList::reverse()
{
listNode *ahead=firstNode->next;
listNode *targetnow=ahead;
listNode *before=NULL;
while(targetnow!=NULL)
{
ahead=targetnow->next;
targetnow->next=before;
before=targetnow;
targetnow=ahead;
}
lastNode=firstNode->next;
firstNode->next=before;
}
void merge(linkList& a, linkList& b)
{
a=a+b;
a.ascendingOrder();
a.reverse();
return;
}