-
Notifications
You must be signed in to change notification settings - Fork 1
/
1505057_offline.cpp
274 lines (254 loc) · 5.47 KB
/
1505057_offline.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
#include<stdio.h>
#include<stdlib.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(!length)
{
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("Current size: %d, current length: %d\n", listMaxSize, length);
}
int getLength()
{
return length;
}
int insertItemAt(int pos, int item)
{
if(length==listMaxSize)
{
int *temp;
listMaxSize*=2;
temp=(int*)malloc(sizeof(int)*listMaxSize);
for(int i=0; i<listMaxSize; i++)
{
temp[i]=list[i];
}
free(list);
list=temp;
}
if(pos>=length)
{
return NULL_VALUE;
}
else
{
list[length++]=list[pos-1];
list[pos-1]=item;
return SUCCESS_VALUE;
}
}
int shrink()
{
if(length<=(listMaxSize/2) && listMaxSize!=LIST_INIT_SIZE)
{
int * temp;
listMaxSize/=2;
temp=(int*) malloc(sizeof(int)*listMaxSize);
for(int i=0; i<length; i++)
{
temp[i]=list[i];
}
free(list);
list=temp;
return SUCCESS_VALUE;
}
else
return NULL_VALUE;
}
int deleteLast()
{
if(!length)
return NULL_VALUE;
length--;
int last=list[length];
shrink();
return last;
}
int Clear()
{
if(list)
{
free(list);
list=NULL;
length=0;
listMaxSize=0;
return SUCCESS_VALUE;
}
}
int deleteAll()
{
length=0;
while(listMaxSize>LIST_INIT_SIZE)
{
shrink();
}
return SUCCESS_VALUE;
}
int main(void)
{
initializeList();
while(1)
{
printf("\n1. Insert new item.\n2. Delete item at. \n3. Delete item.\n");
printf("4. Insert new item at a given position.\n5. Lenght. \n6. Print. \n7. exit.\n8. Shrink.\n");
printf("9. Delete Last Item.\n10. Clear list.\n11. Delete All\n\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)
{
int pos,item;
printf("Enter the postion and item :\n");
scanf("%d %d",&pos,&item);
insertItemAt(pos,item);
}
else if(ch==5)
{
printf("Lenght of list : %d\n",getLength());
}
else if(ch==6)
{
printList();
}
else if(ch==7)
{
break;
}
else if(ch==8)
{
shrink();
}
else if(ch==9)
{
deleteLast();
}
else if(ch==10)
{
Clear();
}
else if(ch==11)
{
deleteAll();
}
}
}
int main0()
{
initializeList();
char exp[50];
scanf("%s",exp);
int i=0;
while(exp[i])
{
int num;
if(exp[i]>=48 && exp[i]<=57)
{
num=exp[i]-48;
insertItem(num);
}
else if(exp[i]=='+' || exp[i]=='-' || exp[i]=='*' || exp[i]=='/')
{
int a=deleteLast(),b=deleteLast(),temp;
if(exp[i]=='+')
{
temp=a+b;
}
else if(exp[i]=='-')
{
temp=b-a;
}
else if(exp[i]=='*')
{
temp=a*b;
}
else if(exp[i]=='/')
{
temp=b/a;
}
insertItem(temp);
}
i++;
}
int ans=deleteLast();
printf("%s = %d\n",exp,ans);
Clear();
}