-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDLlist.cpp
71 lines (59 loc) · 1.36 KB
/
DLlist.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
//
// Created by rarce on 03/09/20.
//
#include "DLlist.h"
void DLlist::insertAfter(DNode *curNode, int value) {
DNode *n = new DNode(value);
if (curNode == NULL) {
if (head != NULL) head->prev = n;
n->next = head;
head = n;
if (tail == NULL) tail = n;
}
else {
DNode *p = curNode->next;
n->prev = curNode;
n->next = p;
curNode->next = n;
if (p != NULL) p->prev = n;
else tail = n;
}
length++;
}
void DLlist::append(int value) { insertAfter(tail,value); }
void DLlist::prepend(int value) { insertAfter(NULL,value); }
int DLlist::at(int idx) const {
int i = 0;
// validate the index
if (idx < 0 || idx >= length) {
cout << "error" << endl;
exit(1);
}
// traverse the list until index
DNode *p = head;
for (int i = 0; i < idx; i++) p = p->next;
return p->data;
}
void DLlist::removeFirst() {
DNode *p;
p = head;
head = head ->next;
if (head != NULL) head-> prev = NULL;
else tail = NULL;
delete p;
length--;
}
int DLlist::getSize() const { return length; }
DLlist::~DLlist() {
while (head!=NULL) {
removeFirst();
}
}
DNode* DLlist::search(int key) const {
DNode *p = head;
while (p != NULL) {
if (p->data == key) return p;
p = p->next;
}
return p;
}