forked from msdohehrty/dsa555-s16
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlistsentinels.h
133 lines (120 loc) · 2.2 KB
/
dlistsentinels.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
//singly linked list
template <typename T>
class DList{
struct Node{
T data_;
Node* next_;
Node* prev_;
Node(const T& data=T{}, Node* prev=nullptr, Node* next=nullptr){
data_=data;
next_=next;
prev_=prev;
}
};
Node* first_;
Node* last_;
public:
class iterator{
friend class DList;
Node* curr_;
DList* myList_;
iterator(Node* curr,DList* 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;
}
iterator operator--(){
//--x
if(curr_){
curr_=curr_->prev_;
}
return *this;
}
iterator operator--(int){
//--x
iterator old=*this;
if(curr_){
curr_=curr_->prev_;
}
return old;
}
bool operator==(const iterator& other){
return other.curr_==curr_;
}
bool operator!=(const iterator& other){
return other.curr_!=curr_;
}
};
//creates empty linked list
DList(){
first_=new Node();
last_=new Node();
first_->next_=last_;
last_->prev_=first_;
}
//insert node at front of list and return iterator
//to that node
//O(1)
iterator insertFront(const T& data){
Node* firstData=begin().curr_;
Node* nn=new Node(data,first_,firstData);
firstData->prev_=nn;
first->next_=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){
}
//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){
}
//O(1)
void removeFront(){
//begin() == end() means empty list
if(begin() != end()){
Node* rm=begin().curr_;
Node* next=rm->next_;
first_->next_=next;
next->prev_=first_;
delete rm;
}
}
//O(n)
void remove(iterator& it){
}
//O(n)
void removeBack(){
}
iterator begin(){return iterator(first_->next_,this);}
iterator end(){return iterator(last_,this);}
};