-
Notifications
You must be signed in to change notification settings - Fork 0
/
lista.cpp
102 lines (90 loc) · 2.25 KB
/
lista.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
#include "utils.cpp"
struct nodLista {
Articol info;
nodLista* next;
nodLista* prev;
};
// adaugare articol in lista
nodLista* adaugaArticol(nodLista* cap, int pos, Articol articol) {
nodLista* temp = (nodLista*)malloc(sizeof(nodLista));
temp->info = deepCopy(articol);
// nu avem cap
if (!cap) {
temp->next = NULL;
temp->prev = NULL;
return temp;
}
// inseram pe pozitia 0 (inceput)
if (pos == 0) {
temp->prev = NULL;
temp->next = cap;
cap->prev = temp;
return temp;
}
// mergem prin lista pana cand ajungem la capat sau la pozitie
nodLista* curent = cap;
int i = 0;
while (curent->next != NULL && pos != i) {
curent=curent->next; i++;
}
// verificam daca suntem la sfarsit
if (curent->next == NULL) {
// inserare la sfarsit
temp->next = NULL;
temp->prev = curent;
curent->next = temp;
} else {
// inserare pe pozitie
temp->next = curent;
temp->prev = curent->prev;
curent->prev = temp;
}
return cap;
}
nodLista* citesteArticole(nodLista* cap, char* f_name, int items) {
// citire din fisier
FILE* f = fopen(f_name,"r");
Articol articol;
int pos = INT_MAX; // inserare la sfarsit
for (int i=0; i<items; i++) {
articol = citesteArticol(f);
cap = adaugaArticol(cap, pos, articol);
}
fclose(f);
return cap;
}
int main() {
// lista articole
char f_name[] = "data-structures.in";
// citire numar de linii din fisier
int items = fileRecords(f_name);
nodLista* cap = NULL;
// citire articole in lista
cap = citesteArticole(cap, f_name, items);
// lista dubla inlantuita, parcurgere si afisare in ambele sensuri
nodLista* curent = cap;
printf("Lista dublu inlantuita, parcurgere pe next:\n");
do {
afiseazaArticol(curent->info);
if (curent->next) curent = curent->next; else break;
} while(true);
printf("Lista dublu inlantuita, parcurgere pe prev:\n");
do {
afiseazaArticol(curent->info);
if (curent->prev) curent = curent->prev; else break;
} while(true);
// cleanup
curent = cap;
do {
free(curent->info.nume);
if (curent->next) {
curent = curent->next;
free(curent->prev);
} else {
free(curent);
break;
}
} while(true);
printf("Sfarsit demonstratie.\n");
return 0;
}