-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.c
More file actions
169 lines (142 loc) · 3.63 KB
/
List.c
File metadata and controls
169 lines (142 loc) · 3.63 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
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
// Implementation of the String List ADT
// !!! DO NOT MODIFY THIS FILE !!!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "List.h"
struct node {
char *s;
struct node *next;
};
struct list {
struct node *head;
struct node *tail;
int size;
};
static struct node *newNode(char *s);
static char *myStrdup(char *s);
static int qsortStrcmp(const void *ptr1, const void *ptr2);
////////////////////////////////////////////////////////////////////////
// Creates a new empty list
List ListNew(void) {
List l = malloc(sizeof(*l));
if (l == NULL) {
fprintf(stderr, "error: out of memory\n");
exit(EXIT_FAILURE);
}
l->head = NULL;
l->tail = NULL;
l->size = 0;
return l;
}
// Frees all memory allocated for the given list
void ListFree(List l) {
struct node *curr = l->head;
while (curr != NULL) {
struct node *temp = curr;
curr = curr->next;
free(temp->s);
free(temp);
}
free(l);
}
// Adds a string to the end of the list
void ListAppend(List l, char *s) {
struct node *n = newNode(s);
if (l->head == NULL) {
l->head = n;
} else {
l->tail->next = n;
}
l->tail = n;
l->size++;
}
static struct node *newNode(char *s) {
struct node *n = malloc(sizeof(*n));
if (n == NULL) {
fprintf(stderr, "error: out of memory\n");
exit(EXIT_FAILURE);
}
n->s = myStrdup(s);
n->next = NULL;
return n;
}
static char *myStrdup(char *s) {
char *copy = malloc((strlen(s) + 1) * sizeof(char));
if (copy == NULL) {
fprintf(stderr, "error: out of memory\n");
exit(EXIT_FAILURE);
}
return strcpy(copy, s);
}
// Returns the number of items in the list
int ListSize(List l) {
return l->size;
}
// Sorts the list in ASCII order
void ListSort(List l) {
char **items = malloc(l->size * sizeof(char *));
if (items == NULL) {
fprintf(stderr, "error: out of memory\n");
exit(EXIT_FAILURE);
}
int i = 0;
for (struct node *curr = l->head; curr != NULL; curr = curr->next) {
items[i++] = curr->s;
}
qsort(items, l->size, sizeof(char *), qsortStrcmp);
i = 0;
for (struct node *curr = l->head; curr != NULL; curr = curr->next) {
curr->s = items[i++];
}
free(items);
}
static int qsortStrcmp(const void *ptr1, const void *ptr2) {
char *s1 = *(char **)ptr1;
char *s2 = *(char **)ptr2;
return strcmp(s1, s2);
}
// Prints the list, one string per line
// If the strings themselves contain newlines, too bad
void ListPrint(List l) {
for (struct node *n = l->head; n != NULL; n = n->next) {
printf("%s\n", n->s);
}
}
////////////////////////////////////////////////////////////////////////
struct listIterator {
struct node *curr;
List list;
};
// Creates an iterator for the given list
ListIterator ListItNew(List l) {
ListIterator it = malloc(sizeof(*it));
if (it == NULL) {
fprintf(stderr, "error: out of memory\n");
exit(EXIT_FAILURE);
}
it->curr = l->head;
it->list = l;
return it;
}
// Gets the next item in the list. The item should not be modified.
char *ListItNext(ListIterator it) {
if (it->curr == NULL) {
fprintf(stderr, "error: no more items in iterator!\n");
exit(EXIT_FAILURE);
}
char *item = it->curr->s;
it->curr = it->curr->next;
return item;
}
// Checks if the list has a next item
bool ListItHasNext(ListIterator it) {
return it->curr != NULL;
}
// Frees the given iterator
void ListItFree(ListIterator it) {
free(it);
}