-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
145 lines (116 loc) · 2.61 KB
/
list.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
#include "list.h"
struct ListNode *createListNode(const char *word)
{
struct ListNode *newListNode;
newListNode = (struct ListNode *)malloc(sizeof(struct ListNode));
if (newListNode == NULL) {
fprintf(stderr, "ListNode allocation error!\n");
exit(-1);
}
newListNode->word = (char *)malloc(sizeof(char) * (strlen(word) + 1));
if (newListNode->word == NULL) {
fprintf(stderr, "ListNode word allocation error!\n");
exit(-1);
}
strcpy(newListNode->word, word);
newListNode->next = NULL;
return newListNode;
}
void deleteListNode(struct ListNode *listNode)
{
free(listNode->word);
free(listNode);
}
struct List *createList(void)
{
struct List *newList;
newList = (struct List *)malloc(sizeof(struct List));
if (newList == NULL) {
fprintf(stderr, "List allocation error!\n");
exit(-1);
}
newList->numberElements = 0;
newList->start = NULL;
return newList;
}
void deleteList(struct List *list)
{
struct ListNode *current;
struct ListNode *forward;
current = list->start;
while (current != NULL) {
forward = current->next;
deleteListNode(current);
current = forward;
}
free(list);
}
int isElementInList(struct List *list, const char *word)
{
struct ListNode *element;
element = list->start;
while (element != NULL) {
if (strcmp(element->word, word) == 0)
return 1;
element = element->next;
}
return 0;
}
void addElementToList(struct List *list, const char *word)
{
struct ListNode *elementToAdd;
struct ListNode *current;
elementToAdd = createListNode(word);
if (list->start == NULL) {
list->start = elementToAdd;
list->numberElements++;
return;
}
current = list->start;
while (1) {
if (strcmp(current->word, word) == 0) {
deleteListNode(elementToAdd);
return;
}
if (current->next != NULL)
current = current->next;
else
break;
}
current->next = elementToAdd;
list->numberElements++;
}
void removeElementFromList(struct List *list, const char *word)
{
struct ListNode *current;
struct ListNode *temp;
if (list->start == NULL)
return;
current = list->start;
if (strcmp(current->word, word) == 0) {
list->start = list->start->next;
deleteListNode(current);
list->numberElements--;
return;
}
while (current->next != NULL) {
if (strcmp(current->next->word, word) == 0) {
temp = current->next;
current->next = temp->next;
deleteListNode(temp);
list->numberElements--;
break;
}
current = current->next;
}
}
void printList(struct List *list)
{
struct ListNode *currentNode;
currentNode = list->start;
while (currentNode != NULL) {
printf("%s ", currentNode->word);
currentNode = currentNode->next;
}
printf("\n");
}