-
Notifications
You must be signed in to change notification settings - Fork 0
/
progetto.c
404 lines (392 loc) · 11.6 KB
/
progetto.c
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define N 1024
//Globali
typedef struct nodo{
char *s; //1024
struct nodo * next; //8
}nodo;
typedef struct saveNode{
struct nodo * headList;
struct nodo * start;
struct nodo * oldCoda;
struct nodo * last;
struct saveNode * next;
int numNodi;
char c;
}saveNode;
typedef nodo * puntNodo;
typedef saveNode * puntSave;
puntSave undoPile, redoPile;
puntNodo savedLast,saveToUpdate,saveToUpdatePrec,nodoCoda;
int cont=1,flagUpdate=0,contUndoPile=0,contRedoPile = 0,flagFixSalvataggi=0;
char c;
//CAST
int castToInt(char s[],int * i,int castedValue){
if(s[*i]>47&&s[*i]<58) //Entro se numero
{
castedValue = (castedValue*10) + ((int)s[*i]) - 48; //Trasformo in intero
++(*i);
return castToInt(s,i,castedValue);
}
return castedValue; //Ritorno valore castato a intero
}
int castComandi(char s[],int valComandi[]){
int i=0;
valComandi[0] = castToInt(s,&i,0);
if(s[i]=='u'||s[i]=='r')
return i;
++i;
valComandi[1] = castToInt(s,&i,0);
return i;
}
//UPDATELISTA
void updateLista(puntNodo start,puntNodo n){
flagUpdate = 1;
undoPile->start = start;
undoPile->headList = n;
undoPile->numNodi = cont;
undoPile->oldCoda = nodoCoda;
contUndoPile++;
}
//INSERIMENTI
puntNodo inserimento(puntNodo n, char input[]){
puntNodo new = (puntNodo) malloc(sizeof(nodo));
n->s = (char *) malloc(1+strlen(input));
strcpy(n->s,input);
cont++;
n->next = new;
gets(input);
if(strcmp(input,".")==0){
n->next = NULL;
free(new);
return n;
}
inserimento(new,input);
}
puntNodo modifica(puntNodo n, char input[],int valComandi[]){
puntNodo temp = n;
puntNodo newHead = n;
puntNodo prec = temp;
puntNodo new = (puntNodo) malloc(sizeof(nodo));
int i;
for(i=1;i<valComandi[0];i++){
prec = temp;
temp = temp->next;
}
saveToUpdatePrec = prec;
saveToUpdate = temp;
if(valComandi[0]==1)
newHead = new;
else
prec->next = new;
while(strcmp(input,".")!=0&&temp!=NULL){ //TEMP POS !=0 MODIFICO!
new->s = (char *) malloc(1+strlen(input));
strcpy(new->s,input);
puntNodo newNodo = (puntNodo) malloc(sizeof(nodo));
newNodo->next = NULL;
new->next = newNodo;
gets(input);
if(i==valComandi[1]){
free(newNodo);
new->next = temp->next;
}
else{
new = new->next;
if(temp->next==NULL)
savedLast = temp;
temp = temp->next;
}
i++;
}
if(strcmp(input,".")!=0)
inserimento(new,input);
return newHead;
}
//STAMPA
void stampaLista(puntNodo n,int valComandi[]){
puntNodo temp = n;
if(valComandi[0]==0&&valComandi[1]==0)
printf(".\n");
else{
for(int i=1;i<valComandi[0]&&temp!=NULL;i++)
temp = temp->next;
int val = valComandi[1] -valComandi[0] + 1;
for(int i=0;i<val;i++){
if(temp!=NULL){
printf("%s\n",temp->s);
temp = temp->next;
}
else
printf(".\n");
}
}
}
puntNodo deleteNode(puntNodo n,puntNodo succ,int valComandi[]){
puntNodo prec = n;
puntNodo toBeDel = succ;
for(int i=1;i<valComandi[0]-1&&prec!=NULL;i++){
prec = prec->next;
}
//SALVATAGGI
if(valComandi[0]==1){
toBeDel = prec;
updateLista(NULL,toBeDel);
}
else{
toBeDel = prec->next;
updateLista(prec,toBeDel);
}
//ELIMINAZIONI
puntNodo newHead;
if(valComandi[1]-valComandi[0]==0){
prec->next = toBeDel->next;
undoPile->last = toBeDel;
}
else
if(valComandi[1]-valComandi[0]>0){
for(int i=valComandi[0];i<valComandi[1]&&toBeDel->next!=NULL;i++)
toBeDel = toBeDel->next;
undoPile->last = toBeDel;
if(valComandi[0]!=1)
prec->next = toBeDel->next;
else{ //DELETE TESTA
if(toBeDel==NULL)
newHead = NULL;
else
newHead = toBeDel->next;
return newHead;
}
}
}
puntNodo fixPos(puntNodo n,bool checkHead){
puntNodo temp = n;
if(checkHead){
cont = 1;
while(temp!=NULL){
cont++;
if(temp->next==NULL)
return temp;
temp = temp->next;
}
}
}
void noSenseDelete(){
flagUpdate = 1;
undoPile->headList = NULL;
contUndoPile++;
}
void fixSalvataggi(){ //Svuoto pila redo
puntSave toFreePile;
while(redoPile!=NULL){
toFreePile = redoPile;
redoPile = redoPile->next;
free(toFreePile);
}
contRedoPile = 0;
}
int swapPunt(puntNodo coda,int numNodi){
puntNodo tempCoda = nodoCoda;
nodoCoda = coda;
coda = tempCoda;
int contCurr = cont;
cont = numNodi;
return contCurr;
}
puntNodo change(puntNodo head,puntSave pile){
puntNodo saved = pile->headList;
puntNodo prec = pile->start;
puntNodo swap;
pile->numNodi = swapPunt(pile->oldCoda,pile->numNodi);
if(prec==NULL){
swap = head;
head = saved;
}
else{
swap = prec->next;
prec->next = saved;
}
pile->headList = swap;
return head;
}
puntNodo undoDelete(puntNodo head,puntSave pile){
puntNodo saved = pile->headList;
puntNodo prec = pile->start;
pile->numNodi = swapPunt(pile->oldCoda,pile->numNodi);
if(saved!=NULL){ //REINSERISCO
if(prec==NULL)
head = saved;
else
prec->next = saved;
}
return head;
}
puntNodo redoDelete(puntNodo head,puntSave pile){
puntNodo saved = pile->headList;
puntNodo prec = pile->start;
puntNodo last = pile->last;
pile->numNodi = swapPunt(pile->oldCoda,pile->numNodi);
if(saved!=NULL){ //REINSERISCO
if(prec==NULL){
if(last!=NULL)
head = last->next;
else
head = NULL;
}
else{
if(last!=NULL)
prec->next = last->next;
else
prec->next = NULL;
}
}
return head;
}
void fixRedoPiles(){
puntSave temp = redoPile;
redoPile = temp->next;
if(undoPile->next!=NULL)
temp->next = undoPile->next;
else
temp->next = NULL;
undoPile->next = temp;
contRedoPile--;
contUndoPile++;
}
void fixUndoPiles(){
puntSave temp = undoPile->next;
undoPile->next = temp->next;
if(redoPile==NULL)
temp->next = NULL;
else
temp->next = redoPile;
redoPile = temp;
contRedoPile++;
contUndoPile--;
}
puntNodo redo(int valCom,puntNodo head,bool flagStart){
for(int i=0;i<valCom&&redoPile!=NULL;i++){
if(redoPile->c=='c')
head = change(head,redoPile);
else
head = redoDelete(head,redoPile);
fixRedoPiles();
}
return head;
}
puntNodo undo(int valCom,puntNodo head){
puntSave pile = undoPile->next;
for(int i=0;i<valCom&&pile!=NULL;i++){
if(pile->c=='c')
head = change(head,pile);
else
head = undoDelete(head,pile);
fixUndoPiles();
pile = undoPile->next;
}
return head;
}
int main(){
char rigaInput[N];
puntNodo head = NULL;
undoPile = (puntSave) malloc(sizeof(saveNode));
undoPile->next = NULL;
redoPile = NULL;
nodoCoda = head;
gets(rigaInput);
while(strncmp(rigaInput,"q",1)!=0)
{
int valComandi[2];
int contCom = castComandi(rigaInput,valComandi);
c = rigaInput[contCom];
if(rigaInput[contCom]=='c'){
if(flagFixSalvataggi&&contRedoPile!=0)
fixSalvataggi();
gets(rigaInput);
if(valComandi[0]==cont){
puntNodo oldHead = nodoCoda;
puntNodo oldPrec = savedLast;
if(head!=NULL){
nodoCoda->next = (puntNodo) malloc(sizeof(nodo));
nodoCoda = nodoCoda->next;
}
else{
head = (puntNodo) malloc(sizeof(nodo));
nodoCoda = head;
}
nodoCoda = inserimento(nodoCoda,rigaInput);
if(valComandi[0]==1)
updateLista(NULL,NULL);
else
updateLista(oldPrec,NULL);
}
else{
if(valComandi[0]==1){
head = modifica(head,rigaInput,valComandi);
updateLista(NULL,saveToUpdate);
}
else{
modifica(head,rigaInput,valComandi);
updateLista(saveToUpdatePrec,saveToUpdate);
}
if(flagUpdate)
nodoCoda = fixPos(head,true);
}
}
else{
if(rigaInput[contCom]=='p'){
stampaLista(head,valComandi);
}
else{
if(rigaInput[contCom]=='d'){
if(flagFixSalvataggi&&contRedoPile!=0)
fixSalvataggi();
if(head==NULL||valComandi[0]>=cont) //Delete senza senso lista gia vuota
noSenseDelete();
else{ //Delete con senso
if(valComandi[0]==1&&valComandi[0]==valComandi[1]){
puntNodo temp = head->next;
updateLista(NULL,head);
undoPile->last = head;
head = temp;
}
else{
if(valComandi[0]==1)
head = deleteNode(head,head->next,valComandi);
else
deleteNode(head,head->next,valComandi);
}
if(head!=NULL)
nodoCoda = fixPos(head,true);
else{
cont = 1;
nodoCoda = head;
}
}
}
else{
if(rigaInput[contCom]=='r'&&contRedoPile!=0)
head = redo(valComandi[0],head,false);
else{
if(rigaInput[contCom]=='u'&&contUndoPile!=0){
flagFixSalvataggi = 1;
head = undo(valComandi[0],head);
}
}
}
}
}
if(flagUpdate){
undoPile->c = c;
puntSave newNodoUndoPile = (puntSave) malloc(sizeof(saveNode));
newNodoUndoPile->next = undoPile;
undoPile = newNodoUndoPile;
flagUpdate = 0;
}
gets(rigaInput);
}
return 0;
}