-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.cpp
More file actions
119 lines (107 loc) · 2.34 KB
/
List.cpp
File metadata and controls
119 lines (107 loc) · 2.34 KB
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
#include "List.h"
#include "ListNode.h"
#include "ListItr.h"
#include <iostream>
using namespace std;
List::List(){
head = new ListNode();
tail = new ListNode();
int count = 0;
}
List::List(const List& source) {
head = new ListNode();
tail = new ListNode();
head ->next = tail;
tail ->previous = head;
count = 0;
ListItr iter(source.head->next);
while (!iter.isPastEnd()){
insertAtTail(iter.retrieve());
iter.moveForward();
}
}
List:: ~List(){
makeEmpty();
delete head;
delete tail;
}
List& List:: operator= (const List& source) {
if (this ==&source)
return *this;
else {
makeEmpty();
ListItr iter(source.head->next);
while(!iter.isPastEnd()){
insertAtTail(iter.retrieve());
iter.moveForward();
}
}
return *this;
}
bool List::isEmpty() const {
return size()==0;
}
void List:: makeEmpty(){
if(!isEmpty())
delete head->next;
}
ListItr List::find (int x){
ListItr *temp = new ListItr(head);
while(temp->retrieve() !=x && !temp->isPastEnd()){
temp->moveForward();
}
if(temp->isPastEnd())
return NULL;
else
return *temp;
}
void List::remove (int x){
ListItr temp = find(x);
temp.current->previous->next= temp.current->next;
temp.current->next->previous = temp.current->previous;
temp.current->previous ==NULL;
temp.current->previous==NULL;
delete temp.current;
}
ListItr List::first(){
ListItr *temp;
temp = new ListItr(head);
temp->moveForward();
if(temp->isPastEnd()){
return NULL;
}
else {
return *temp;
}
}
ListItr List::last(){
ListItr *temp;
temp = new ListItr(tail);
temp ->moveBackward();
if(temp->isPastBeginning()) return NULL;
else return *temp;
}
void List:: insertAtTail(int x){
ListNode* l = new ListNode;
tail->previous ->next = l;
l->previous = tail->previous;
l->value = x;
l->next = tail;
tail->previous = l;
}
void List:: insertAfter(int x, ListItr position){
ListNode* l = new ListNode;
position.current->previous ->next = l;
l->previous = position.current->previous;
l->value = x;
l->next = position.current;
position.current->previous = l;
}
void List:: insertBefore(int x, ListItr position){
ListNode* l = new ListNode(x);
position.current->next -> previous= l;
l->next = position.current->next;
l->value = x;
l->previous = position.current;
position.current->next = l;
}