-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.cpp
299 lines (267 loc) · 5.82 KB
/
list.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LIST_INIT_SIZE 2
#define NULL_VALUE -99999
#define SUCCESS_VALUE 99999
int listMaxSize;
int * list;
int length;
int shrink();
void initializeList()
{
listMaxSize = LIST_INIT_SIZE;
list = (int*)malloc(sizeof(int)*listMaxSize) ;
length = 0 ;
}
int searchItem(int item)
{
int i = 0;
for (i = 0; i < length; i++)
{
if( list[i] == item ) return i;
}
return NULL_VALUE;
}
int insertItem(int newitem)
{
if(list==NULL)
{
initializeList();
}
int * tempList ;
if (length == listMaxSize)
{
//allocate new memory space for tempList
listMaxSize = 2 * listMaxSize ;
tempList = (int*) malloc (listMaxSize*sizeof(int)) ;
int i;
for( i = 0; i < length ; i++ )
{
tempList[i] = list[i] ; //copy all items from list to tempList
}
free(list) ; //free the memory allocated before
list = tempList ; //make list to point to new memory
}
list[length] = newitem ; //store new item
length++ ;
return SUCCESS_VALUE ;
}
int deleteItemAt(int position) //version 2, do not preserve order of items
{
if ( position >= length ) return NULL_VALUE;
list[position] = list[length-1] ;
length-- ;
shrink();
return SUCCESS_VALUE ;
}
int deleteItem(int item)
{
int position;
position = searchItem(item) ;
if ( position == NULL_VALUE ) return NULL_VALUE;
deleteItemAt(position) ;
shrink();
return SUCCESS_VALUE ;
}
void printList()
{
int i;
for(i=0; i<length; i++)
printf("%d ", list[i]);
printf("\n\nCurrent size: %d, current length: %d\n\n", listMaxSize, length);
}
// ===========================// My Implementations//===================================
int getLength()
{
return length;
}
int insertItemAt(int pos, int item)
{
if(pos<length)
{
if (length == listMaxSize)
{
int *tempList;
//allocate new memory space for tempList
listMaxSize = 2 * listMaxSize ;
tempList = (int*) malloc (listMaxSize*sizeof(int)) ;
int i;
for( i = 0; i < length ; i++ )
{
tempList[i] = list[i] ; //copy all items from list to tempList
}
free(list) ; //free the memory allocated before
list = tempList ; //make list to point to new memory
}
list[length] = list[pos];
list[pos]=item;
length++ ;
return SUCCESS_VALUE ;
}
else
{
return NULL_VALUE;
}
}
int shrink()
{
if(length==LIST_INIT_SIZE)
{
return NULL_VALUE;
}
else
{
if(2*length==listMaxSize)
{
int *tempList;
//allocate new memory space for tempList
listMaxSize = listMaxSize/2 ;
tempList = (int*) malloc (listMaxSize*sizeof(int)) ;
int i;
for( i = 0; i < length ; i++ )
{
tempList[i] = list[i] ; //copy all items from list to tempList
}
free(list) ; //free the memory allocated before
list = tempList ; //make list to point to new memory
return SUCCESS_VALUE ;
}
else
{
return NULL_VALUE;
}
}
}
int deleteLast()
{
if(length>0)
{
length--;
shrink();
return SUCCESS_VALUE;
}
else return NULL_VALUE;
}
int clear()
{
if(list)
{
free(list);
list=NULL;
length=0;
listMaxSize=0;
return SUCCESS_VALUE;
}
else return NULL_VALUE;
}
int deleteAll()
{
if(listMaxSize==LIST_INIT_SIZE)
{
list[0]=list[1]=NULL;
length=0;
}
else
{
free(list);
listMaxSize=LIST_INIT_SIZE;
list = (int*)malloc(sizeof(int)*listMaxSize) ;
length = 0 ;
}
}
int removeDuplicate(int item)
{
int found=0,index=0,i=0,j=0,count=0;
for(i=0; i<length; i++)
{
if(list[i]==item)
{
found=1;
index=i;
//length--;
break;
}
}
if(found)
{
for(i=index+1; i<length; i++)
{
if(list[i]==item)
{
count++;
for(j=i; j<length-1; j++)
{
list[j]=list[j+1];
}
}
}
}
//length-=count;
//shrink();
for(i=0;i<count;i++)
{
length--;
shrink();
}
}
int main(void)
{
initializeList();
while(1)
{
printf("1. Insert new item.\n2. Delete item at.\n3. Delete item.\n");
printf("4. Get Length.\n5. Insert Item at(Position,Item) \n6. Delete last item\n");
printf("7. Clear\n8. Delete All\n9. Remove Duplicate\n10. Print. \n11.exit.\n");
int ch;
scanf("%d",&ch);
if(ch==1)
{
int item;
scanf("%d", &item);
insertItem(item);
}
else if(ch==2)
{
int pos;
scanf("%d", &pos);
deleteItemAt(pos);
}
else if(ch==3)
{
int item;
scanf("%d", &item);
deleteItem(item);
}
else if(ch==4){
printf("Length of the list is=%d\n",getLength());
}
else if(ch==5)
{
int item,pos;
scanf("%d%d",&pos,&item);
insertItemAt(pos,item);
}
else if(ch==6)
deleteLast();
else if(ch==7)
{
clear();
}
else if(ch==8)
deleteAll();
else if(ch==9)
{
int item;
scanf("%d",&item);
removeDuplicate(item);
}
else if(ch==10){
printList();
}
else if(ch==11)
{
break;
}
}
}