-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.cpp
135 lines (120 loc) · 3.16 KB
/
LinkedList.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
#include "LinkedList.h"
#include <iostream>
#include "stdlib.h"
LinkedList::LinkedList() {
LinkedList::_init(DEFAULT_BLOCK_SIZE);
}
LinkedList::LinkedList(int blockSizeKb) {
LinkedList::_init(blockSizeKb);
}
void LinkedList::_init(int blockSizeKb) {
_blockSizeKb = blockSizeKb;
_head = NULL;
_tail = NULL;
_length = 0;
}
bool LinkedList::isEmpty() {
return _head == NULL && _tail == NULL;
}
int LinkedList::getBlockSizeKb() {
return _blockSizeKb;
}
int LinkedList::getLength() {
return _length;
}
void LinkedList::print() {
list_block *iterator = new list_block;
iterator = _head;
while (iterator != NULL) {
std::cout << iterator->address << " ";
iterator = iterator->next;
}
std::cout << std::endl;
}
list_block *LinkedList::getBlockAt(unsigned int position) {
list_block *current = new list_block;
current = _head;
if (position > 0) {
for (int i = 0; i < position; i++) {
current = current->next;
}
}
return current;
}
void LinkedList::addBlockStart(void *address) {
list_block *newBlock = new list_block;
newBlock->address = address;
newBlock->next = NULL;
if (_length == 0) {
_head = newBlock;
_tail = newBlock;
}
else {
newBlock->next = _head;
_head = newBlock;
}
_length++;
}
void LinkedList::addBlockEnd(void *address) {
list_block *newBlock = new list_block;
newBlock->address = address;
newBlock->next = NULL;
if (_length == 0) {
_head = newBlock;
_tail = newBlock;
}
else {
list_block *secondLastBlock = getBlockAt((_length == 1) ? 0 : _length - 2);
secondLastBlock->next = newBlock;
_tail = newBlock;
}
_length++;
}
void LinkedList::addBlockAt(unsigned int position, void *address) {
if (_length == 0 || position == 0) {
addBlockStart(address);
}
else {
list_block *newBlock = new list_block;
newBlock->address = address;
list_block *beforeBlock = new list_block;
beforeBlock = getBlockAt(position - 1);
newBlock->next = beforeBlock->next;
beforeBlock->next = newBlock;
_length++;
}
}
void LinkedList::removeBlockStart() {
removeBlockAt(0);
}
void LinkedList::removeBlockEnd() {
removeBlockAt(_length - 1);
}
void LinkedList::removeBlockAt(unsigned int position) {
if (_length != 0 && position < _length) {
list_block *removeBlock = new list_block;
removeBlock = getBlockAt(position);
if (_length == 1) {
_head = NULL;
_tail = NULL;
}
// else if (position == 0) {
// removeBlockStart();
// }
else {
if (position == 0) {
_head = removeBlock->next;
}
else {
list_block *blockBefore = new list_block;
blockBefore = getBlockAt(position - 1);
blockBefore->next = removeBlock->next;
if (position == _length - 1) {
_tail = blockBefore;
}
}
}
delete removeBlock;
_length--;
}
}