|
@@ -0,0 +1,119 @@
|
|
1
|
+#include "Llist.h"
|
|
2
|
+
|
|
3
|
+void Llist::append(ElementType d){
|
|
4
|
+ Node *n = new Node(d);
|
|
5
|
+
|
|
6
|
+ // in case of empty list
|
|
7
|
+ if (!head) {
|
|
8
|
+ head = n;
|
|
9
|
+ }
|
|
10
|
+ // otherwise, non empty list...
|
|
11
|
+ else {
|
|
12
|
+ tail->next = n;
|
|
13
|
+ }
|
|
14
|
+ tail = n;
|
|
15
|
+ length++;
|
|
16
|
+}
|
|
17
|
+
|
|
18
|
+void Llist::prepend(ElementType d) {
|
|
19
|
+ Node *n = new Node(d, head);
|
|
20
|
+
|
|
21
|
+ head = n;
|
|
22
|
+ // in case of empty list the tail = head = n
|
|
23
|
+ if (length == 0) tail = n;
|
|
24
|
+
|
|
25
|
+ length++;
|
|
26
|
+}
|
|
27
|
+
|
|
28
|
+void Llist::display(ostream &out) const {
|
|
29
|
+ Node *p = head;
|
|
30
|
+ while(p) {
|
|
31
|
+ out << p->data << " ";
|
|
32
|
+ p = p->next;
|
|
33
|
+ }
|
|
34
|
+}
|
|
35
|
+
|
|
36
|
+void Llist::insertAfter(Node *p, ElementType d) {
|
|
37
|
+ Node *n;
|
|
38
|
+ if (p == NULL) {
|
|
39
|
+ n = new Node(d, head);
|
|
40
|
+ head = n;
|
|
41
|
+ if (!tail) tail = n;
|
|
42
|
+ }
|
|
43
|
+ else {
|
|
44
|
+ n = new Node (d, p->next);
|
|
45
|
+ p->next = n;
|
|
46
|
+ if (tail == p) tail = n;
|
|
47
|
+ }
|
|
48
|
+ length++;
|
|
49
|
+}
|
|
50
|
+
|
|
51
|
+Node* Llist::search(ElementType d) const {
|
|
52
|
+ Node *p = head;
|
|
53
|
+ while(p) {
|
|
54
|
+ if (p->data == d) return p;
|
|
55
|
+ p = p->next;
|
|
56
|
+ }
|
|
57
|
+ return NULL;
|
|
58
|
+}
|
|
59
|
+
|
|
60
|
+ostream& operator<< (ostream &out, const Llist& L) {
|
|
61
|
+ L.display(out);
|
|
62
|
+ return out;
|
|
63
|
+}
|
|
64
|
+
|
|
65
|
+void Llist::printRev() const {
|
|
66
|
+ printRev(head);
|
|
67
|
+}
|
|
68
|
+void Llist::printRev(Node *p) const {
|
|
69
|
+ if (p != NULL) {
|
|
70
|
+ printRev(p->next);
|
|
71
|
+ cout << p->data << endl;
|
|
72
|
+ }
|
|
73
|
+}
|
|
74
|
+
|
|
75
|
+void Llist::removeAfter(Node *cur) {
|
|
76
|
+ Node *suc;
|
|
77
|
+ if (cur == NULL and head != NULL) {
|
|
78
|
+ suc = head->next;
|
|
79
|
+ delete head;
|
|
80
|
+ head = suc;
|
|
81
|
+
|
|
82
|
+ if (suc == NULL) tail = NULL;
|
|
83
|
+ }
|
|
84
|
+ else if (cur->next != NULL) {
|
|
85
|
+ suc = cur->next->next;
|
|
86
|
+ delete cur->next;
|
|
87
|
+ cur->next = suc;
|
|
88
|
+ if (suc == NULL) tail = cur;
|
|
89
|
+ }
|
|
90
|
+ length--;
|
|
91
|
+}
|
|
92
|
+
|
|
93
|
+Llist& Llist::operator=(const Llist &L) {
|
|
94
|
+ if (this != &L) {
|
|
95
|
+ while(head != NULL) removeAfter(NULL);
|
|
96
|
+ Node *cur = L.head;
|
|
97
|
+ while (cur != NULL) {
|
|
98
|
+ append(cur->data);
|
|
99
|
+ cur = cur->next;
|
|
100
|
+ }
|
|
101
|
+ }
|
|
102
|
+ return *this;
|
|
103
|
+}
|
|
104
|
+
|
|
105
|
+Llist::Llist(const Llist &L) {
|
|
106
|
+ head = tail = NULL;
|
|
107
|
+ length = 0;
|
|
108
|
+ if (this != &L) {
|
|
109
|
+ Node *cur = L.head;
|
|
110
|
+ while (cur != NULL) {
|
|
111
|
+ append(cur->data);
|
|
112
|
+ cur = cur->next;
|
|
113
|
+ }
|
|
114
|
+ }
|
|
115
|
+}
|
|
116
|
+
|
|
117
|
+Llist::~Llist() {
|
|
118
|
+ while(head != NULL) removeAfter(NULL);
|
|
119
|
+}
|