-
Notifications
You must be signed in to change notification settings - Fork 0
/
StructListDemo.c
178 lines (128 loc) · 4.09 KB
/
StructListDemo.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
/*
* Slightly more complex example - list contents are a simple struct
*/
#include <stdio.h>
#include <string.h>
#include "LinkedListAPI.h"
typedef struct name {
char* firstName;
char* lastName;
unsigned int age;
}Name;
//printFunc will return a string that contains a humanly readable copy of the list contents
char* printFunc(void *toBePrinted){
char* tmpStr;
Name* tmpName;
int len;
if (toBePrinted == NULL){
return NULL;
}
tmpName = (Name*)toBePrinted;
/*
Length of the string is:
length of the first and last names+3 spaces+4 chars for "Age:"+1 character for '\0'+20 digits to represent the age
While we only need 3 digits to represent the human age, if the age is uninitialised, it can be any int value
An int is 8 bytes on 64-bit system, and needs up to 20 chars to represent it
If we don't do this, our code will crash if age is uninitialized
*/
len = strlen(tmpName->firstName)+strlen(tmpName->lastName)+28;
tmpStr = (char*)malloc(sizeof(char)*len);
sprintf(tmpStr, "%s %s Age: %d", tmpName->firstName, tmpName->lastName, tmpName->age);
return tmpStr;
}
//We compare names by last name
int compareFunc(const void *first, const void *second){
Name* tmpName1;
Name* tmpName2;
if (first == NULL || second == NULL){
return 0;
}
tmpName1 = (Name*)first;
tmpName2 = (Name*)second;
return strcmp((char*)tmpName1->lastName, (char*)tmpName2->lastName);
}
//We need to free the first and last names, and then the Name struct itself
void deleteFunc(void *toBeDeleted){
Name* tmpName;
if (toBeDeleted == NULL){
return;
}
tmpName = (Name*)toBeDeleted;
free(tmpName->firstName);
free(tmpName->lastName);
free(tmpName);
}
int main(void){
Name* tmpName;
char tmpStr[100];
int memLen;
/*
Create the list. The list is allocated on the stack, and initializeList returns the list struct.
*/
List* list = initializeList(&printFunc, &deleteFunc, &compareFunc);
//Populate the list
for (int i = 0; i < 4; i++){
tmpName = (Name*)malloc(sizeof(Name));
tmpName->age = (i+1)*10;
sprintf(tmpStr, "Name%d", i);
memLen = strlen(tmpStr)+2;
tmpName->firstName = (char*)malloc(sizeof(char)*memLen);
strcpy(tmpName->firstName, tmpStr);
sprintf(tmpStr, "Lastname%d", i);
memLen = strlen(tmpStr)+2;
tmpName->lastName = (char*)malloc(sizeof(char)*memLen);
strcpy(tmpName->lastName, tmpStr);
insertBack(list, (void*)tmpName);
}
char* tmp = list->printData(getFromFront(list));
printf("First element in the list is: %s\n", tmp);
free(tmp);
tmp = list->printData(getFromBack(list));
printf("Last element in the list is: %s\n", tmp);
free(tmp);
void* elem;
//Create an iterator - again, the iterator is allocated on the stack
ListIterator iter = createIterator(list);
/*
Traverse the list using an iterator.
nextElement() returns NULL ones we reach the end of the list
*/
while ((elem = nextElement(&iter)) != NULL){
Name* tmpName = (Name*)elem;
/*
We use the printData function that we created to return a string representation
of the data associated with the current node
*/
char* str = list->printData(tmpName);
printf("%s\n", str);
//Since list.printData dynamically allocates the string, we must free it
free(str);
}
printf("\n");
/*
Use deleteDataFromList to remove an element wirh a specific last name
We use the last name since that is what compareFunc uses to compare two Names
*/
Name searchName;
searchName.lastName = malloc(sizeof(char)*(strlen("Lastname1")+1));
strcpy(searchName.lastName, "Lastname1");
void* retVal = deleteDataFromList(list, &searchName);
if (retVal != NULL){
Name* foundName = (Name*)retVal;
char* nameDescr = list->printData(foundName);
printf("Removed %s from the list\n", nameDescr);
list->deleteData(retVal);
free(nameDescr);
}else{
printf("A value with the last name Lastname1 not found the list\n");
}
free(searchName.lastName);
char* listDescr = toString(list);
printf("After removal, the list is %s\n", listDescr);
free(listDescr);
/*
Free all memory associated with the list contents and the list itself
*/
freeList(list);
return 0;
}