-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDLL.h
159 lines (137 loc) · 4.12 KB
/
DLL.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
// Class for implementing Dictionary using Double Linked List
#ifndef dll
#define dll
#include "Dictionary.h"
//Dictionary using Doubly Linked List (DLL)
class List: public Dictionary{
private:
List *next=nullptr;
List *prev=nullptr;
public:
//Parameterised Constructor
List(int address, int size, int key): Dictionary(address,size,key){}
//Non Parametrised Constructor
//initialising the head pointer
List(): Dictionary(-1,-1,-1){
next=nullptr;
prev=nullptr;
//initialsing the tail pointer
List *tail = new List(-1,-1,-1);
this->next=tail;
tail->prev= this;
}
//overriding insert function
List *Insert(int address, int size, int key){
if(this==nullptr || next==nullptr){
return nullptr;
}
List* t = new List(address,size,key);
this->next->prev = t;
t->next = this->next;
this->next = t;
t->prev = this;
return t;
}
//overriding the Delete function
bool Delete(Dictionary *d){
if(d==nullptr){
return false;
}
for(List *i= this->getFirst(); i!=nullptr; i=i->getNext()){
if((i->address == d->address) && (i->size == d->size) && (i->key==d->key)){
i->prev->next=i->next;
i->next->prev = i->prev;
i=nullptr;
return true;
}
}
}
//overrinding the Find() function
List *Find(int k, bool exact){
for(List *i= this->getFirst(); i!=nullptr; i=i->getNext()){
if(exact){
if(i->key==k){
return i;
}
}
else{
if(i->key>=k){
return i;
}
}
}
}
//overriding getFirst function
List *getFirst(){
if(this==nullptr){
return nullptr;
}
if(this->prev==nullptr){
if(this->next->next==nullptr){
return nullptr;
}
else{
return this->next;
}
}
else{
this->prev->getFirst();
}
}
//overriding getNext function
List *getNext(){
if(this->next->next==nullptr){
return nullptr;
}
else{
return this->next;
}
}
//overriding the sanity() function
bool sanity(){
//condition for null list
if(this==nullptr){
return true;
}
//condition for empty list
if((this->getFirst()==nullptr)){
//getfirst() would return null if current node is head pointer
if(this->next->address!=-1 || this->next->key!=-1 || this->next->size!=-1){
return false;
}
}
//condition to check loop in the list
List *slow=this->getFirst();
List *fast=slow->getNext();
for(; slow!=nullptr;slow=slow->getNext()){
if(slow==fast){
return false;
}
if(fast==nullptr){
break;
}
fast=fast->getNext();
if(fast==nullptr){
break;
}
fast=fast->getNext();
}
//checking head pointers
List *check=this->getFirst();
if(check->prev->size!=-1||check->prev->address!=-1||check->prev->key!=-1||check->prev->prev!=nullptr){
return false;
}
//checking next of current node is equal to prev of next node;
for(check=check->prev;check->getNext()!=nullptr;check=check->getNext()){
if(check->getNext()->prev!=check || check->prev->getNext()!=check){
return false;
}
}
//checking tail pointer
if(check->key!=-1 || check->address!=-1 ||check->size!=-1 ||check->getNext()!=nullptr ){
return false;
}
return true;
}
};
#endif