-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.cpp
69 lines (63 loc) · 1.16 KB
/
linked_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
#include "linked_list.h"
LinkedList::LinkedList() {
head = 0;
}
LinkedList::~LinkedList() {
while(head)
destroy(0);
}
int LinkedList::size() {
int size = 0;
Node *node = head;
while(node)
{
size++;
node = node->next;
}
return size;
}
Node* LinkedList::at(int i) {
Node *node = head;
for(int c=0; c<i && node; c++)
node = node->next;
return node ? node : 0;
}
bool LinkedList::destroy(int i) {
Node *parent = head;
Node *after = 0;
Node *node = 0;
for(int c=0; c<i && parent; c++)
parent = parent->next;
if(!parent)
return false;
node = parent->next;
if(!node)
{
head = 0;
return true;
}
after = node->next;
delete node;
parent->next = after;
return true;
}
void LinkedList::push(Node *new_node)
{
int size = this->size();
if(size == 0)
head = new_node;
else {
Node *last = at(size-1);
last->next = new_node;
}
}
void LinkedList::push(const char *key, const char *value)
{
Node *new_node = new Node;
new_node->key = new char[256];
new_node->value = new char[256];
new_node->next = 0;
strcpy(new_node->key, key);
strcpy(new_node->value, value);
this->push(new_node);
}