forked from birtony/btp500-f19
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab3.h
158 lines (144 loc) · 2.86 KB
/
lab3.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
#include <iostream>
/****************************************/
/* */
/* Lab 3 starter file */
/* V 1.1.1:fixed back_ typo in line 150 */
/* V 1.1: updated Sentinel::print() */
/* and sentinel::reversePrint() */
/* */
/* */
/****************************************/
template <typename T>
class DList{
struct Node{
T data_;
Node* next_;
Node* prev_;
Node(const T& data=T{},Node* next=nullptr, Node* prev=nullptr){
data_=data;
next_=next;
prev_=prev;
}
};
Node* front_;
Node* back_;
public:
DList(){
front_=nullptr;
back_=nullptr;
}
void push_front(const T& data);
void push_back(const T& data);
void pop_front();
void pop_back();
void print() const;
void reversePrint() const;
~DList();
};
template <typename T>
void DList<T>::push_front(const T& data){
}
template <typename T>
void DList<T>::push_back(const T& data){
}
template <typename T>
void DList<T>::pop_front(){
}
template <typename T>
void DList<T>::pop_back(){
}
template <typename T>
void DList<T>::print() const{
Node* curr=front_;
while(curr!=nullptr){
std::cout << curr->data_ << " ";
curr=curr->next_;
}
if(!front_){
std::cout << "empty list";
}
std::cout << std::endl;
}
template <typename T>
void DList<T>::reversePrint() const{
Node* curr=back_;
while(curr!=nullptr){
std::cout << curr->data_ << " ";
curr=curr->prev_;
}
if(!back_){
std::cout << "empty list";
}
std::cout << std::endl;
}
template <typename T>
DList<T>::~DList(){
}
template <typename T>
class Sentinel{
struct Node{
T data_;
Node* next_;
Node* prev_;
Node(const T& data=T{},Node* next=nullptr, Node* prev=nullptr){
data_=data;
next_=next;
prev_=prev;
}
};
Node* front_;
Node* back_;
public:
Sentinel(){
front_=new Node();
back_=new Node();
front_->next_=back_;
back_->prev_=front_;
}
void push_front(const T& data);
void push_back(const T& data);
void pop_front();
void pop_back();
void print() const;
void reversePrint() const;
~Sentinel();
};
template <typename T>
void Sentinel<T>::push_front(const T& data){
}
template <typename T>
void Sentinel<T>::push_back(const T& data){
}
template <typename T>
void Sentinel<T>::pop_front(){
}
template <typename T>
void Sentinel<T>::pop_back(){
}
template <typename T>
void Sentinel<T>::print() const{
Node* curr=front_->next_;
while(curr!=back_){
std::cout << curr->data_ << " ";
curr=curr->next_;
}
if(front_->next_==back_){
std::cout << "empty list";
}
std::cout << std::endl;
}
template <typename T>
void Sentinel<T>::reversePrint() const{
Node* curr=back_->prev_;
while(curr!=front_){
std::cout << curr->data_ << " ";
curr=curr->prev_;
}
if(back_->prev_==front_){
std::cout << "empty list";
}
std::cout << std::endl;
}
template <typename T>
Sentinel<T>::~Sentinel(){
}