-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDLList.c
198 lines (173 loc) · 4.66 KB
/
DLList.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// DLList.c - Implementation of doubly-linked list ADT
// Written by John Shepherd, March 2013
// Modified by Luka Gamulin, August 2019
// Luka Gamulin
#include <assert.h>
#include <err.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include "DLList.h"
char *strdup(const char *s);
// data structures representing DLList
typedef struct DLListNode {
char *value; /**< value of this list item (string) */
struct DLListNode *prev;
int numwords; /**< pointer to previous node in list */
struct DLListNode *next;
/**< pointer to next node in list */
} DLListNode;
typedef struct DLListRep {
size_t nitems; /**< count of items in list */
DLListNode *first; /**< first node in list */
DLListNode *curr; /**< current node in list */
DLListNode *last; /**< last node in list */
} DLListRep;
/** Function prototypes for static functions local to this file */
static DLListNode *newDLListNode(char *it);
static void freeDLListNode(DLListNode *node);
/** Create a new, empty DLList. */
DLList newDLList (void)
{
DLListRep *new = malloc (sizeof *new);
if (new == NULL) err (EX_OSERR, "couldn't allocate DLList");
new->curr = new->first = new->last = NULL;
new->nitems = 0;
return new;
}
/** Release all resources associated with a DLList. */
void freeDLList (DLList L)
{
if (L == NULL) return;
DLListNode *curr = L->first;
while (curr != NULL) {
DLListNode *next = curr->next;
freeDLListNode (curr);
curr = next;
}
free (L);
}
/** Create a new DLListNode (private function) */
static DLListNode *newDLListNode (char *it)
{
//Allocates space in heap for a new node
DLListNode *new = malloc (sizeof *new);
//checks that new is not NULL before accessing memory to allocate value
if (new == NULL) err (EX_OSERR, "couldn't allocate DLList node");
// Then given the input string creates an independent copy into the node.
new->value = strdup(it);
//by default prev and node will point at null
new->prev = new->next = NULL;
new->numwords = 1;
return new;
}
/** Release a DLListNode (private function) */
static void freeDLListNode (DLListNode *node)
{
if (node == NULL) return;
free (node->value);
free (node);
}
/** Return the item at current position. */
char *DLListCurrent (DLList L)
{
assert (L != NULL);
return L->curr != NULL ? L->curr->value : NULL;
}
//Written by Luka Gamulin
/** insert an item before current item
* new item becomes current item */
void DLListBefore (DLList L, char *it)
{
assert (L != NULL);
//empty List
DLListNode *new = newDLListNode(it);
if (L->curr == NULL) {
L->first = new;
L->last = new;
L->curr = new;
}
//curr is in head
//condition can also be if curr == first
else if (L->curr->prev == NULL) {
new->next = L->first;
L->first->prev = new;
L->first = new;
L->curr = new;
}
//curr is in last and curr is anywhere else in middle
else {
new->prev = L->curr->prev;
L->curr->prev->next = new;
L->curr->prev = new;
new->next = L->curr;
L->curr = new;
}
L->nitems++;
}
/** delete current item
* new item becomes item following current
* if current was last, current becomes new last
* if current was only item, current becomes null */
void DLListDelete (DLList L)
{
assert (L != NULL);
//Empty List
if (L->curr == NULL)
return;
//One node
DLListNode *temp = L->curr;
if (L->curr->prev == NULL && L->curr->next == NULL) {
L->curr = NULL;
L->first = NULL;
L->last = NULL;
freeDLListNode(temp);
}
//curr is last
else if (L->curr == L->last && L->curr->prev != NULL) {
L->curr->prev->next = NULL;
L->curr = L->curr->prev;
L->last = L->curr;
freeDLListNode(temp);
}
//curr is head
else if (L->curr == L->first && L->curr->next != NULL) {
L->curr->next->prev = NULL;
L->first = L->first->next;
L->curr = L->first;
freeDLListNode(temp);
}
//middle case
else {
L->curr->prev->next = L->curr->next;
L->curr->next->prev = L->curr->prev;
L->curr = L->curr->next;
freeDLListNode(temp);
}
L->nitems--;
}
/** is the list empty? */
bool DLListIsEmpty (DLList L)
{
return L->nitems == 0;
}
bool InList(DLList L, char * word) {
assert(L != NULL);
if(DLListIsEmpty(L)) return 0;
int flag = 0;
DLListNode *curr = L->first;
while(curr != NULL) {
if(strcmp(curr->value,word) == 0){
curr->numwords++;
flag = 1;
}
curr = curr->next;
}
return flag;
}
//Get current node num words
int GetWords(DLList L) {
return L->curr->numwords;
}