forked from ZenanZha/btp500-f16
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentinels.h
62 lines (54 loc) · 1.1 KB
/
sentinels.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
#include <iostream>
using namespace std;
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* head_;
Node* tail_;
public:
DList(){
head_=new Node(); //create front sentinel
tail_=new Node(); //create back sentinel
head_->next_=tail_;
tail_->prev_=head_;
}
void push_front(const T& data){
//this function adds a node between
//front sentinel and the first node in list
Node* first=head_->next_; //it is ok if this
//is back sentinel
Node* temp=new Node(data,first,head_);
head_->next_=temp;
first->prev_=temp;
}
void push_back(const T& data){
}
void pop_front(){
}
void pop_back(){
Node* last=tail_->prev_;
//checks if list is empty
if(last!=head_){
Node* secondLast=last->prev_;
tail_->prev_=secondLast;
secondLast->next_=tail_;
delete last;
}
}
void print() const{
Node* it=head_->next_;
while(it!=tail_){
cout << it->data_ << endl;
it=it->next_;
}
}
};