-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring_queue.c
More file actions
159 lines (141 loc) · 2.51 KB
/
Copy pathstring_queue.c
File metadata and controls
159 lines (141 loc) · 2.51 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
/* AUTHOR: M Jake Palanker */
/* Terrible basic queue */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct string_queue_entry string_queue_entry;
typedef struct
{
size_t size;
string_queue_entry *head;
string_queue_entry *tail;
} string_queue;
struct string_queue_entry
{
char *data;
string_queue_entry *next;
};
int string_queue_size(const string_queue *q)
{
return q->size;
}
int string_queue_push(char* data, string_queue *q)
{
string_queue_entry *new;
new = malloc(sizeof(struct string_queue_entry));
new->data = data;
new->next = NULL;
if (string_queue_size(q) == 0)
{
q->head = new;
q->tail = new;
}
else
{
q->tail->next = new;
q->tail = new;
}
q->size = q->size + 1;
return 0;
}
char *string_queue_pop(string_queue *q)
{
string_queue_entry *temp;
char *retval;
if (q->size == 0)
{
printf("THIS string_queue IS EMPTY, WHY DID YOU POP?\n");
return NULL;
}
else if (q->size == 1)
{
q->size = 0;
retval = q->head->data;
free(q->head);
q->head = NULL;
q->tail = NULL;
}
else
{
q->size = q->size -1;
retval = q->head->data;
temp = q->head->next;
free(q->head);
q->head = temp;
}
return retval;
}
string_queue new_string_queue()
{
string_queue new;
new.size = 0;
new.head = NULL;
new.tail = NULL;
return new;
}
int string_is_in(char* data, const string_queue *q)
{
struct string_queue_entry *ptr;
ptr = q->head;
while(ptr != NULL)
{
if (strcmp(data, ptr->data) == 0)
return 1;
ptr = ptr->next;
}
return 0;
}
/*
int string_queue_remove(char *data, string_queue *q)
{
*//* DONT CALL IS BROKEN *//*
struct string_queue_entry *last;
struct string_queue_entry *next;
if (string_queue_size(q) == 0)
{
return -1;
}
next = q->head;
if (next->data == data)
{
string_queue_pop(q);
return 0;
}
while (next != NULL)
{
if (next->data == data)
{
last->next = next->next;
free(next);
q->size -= 1;
return 0;
}
last = next;
next = next->next;
}
return -1;
}*/
int string_queue_destroy(string_queue *q)
{
while (q->size > 0)
{
free(string_queue_pop(q));
}
q->head = NULL;
q->tail = NULL;
return 0;
}
string_queue string_queue_copy(string_queue *q)
{
char *copy;
string_queue_entry *ptr;
string_queue new = new_string_queue();
ptr = q->head;
while (ptr != NULL)
{
copy = strdup(ptr->data);
string_queue_push(copy, &new);
ptr = ptr->next;
}
return new;
}