-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.cpp
executable file
·188 lines (154 loc) · 3.21 KB
/
list.cpp
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
// Ryan Wiener, Porter Haet, 2/16/17
#ifndef LIST_CPP
#define LIST_CPP
#include "list.h"
#include "word.h"
#include "itemtype.h"
// Passes initialization data by reference
template<typename T>
node<T>::node(T &d, node<T>* n) : data(d), next(n)
{
}
template<typename T>
T node<T>::getData()
{
return this->data;
}
// returns the address of the data
template<typename T>
T* node<T>::getDataPointer()
{
return &(this->data);
}
template<typename T>
node<T>* node<T>::getNext()
{
return this->next;
}
template<typename T>
void node<T>::setNext(node<T>* n)
{
this->next = n;
}
template<typename K, typename V>
valueNode<K, V>::valueNode(K& initKey, V& initValue, valueNode *initNext) : key(initKey), value(initValue), next(initNext)
{
}
// map constructor initializes the array to null
template<typename K, typename V>
map<K, V>::map()
{
for (int i = 0; i < MAX_SIZE; i++)
{
keys[i] = NULL;
}
}
template<typename K, typename V>
void map<K, V>::set(K key, V value) {
// access the place where the key hashs to
int hashCode = key.getHashCode() % MAX_SIZE;
valueNode<K, V>* ptr = keys[hashCode];
// does key exist?
if (ptr) {
// loop through possible keys
valueNode<K, V>* lagging = NULL;
while (ptr) {
// if found set value and exit
if (ptr->key == key) {
ptr->value = value;
return;
}
lagging = ptr;
ptr = ptr->next;
}
// ptr->key never equaled key
// add value to end of chain
lagging->next = new valueNode<K, V>(key, value, NULL);
}
else {
// insert key/value pair
keys[hashCode] = new valueNode<K, V>(key, value, NULL);
}
}
template<typename K, typename V>
V map<K, V>::get(K key) {
// access place where key could be
int hashCode = key.getHashCode() % MAX_SIZE;
valueNode<K, V>* ptr = keys[hashCode];
// key not found
if (!ptr) return NULL;
// begin searching for key
while (ptr) {
if (ptr->key == key) {
// key found
return ptr->value;
}
ptr = ptr->next;
}
// key not found
return NULL;
}
// initialize vars to NULL/0
template<typename S>
list<S>::list()
{
headptr = NULL;
iter = NULL;
len = 0;
}
template<class S>
int list<S>::length()
{
return len;
}
// adds data to linked list and to hash map
template<typename S>
void list<S>::pushfront(S& data) {
// insert new node at head, reassign head to new node
headptr = new node<S>(data, headptr);
hashMap.set(data, headptr);
len++;
}
template<typename S>
S list<S>::get(int index) {
node<S> *current = headptr;
for (int i = 0; i < index && current; i++)
{
current = current->getNext();
}
return current->getData();
}
// gets the address of node with data equal to argument given
template<typename S>
S* list<S>::getDataPointer(S &data)
{
node<S> *result = hashMap.get(data);
if (result)
{
S* dataPtr = result->getDataPointer();
return dataPtr;
}
return NULL;
}
// Methods below allow traversal of the linked list
template<typename S>
void list<S>::startIterating() {
iter = headptr;
}
template<typename S>
void list<S>::iterate() {
iter = iter->getNext();
}
template<typename S>
S list<S>::getCurrent() {
return iter->getData();
}
template<typename S>
S* list<S>::getCurrentPointer() {
return iter->getDataPointer();
}
template<typename S>
bool list<S>::hasNext() {
return iter != NULL;
}
#endif