-
Notifications
You must be signed in to change notification settings - Fork 9
/
genericDataStructure.c
153 lines (149 loc) · 3.78 KB
/
genericDataStructure.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
#include "genericDataStructure.h"
typedef struct node {
int data;
struct node * next;
} Node;
typedef struct list {
Node * head;
} List;
void* Create();
bool Execute(void* ptr,char* op,void* args);
bool IsReadOnly(void* ptr,char* op);
int gds_str2int(char* str);
Node * createnode(int data);
bool add(int data,List* list);
bool rmv(int data,List* list);
bool display(List* list);
bool reverse(List* list);
bool destroy(List* list);
/**************************************************************/
/*************** Private Functions ***************/
/**************************************************************/
void* Create() {
List* list = (List*) malloc(sizeof(List));
list->head = NULL;
return list;
}
bool Execute(void* ptr,char* op,void* args) {
if (strncmp (op,"add",3) == 0) {
return add(gds_str2int((char*)args),(List*)ptr);
} else if (strncmp (op,"remove",6) == 0) {
return rmv(gds_str2int((char*)args),(List*)ptr);
} else if (strncmp (op,"display",7) == 0) {
return display((List*)ptr);
} else if (strncmp (op,"reverse",7) == 0) {
return reverse((List*)ptr);
} else if (strncmp (op,"destroy",7) == 0) {
return destroy((List*)ptr);
}
return false;
}
bool IsReadOnly(void* ptr,char* op) {
if (strncmp (op,"add",3) == 0) {
return false;
} else if (strncmp (op,"remove",6) == 0) {
return false;
} else if (strncmp (op,"display",7) == 0) {
return true;
} else if (strncmp (op,"reverse",7) == 0) {
return false;
} else if (strncmp (op,"destroy",7) == 0) {
return false;
}
return false;
}
/**************************************************************/
/*************** Support Functions ***************/
/**************************************************************/
int gds_str2int(char* str) { /* Convert string to integer */
int dec = 0;
int i = 0;
int len = 0;
len = strlen(str);
for (i=0;i<len;i++) {
if ((48 <= str[i])&&(str[i] <= 57)) { /* If it's an integer, 48<=char<=57 */
dec = dec*10 + (str[i]-'0');
}
}
return dec;
}
Node * createnode(int data) {
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
bool add(int data,List* list) {
Node * current = NULL;
if (list->head == NULL) {
list->head = createnode(data);
} else {
current = list->head;
while (current->next != NULL) {
current = current->next;
}
current->next = createnode(data);
}
return true;
}
bool rmv(int data,List* list) {
Node * current = list->head;
Node * previous = current;
while (current != NULL) {
if (current->data == data) {
previous->next = current->next;
if (current == list->head)
list->head = current->next;
free(current);
return true;
}
previous = current;
current = current->next;
}
return true;
}
bool display(List* list) {
Node * current = list->head;
if (list->head == NULL)
return false;
while (current->next != NULL) {
printf("%d,", current->data);
current = current->next;
}
printf("%d\n", current->data);
return true;
}
bool reverse(List* list) {
Node * reversed = NULL;
Node * current = list->head;
Node * temp = NULL;
while (current != NULL) {
temp = current;
current = current->next;
temp->next = reversed;
reversed = temp;
}
list->head = reversed;
return true;
}
bool destroy(List* list) {
Node * current = list->head;
Node * next = current;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
free(list);
return true;
}
/**************************************************************/
/*************** Public Functions ***************/
/**************************************************************/
GenericDS* initializeDataStructure() {
GenericDS* s_pointers = (GenericDS*) malloc(sizeof(GenericDS));
s_pointers->Create = Create;
s_pointers->Execute = Execute;
s_pointers->IsReadOnly = IsReadOnly;
return s_pointers;
}