-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA.cpp
More file actions
283 lines (248 loc) · 6.12 KB
/
DSA.cpp
File metadata and controls
283 lines (248 loc) · 6.12 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <iostream>
#include <cstring>
#define MAXSIZE 100
#define QUEUE_MAXSIZE 4
#define DICTIONERY_MAX_SIZE 20
/// @brief Implementation of a custom static sized stack
class Stack{
private:
int data[MAXSIZE];
int top;
public :
Stack() : top(-1) {}
void push(int value){
if(top + 1 >= MAXSIZE){
std::cout << "Stack overflow";
return;
}
top++;
data[top] = value;
}
int pop(){
if(top == -1){
std::cout << "Stack underflow" << std::endl;
return -1;
}
int value = data[top];
top--;
return value;
}
int peek(){
if(top == -1){
std::cout << "Stack is empty" << std::endl;
return -1;
}
return data[top];
}
void print(){
for (int i = top; i >= 0; i--)
{
std::cout << data[i] << std::endl;
}
}
};
/// @brief Implementation of a custom static sized queue
class Queue{
};
struct KeyValuePair{
char *key;
int value;
};
unsigned long int hashFunction(const char* str, int max_size){
unsigned long int hash = 5321;
int c;
while((c = *str++)){
hash = ((hash << 5) + hash) + c;
}
//we need to limit it within max size
return hash % max_size;
};
/// @brief Implementation of a custom static sized dictionery
class Dictionery{
private:
KeyValuePair arr[DICTIONERY_MAX_SIZE];
public :
Dictionery() {}
~Dictionery(){
for (int i = 0; i < DICTIONERY_MAX_SIZE; i++){
if(arr[i].key != NULL){
delete[] arr[i].key;
}
}
}
void add(const char* key, int value){
//generate hash
unsigned long int hashIndex = hashFunction(key, DICTIONERY_MAX_SIZE);
arr[hashIndex].key = new char[strlen(key) + 1];
strcpy(arr[hashIndex].key, key);
arr[hashIndex].value = value;
}
int getValue(const char* key){
//search hash
unsigned long int searchHashIndex = hashFunction(key, DICTIONERY_MAX_SIZE);
char* foundKey = arr[searchHashIndex].key;
if(strcmp(key, foundKey) != 0){
std::cerr << "KeyNotFound" << std::endl;
return -1;
}
return arr[searchHashIndex].value;
}
};
//Implementation of a linked list
struct LinkNode{
int data;
LinkNode* next;
LinkNode(int val){
data = val;
next = nullptr;
}
};
class LinkedList{
LinkNode* head;
public :
LinkedList(){
head = nullptr;
}
//insert at end
void push_back(int value){
LinkNode* newNode = new LinkNode(value);
//head is not set yet
if(head == nullptr){
head = newNode;
return;
}
//iterate to find the next null node
LinkNode* temp = head;
while(temp->next != nullptr){
temp = temp->next;
}
temp->next = newNode;
}
//insert at front
void push_front(int value){
LinkNode* newNode = new LinkNode(value);
newNode->next = head;
head = newNode;
}
void reverse(){
LinkNode *prev = nullptr;
LinkNode *current = head;
LinkNode *next = nullptr;
//iterate through and swap pointers
while(current != nullptr){
next = current->next;
current->next = prev;
prev = current;
current = next;
}
//new head
head = prev;
}
void print(){
LinkNode *temp = head;
while(temp->next != nullptr){
std::cout << "["<< temp->data << "]" << "->";
temp = temp->next;
}
std::cout << "NULL \n";
}
};
//Implementation of BST
struct Node{
int val;
Node *left;
Node *right;
Node(int newVal){
val = newVal;
left = right = nullptr;
}
};
class BST{
Node *root;
Node* insert(Node* root, int val){
if(root == nullptr){
return new Node(val);
}
if(val < root->val){
root->left = insert(root->left, val);
}else if(val >= root->val){
//duplicate go to the right
root->right = insert(root->right, val);
}
return root;
}
bool search(Node* root, int val){
if(root == nullptr){
return false;
}
if(root->val == val){
return true;
}
if(val < root->val){
return search(root->left, val);
}else {
return search(root->right, val);
}
}
//in order traversal => left -> root ->right
void inorder(Node* node){
if(node == nullptr){
return;
}
inorder(node->left);
std::cout << node->val << " ";
inorder(node->right);
}
Node* findMin(Node* node){
while(node && node->left != nullptr){
node = node->left;
}
return node;
}
void mirror(Node* node){
if(node == nullptr){
return;
}
//swap left and right
Node *temp = node->left;
node->left = node->right;
node->right = temp;
//recurse for children
mirror(node->left);
mirror(node->right);
}
};
void testLinkedList(){
LinkedList list;
list.push_back(10);
list.push_back(11);
list.push_back(12);
list.push_back(13);
list.print();
list.reverse();
list.print();
}
void testStack(){
Stack s;
s.push(10);
s.push(20);
s.push(30);
s.print();
s.pop();
s.print();
std::cout << s.peek() << std::endl;
};
void testDictionery() {
Dictionery d;
d.add("chetan", 24);
d.add("pragya", 22);
d.add("pratik", 20);
std::cout << "age of chetan is : " << d.getValue("chetan") << std::endl;
std::cout << "age of suraj is : " << d.getValue("suraj") << std::endl;
std::cout << "age of pragya is : " << d.getValue("pragya") << std::endl;
}
int main(int argc, char const *argv[])
{
testLinkedList();
return 0;
}