-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.c
More file actions
183 lines (160 loc) · 3.35 KB
/
List.c
File metadata and controls
183 lines (160 loc) · 3.35 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
#include "Header.h"
#include "Util.h"
/**
* Functie care adauga un cuvant intr-o lista.
*/
void addWordToList(struct Node **headList, char *new_word)
{
struct Node *p;
if(*headList == NULL){
*headList = (struct Node*)malloc(sizeof(struct Node));
DIE(*headList == NULL, "Nu s-a putut aloca memorie !\n");
(*headList)-> value = (char*)malloc(strlen(new_word) + 1);
DIE((*headList)-> value == NULL, "Nu s-a alocat memorie !\n");
memcpy((*headList)-> value, new_word, strlen(new_word) + 1);
(*headList)->next = NULL;
}
else{
for(p = *headList;;p = p->next){
/*
Daca cuvantul exista deja in lista, va fi ignorat.
*/
if(strcmp(new_word, p->value) == 0)
break;
if(p->next == NULL){
p->next = (struct Node*)malloc(
sizeof(struct Node));
DIE(p->next == NULL, "Eroare alocare"
"memorie !\n");
p->next->value = (char*)malloc(
strlen(new_word) + 1);
DIE(p->next->value == NULL, "Eroare alocare"
"memeorie !\n");
memcpy(p->next->value, new_word,
strlen(new_word) + 1);
p->next->next = NULL;
break;
}
}
}
}
/**
* Functie care cauta un cuvant intr-o lista.
*/
int searchWordInList(struct Node **headList, char *wordToFind)
{
struct Node *p;
if(*headList == NULL){
return FALSE;
}
else{
for(p = *headList; p != NULL; p = p->next){
if(strcmp(wordToFind, p->value) == 0)
return TRUE;
}
}
return FALSE;
}
/**
* Functie care sterge un cuvant dintr-o lista.
*/
void removeWordFromList(struct Node **headList, char *wordToRemove)
{
struct Node *p;
struct Node *q;
if(*headList == NULL)
return;
if(strcmp(wordToRemove, (*headList)->value) == 0){
struct Node* temp;
temp = *headList;
*headList = (*headList)->next;
free(temp);
}
else{
for(p = *headList, q = p->next;
q != NULL;
p = p->next, q = q->next){
if(strcmp(wordToRemove, q->value) == 0){
p->next = q->next;
free(q);
break;
}
}
}
}
/**
* Functie care elibereaza o lista.
*/
void clearList(struct Node **headList)
{
struct Node *p;
struct Node *q;
if(*headList == NULL)
return;
p = *headList;
while(p){
q = p;
p = p->next;
free(q);
}
}
/**
* Functie care afiseza o lista.
*/
void print(struct Node *headList)
{
while(headList){
printf("%s ", headList->value);
headList = headList->next;
}
printf("\n");
}
/**
* Functie care printeaza bucketul de la un anumit index.
*/
void printList(struct Node **buckets, int index)
{
print(buckets[index]);
}
/**
* Functie care printeaza o lista intr-un fisier.
*/
void printListInFile(struct Node **buckets, int index, char *fileName)
{
FILE *fd;
fd = fopen(fileName, "a");
DIE(fd == NULL, "Nu s-a putut deschide fisierul !\n");
while(buckets[index]){
fprintf(fd, "%s ", (buckets[index])->value);
buckets[index] = (buckets[index])->next;
}
fprintf(fd, "\n");
fclose(fd);
}
/**
* Functie care afiseaza toate bucket-urile unei tabele.
*/
void printBuckets(struct Node **buckets, int size)
{
int i;
for(i = 0; i < size; ++i)
printList(buckets, i);
}
/**
* Printeaza toate Bucket-urile intr-un fisier.
*/
void printBucketsInFile(struct Node **buckets, char *fileName, int size)
{
FILE *fd;
int i;
fd = fopen(fileName, "a");
DIE(fd == NULL, "Eroare deschidere fisier !\n");
for(i = 0; i < size; ++i){
while(buckets[i]){
fprintf(fd, "%s ", (buckets[i])->value);
buckets[i] = (buckets[i])->next;
}
fprintf(fd, "\n");
}
fclose(fd);
}