forked from msdohehrty/dsa555-s16
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slist.h
174 lines (164 loc) · 2.85 KB
/
slist.h
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//singly linked list
template <typename T>
class SList{
struct Node{
T data_;
Node* next_;
Node(const T& data, Node* next=nullptr){
data_=data;
next_=next;
}
};
Node* first_;
Node* last_;
public:
class iterator{
friend class SList;
Node* curr_;
SList* myList_;
iterator(Node* curr,SList* myList){
curr_=curr;
myList_=myList;
}
public:
iterator(){
curr_=nullptr;
myList_=nullptr;
}
T& operator*(){
return curr_->data_;
}
const T& operator*() const{
return curr_->data_;
}
iterator operator++(){
//++x
if(curr_){
curr_=curr_->next_;
}
return *this;
}
iterator operator++(int){
//x++
iterator old=*this;
if(curr_){
curr_=curr_->next_;
}
return old;
}
bool operator==(const iterator& other){
return other.curr_==curr_;
}
bool operator!=(const iterator& other){
return other.curr_!=curr_;
}
};
//creates empty linked list
SList(){
first_=last_=nullptr;
}
//insert node at front of list and return iterator
//to that node
//O(1)
iterator insertFront(const T& data){
Node* nn=new Node(data,first_);
first_=nn;
if(first_->next_==nullptr){
last_=nn;
}
iterator rc(nn,this);
return rc;
}
//inserts a node to end of list
//and returns iterator to newly added node
//O(1)
iterator insertBack(const T& data){
Node* nn=new Node(data,nullptr);
if(last_==nullptr){
//empty list
first_=nn;
}
else{
last_->next_=nn;
}
last_=nn;
iterator rc(nn,this);
return rc;
}
//inserts a node after the node it refers to
//and returns iterator to newly added node
//O(1)
iterator insert(iterator& it, const T& data){
iterator rc;
if(it.myList_ == this && it!=end()){
Node* curr=it.curr_;
Node* nn=new Node(data,curr->next_);
curr->next_=nn;
if(curr==last_){
last_=nn;
}
rc=iterator(nn,this);
}
return rc;
}
//O(1)
void removeFront(){
if(first_){
Node* tmp=first_;
first_=first_->next_;
if(first_==nullptr){
last_=nullptr;
}
delete tmp;
}
}
//O(n)
void remove(iterator& it){
if(it!= end() && it.myList_ == this){
Node* rm = it.curr_;
if(rm == first_){
removeFront();
/*
first_=first_->next_;
if(first_==nullptr){
last_=nullptr;
}
delete rm;
*/
}
else{
Node* curr=first_;
while(curr->next_!=rm){
curr=curr->next_;
}
curr->next_=rm->next_;
if(rm == last_){
last_=curr;
}
delete rm;
}
}
}
//O(n)
void removeBack(){
if(last_){
Node* rm =last_;
if(first_==last_){
delete rm;
first_=last_=nullptr;
}
else{
Node* curr=first_;
//make curr point to second last node
while(curr->next_!=rm){
curr=curr->next_;
}
curr->next_=nullptr;
last_=curr;
delete rm;
}
}
}
iterator begin(){return iterator(first_,this);}
iterator end(){return iterator(nullptr,this);}
};