-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubly_List.h
212 lines (203 loc) · 4.77 KB
/
Doubly_List.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <unordered_map>
using namespace std;
//Node Defination
template <typename T>
class Node
{
public:
T data;
Node <T> * next;
Node <T> * prev;
Node(){next=prev=NULL;}
};
//Class Defination
template <typename T>
class Doubly_List
{
public:
Doubly_List();
long long int N; //Number of nodes
long long int Page_Faults,Capacity;
T Front();
T Back();
bool Empty();
bool Full();
long long int Search(T data);
void Set_Capacity(long long int cap);
void Insert(T data);
void Print();
private:
Node<T> * head;
Node<T> * tail;
unordered_map<T,long long int>mp; //Mapping: {Data -> Position in LRU Structure(1 based indexing)}
};
template<typename T>
Doubly_List<T>:: Doubly_List()
{
head=NULL;
tail=NULL;
N=0;
Page_Faults=0;
}
template <typename T>
T Doubly_List<T>:: Front()
{
return head->data;
}
template <typename T>
T Doubly_List<T>:: Back()
{
return tail->data;
}
template <typename T>
bool Doubly_List<T>:: Empty()
{
return (head==NULL);
}
template <typename T>
bool Doubly_List<T>:: Full()
{
return (N==Capacity);
}
template <typename T>
long long int Doubly_List<T>:: Search(T data)
{
return (mp[data] ? mp[data] : 0);
}
template <typename T>
void Doubly_List<T> :: Set_Capacity(long long int n)
{
Capacity=n;
}
template <typename T>
void Doubly_List<T> :: Print()
{
Node<T> * tmp=head;
while(tmp!=NULL)
{
cout<<tmp->data<<" ";
tmp=tmp->next;
}
}
template <typename T>
void Doubly_List<T> :: Insert(T data)
{
if(Search(data))
{
long long int pos=mp[data];
if(pos!=Capacity)
{
long long int i=1;
if(pos==1)
{
Node <T> * tmp=head;
head=head->next;
delete(tmp);
Node<T> * newnode=new Node<T>();
newnode->data=data;
if(head==NULL)
{
head=newnode;
tail=newnode;
mp[data]=1;
}
else
{
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
mp[data]=N+1;
}
return;
}
if(pos <= N/2) //Traverse from first
{
Node <T> * no=head;
while(i != pos)
{
no=no->next;
i++;
}
no->prev ->next=no->next ;
no->next ->prev=no -> prev;
delete(no);
}
else //Traverse from last
{
pos=N-pos +1;
Node <T> * no=tail;
while(i != pos)
{
no=no->prev;
i++;
}
no->prev ->next=no->next ;
no->next ->prev=no -> prev;
delete(no);
}
Node<T> * newnode=new Node<T>();
newnode->data=data;
if(head==NULL)
{
head=newnode;
tail=newnode;
mp[data]=1;
}
else
{
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
mp[data]=N+1;
}
}
}
else
{
if(Full())
{
//LRU Structure is full,so its needed to replace with Least Recently Used data.
mp.erase(head->data);
mp[data]=Capacity;
Node<T> * tmp=head;
head=head->next;
delete(tmp);
Node<T> * newnode=new Node<T>();
newnode->data=data;
if(head==NULL)
{
head=newnode;
tail=newnode;
}
else
{
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
}
Page_Faults++;
}
else
{
//Insert at tail
Node<T> * newnode=new Node<T>();
newnode->data=data;
if(head==NULL)
{
head=newnode;
tail=newnode;
mp[data]=1;
}
else
{
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
mp[data]=N+1;
}
N++;
Page_Faults++;
}
}
}