|
@@ -35,6 +35,8 @@ void Llist::display(ostream &out) const {
|
35
|
35
|
|
36
|
36
|
void Llist::insertAfter(Node *p, ElementType d) {
|
37
|
37
|
Node *n;
|
|
38
|
+
|
|
39
|
+ // p == NULL is interpreted as prepending
|
38
|
40
|
if (p == NULL) {
|
39
|
41
|
n = new Node(d, head);
|
40
|
42
|
head = n;
|
|
@@ -65,6 +67,7 @@ ostream& operator<< (ostream &out, const Llist& L) {
|
65
|
67
|
void Llist::printRev() const {
|
66
|
68
|
printRev(head);
|
67
|
69
|
}
|
|
70
|
+
|
68
|
71
|
void Llist::printRev(Node *p) const {
|
69
|
72
|
if (p != NULL) {
|
70
|
73
|
printRev(p->next);
|
|
@@ -74,22 +77,31 @@ void Llist::printRev(Node *p) const {
|
74
|
77
|
|
75
|
78
|
void Llist::removeAfter(Node *cur) {
|
76
|
79
|
Node *suc;
|
|
80
|
+
|
|
81
|
+ // cur == NULL is interpreted as removing the first
|
|
82
|
+ // node of the list
|
77
|
83
|
if (cur == NULL and head != NULL) {
|
78
|
84
|
suc = head->next;
|
79
|
85
|
delete head;
|
80
|
86
|
head = suc;
|
81
|
|
-
|
|
87
|
+ // in case the removed node was the only node
|
82
|
88
|
if (suc == NULL) tail = NULL;
|
83
|
89
|
}
|
84
|
90
|
else if (cur->next != NULL) {
|
85
|
91
|
suc = cur->next->next;
|
86
|
92
|
delete cur->next;
|
87
|
93
|
cur->next = suc;
|
|
94
|
+ // if the removed node was the last, update the tail pointer
|
88
|
95
|
if (suc == NULL) tail = cur;
|
89
|
96
|
}
|
90
|
97
|
length--;
|
91
|
98
|
}
|
92
|
99
|
|
|
100
|
+// Assignment operator:
|
|
101
|
+// 1. empty the list of the invoking object
|
|
102
|
+// 2. traverse the list of the parameter appending each
|
|
103
|
+// node data to the invoking object
|
|
104
|
+
|
93
|
105
|
Llist& Llist::operator=(const Llist &L) {
|
94
|
106
|
if (this != &L) {
|
95
|
107
|
while(head != NULL) removeAfter(NULL);
|