Data Structure Lab Programs PDF
Data Structure Lab Programs PDF
#include<iostream>
#include<conio.h>
#include<stdlib.h>
class Queue {
private:
int item, i;
int arr_queue[MAX_SIZE];
int rear;
int front;
public:
Queue() {
rear = 0;
front = 0;
void i nsert(){
if (rear == MAX_SIZE)
else {
cin>>item;
arr_queue[rear++] = item;
void r emoveData(){
if (front == rear)
front++;
void d isplay(){
};
ain(){
int m
Queue obj;
cin>>choice;
switch (choice) {
case 1:
obj.insert();
break;
case 2:
obj.removeData();
break;
case 3:
obj.display();
break;
default:
exit_p = 0;
break;
}
} while (exit_p);
return 0;
Output
1.Insert
2.Remove
3.Display
Others to exit
## Position : 1 , InsertValue : 1
Queue Main Menu
1.Insert
2.Remove
3.Display
Others toexit
## Position : 2 , InsertValue : 2
1.Insert
2.Remove
3.Display
Others toexit
1.Insert
2.Remove
3.Display
Others toexit
1.Insert
2.Remove
3.Display
Others toexit
## Position : 1 , Value : 2
## Position : 2 , Value : 3
#include<iostream>
class Node {
public:
int key;
int data;
Node * next;
Node() {
key = 0;
data = 0;
next = NULL;
Node(int k, int d) {
key = k;
data = d;
};
class SinglyLinkedList {
public:
Node * head;
SinglyLinkedList() {
head = NULL;
}
SinglyLinkedList(Node * n) {
head = n;
Node * nodeExists(int k) {
temp = ptr;
return temp;
}
// 2. Append a node to the list
void appendNode(Node * n) {
cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;
} else {
if (head == NULL) {
head = n;
} else {
}
}
void prependNode(Node * n) {
cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;
} else {
head = n;
if (ptr == NULL) {
cout<< "No node exists with key value: " << k <<endl;
} else {
cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;
} else {
void deleteNodeByKey(int k) {
if (head == NULL) {
} else {
temp = currentptr;
currentptr = NULL;
} else {
if (temp != NULL) {
} else {
cout<< "Node Doesn't exist with key value : " << k <<endl;
}
if (ptr != NULL) {
} else {
cout<< "Node Doesn't exist with key value : " << k <<endl;
// 7th printing
void printList() {
if (head == NULL) {
} else {
cout<< "(" << temp - > key << "," << temp - > data << ") --> ";
};
int main() {
SinglyLinkedList s;
int option;
do {
cin>> option;
//Node n1;
switch (option) {
case 0:
break;
case 1:
cout<< "Append Node Operation \nEnter key & data of the Node to be
Appended" <<endl;
cin>> key1;
cin>> data1;
s.appendNode(n1);
//cout<<n1.key<<" = "<<n1.data<<endl;
break;
case 2:
cout<< "Prepend Node Operation \nEnter key & data of the Node to be
Prepended" <<endl;
cin>> key1;
cin>> data1;
s.prependNode(n1);
break;
case 3:
cout<< "Insert Node After Operation \nEnter key of existing Node after
which you want to Insert this New node: " <<endl;
cin>> k1;
cout<< "Enter key & data of the New Node first: " <<endl;
cin>> key1;
cin>> data1;
s.insertNodeAfter(k1, n1);
break;
case 4:
cout<< "Delete Node By Key Operation - \nEnter key of the Node to be
deleted: " <<endl;
cin>> k1;
s.deleteNodeByKey(k1);
break;
case 5:
cout<< "Update Node By Key Operation - \nEnter key & NEW data to
be updated" <<endl;
cin>> key1;
cin>> data1;
s.updateNodeByKey(key1, data1);
break;
case 6:
s.printList();
break;
case 7:
system("cls");
break;
default:
return 0;
Output:
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
1
Node appended
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
7
8
Node already exists with key value? Prepend another node with
different key value
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
Node prepended
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
Enter key of existing node after which you want to insert this new node
Node inserted
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
5
10
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
0
1(b)Doubly Linked List using C++
#include<iostream>
class Node {
public:
int key;
int data;
Node * next;
Node * previous;
Node() {
key = 0;
data = 0;
next = NULL;
previous = NULL;
Node(int k, int d) {
key = k;
data = d;
}
};
class DoublyLinkedList {
public:
Node * head;
DoublyLinkedList() {
head = NULL;
DoublyLinkedList(Node * n) {
head = n;
Node * nodeExists(int k) {
temp = ptr;
return temp;
void appendNode(Node * n) {
cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;
} else {
if (head == NULL) {
head = n;
} else {
void prependNode(Node * n) {
} else {
if (head == NULL) {
head = n;
} else {
head = n;
cout<< "No node exists with key value: " << k <<endl;
} else {
cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;
} else {
if (nextNode == NULL) {
//inserting in between
else {
void deleteNodeByKey(int k) {
if (ptr == NULL) {
cout<< "No node exists with key value: " << k <<endl;
} else {
} else {
if (nextNode == NULL) {
//deleting in between
else {
}
}
if (ptr != NULL) {
} else {
cout<< "Node Doesn't exist with key value : " << k <<endl;
// 7th printing
void printList() {
if (head == NULL) {
} else {
cout<< "(" << temp - > key << "," << temp - > data << ") <--> ";
};
int main() {
DoublyLinkedListobj;
int option;
do {
cin>> option;
//Node n1;
switch (option) {
case 0:
break;
case 1:
cout<< "Append Node Operation \nEnter key & data of the Node to be
Appended" <<endl;
cin>> key1;
cin>> data1;
obj.appendNode(n1);
//cout<<n1.key<<" = "<<n1.data<<endl;
break;
case 2:
cout<< "Prepend Node Operation \nEnter key & data of the Node to be
Prepended" <<endl;
cin>> key1;
cin>> data1;
break;
case 3:
cout<< "Insert Node After Operation \nEnter key of existing Node after
which you want to Insert this New node: " <<endl;
cin>> k1;
cout<< "Enter key & data of the New Node first: " <<endl;
cin>> key1;
cin>> data1;
obj.insertNodeAfter(k1, n1);
break;
case 4:
cout<< "Delete Node By Key Operation - \nEnter key of the Node to be
deleted: " <<endl;
cin>> k1;
obj.deleteNodeByKey(k1);
break;
case 5:
cout<< "Update Node By Key Operation - \nEnter key & NEW data to
be updated" <<endl;
cin>> key1;
cin>> data1;
obj.updateNodeByKey(key1, data1);
break;
case 6:
obj.printList();
break;
case 7:
system("cls");
break;
default:
return 0;
Output:
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
7 8
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
Node prepended
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
Enter key of existing node after which you want to insert this new node
3 88
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
3 8
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
0
1(c) circular linked list program in c++
#include<iostream>
class Node {
public:
int key;
int data;
Node * next;
Node() {
key = 0;
data = 0;
next = NULL;
Node(int k, int d) {
key = k;
data = d;
};
class CircularLinkedList {
public:
Node * head;
CircularLinkedList() {
head = NULL;
Node * nodeExists(int k) {
if (ptr == NULL) {
return temp;
} else {
do {
if (ptr - > key == k) {
temp = ptr;
return temp;
//return temp;
endl;
} else {
if (head == NULL) {
head = new_node;
} else {
endl;
} else {
if (head == NULL) {
head = new_node;
} else {
head = new_node;
cout<< "Node Prepended" <<endl;
if (ptr == NULL) {
cout<< "No node exists with key value OF: " << k <<endl;
} else {
endl;
} else {
} else {
void deleteNodeByKey(int k) {
if (ptr == NULL) {
cout<< "No node exists with key value OF : " << k <<
endl;
} else {
if (ptr == head) {
head = NULL;
} else {
} else {
temp = currentptr;
currentptr = NULL;
} else {
if (ptr != NULL) {
} else {
cout<< "Node Doesn't exist with key value : " << k <<endl;
// 7th printing
void printList() {
if (head == NULL) {
} else {
do {
cout<< "(" << temp - > key << "," << temp - > data << "," << temp - >
next << ") --> ";
};
int main() {
CircularLinkedListobj;
int option;
cin>> option;
//Node n1;
switch (option) {
case 0:
break;
case 1:
cout<< "Append Node Operation \nEnter key & data of the Node to be
Appended" <<endl;
cin>> key1;
cin>> data1;
obj.appendNode(n1);
//cout<<n1.key<<" = "<<n1.data<<endl;
break;
case 2:
cout<< "Prepend Node Operation \nEnter key & data of the Node to be
Prepended" <<endl;
cin>> key1;
cin>> data1;
obj.prependNode(n1);
break;
case 3:
cout<< "Insert Node After Operation \nEnter key of existing Node after
which you want to Insert this New node: " <<endl;
cin>> k1;
cout<< "Enter key & data of the New Node first: " <<endl;
cin>> key1;
cin>> data1;
obj.insertNodeAfter(k1, n1);
break;
case 4:
cin>> k1;
obj.deleteNodeByKey(k1);
break;
case 5:
cout<< "Update Node By Key Operation - \nEnter key & NEW data to
be updated" <<endl;
cin>> key1;
cin>> data1;
obj.updateNodeByKey(key1, data1);
break;
case 6:
obj.printList();
break;
case 7:
system("cls");
break;
default:
cout<< "Enter Proper Option number " <<endl;
return 0;
Output:
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
1
Append node operation
5 8
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
6 4
Node prepended
What operation do you want to perform? Select option nunber.Enter 0
to exit
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
Enter key and existing node after which you want to insert this new
node
7 7
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
5 6
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
Head address:0x656b30
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
0
4) a) stack using array
#include<iostream>
class stack
int stk[5];
int top;
public:
stack()
top=-1;
void push(int x)
if(top > 4)
return;
}
stk[++top]=x;
cout<<"inserted"<<x;
void pop()
if(top <0)
return;
void display()
if(top<0)
return;
}
for(int i=top; i>=0; i--)
};
int main()
int ch;
stack st;
while(1)
switch(ch)
st.push(ch);
break;
case 4: exit(0);
return(0);
Output:-
Enter ur choice1
inserted23
inserted45
Enter ur choice3
45 78 23
Enter ur choice2
deleted45
1.push 2.pop 3.display 4.exit
Enter ur choice2
deleted78
Enter ur choice3
23
Enter ur choice4
4) a) stack using array
#include<iostream>
class stack
int stk[5];
int top;
public:
stack()
top=-1;
}
void push(int x)
if(top > 4)
return;
stk[++top]=x;
cout<<"inserted"<<x;
void pop()
if(top <0)
return;
}
void display()
if(top<0)
return;
};
int main()
int ch;
stack st;
while(1)
st.push(ch);
break;
case 3: st.display();break;
case 4: exit(0);
return(0);
Output:-
Enter ur choice1
inserted23
inserted45
Enter ur choice3
45 78 23
Enter ur choice2
deleted45
Enter ur choice2
deleted78
Enter ur choice3
23
Enter ur choice4
2) b) stack using linked list
#include <iostream>
struct Node {
int data;
};
newnode->data = val;
newnode->next = top;
top = newnode;
void pop() {
if(top==NULL)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< top->data <<endl;
top = top->next;
void display() {
if(top==NULL)
cout<<"stack is empty";
else {
ptr = top;
ptr = ptr->next;
cout<<endl;
int main() {
int ch, val;
cout<<"4) Exit"<<endl;
do {
cin>>ch;
switch(ch) {
case 1: {
cin>>val;
push(val);
break;
case 2: {
pop();
break;
}
case 3: {
display();
break;
case 4: {
cout<<"Exit"<<endl;
break;
default: {
cout<<"Invalid Choice"<<endl;
}while(ch!=4);
return 0;
Output:-
1) Push in stack
2) Pop from stack
3) Display stack
4) Exit
Enter choice:
Enter choice:
Enter choice:
Enter choice:
Enter choice:
2
Enter choice:
stack is empty
Enter choice:
Exit
12) Implementation of operations on AVL trees.
#include<iostream>
#include<cstdio>
#include<sstream>
#include<algorithm>
#define pow2(n) (1 << (n))
using namespace std;
struct avl {
int d;
struct avl *l;
struct avl *r;
}*r;
class avl_tree {
public:
int height(avl *);
int difference(avl *);
avl *rr_rotat(avl *);
avl *ll_rotat(avl *);
avl *lr_rotat(avl*);
avl *rl_rotat(avl *);
avl * balance(avl *);
avl * insert(avl*, int);
void show(avl*, int);
void inorder(avl *);
void preorder(avl *);
void postorder(avl*);
avl_tree() {
r = NULL;
}
};
int avl_tree::height(avl *t) {
int h = 0;
if (t != NULL) {
int l_height = height(t->l);
int r_height = height(t->r);
int max_height = max(l_height, r_height);
h = max_height + 1;
}
return h;
}
int avl_tree::difference(avl *t) {
int l_height = height(t->l);
int r_height = height(t->r);
int b_factor = l_height - r_height;
return b_factor;
}
avl *avl_tree::rr_rotat(avl *parent) {
avl *t;
t = parent->r;
parent->r = t->l;
t->l = parent;
cout<<"Right-Right Rotation";
return t;
}
avl *avl_tree::ll_rotat(avl *parent) {
avl *t;
t = parent->l;
parent->l = t->r;
t->r = parent;
cout<<"Left-Left Rotation";
return t;
}
avl *avl_tree::lr_rotat(avl *parent) {
avl *t;
t = parent->l;
parent->l = rr_rotat(t);
cout<<"Left-Right Rotation";
return ll_rotat(parent);
}
avl *avl_tree::rl_rotat(avl *parent) {
avl *t;
t = parent->r;
parent->r = ll_rotat(t);
cout<<"Right-Left Rotation";
return rr_rotat(parent);
}
avl *avl_tree::balance(avl *t) {
int bal_factor = difference(t);
if (bal_factor > 1) {
if (difference(t->l) > 0)
t = ll_rotat(t);
else
t = lr_rotat(t);
} else if (bal_factor < -1) {
if (difference(t->r) > 0)
t = rl_rotat(t);
else
t = rr_rotat(t);
}
return t;
}
avl *avl_tree::insert(avl *r, int v) {
if (r == NULL) {
r = new avl;
r->d = v;
r->l = NULL;
r->r = NULL;
return r;
} else if (v< r->d) {
r->l = insert(r->l, v);
r = balance(r);
} else if (v >= r->d) {
r->r = insert(r->r, v);
r = balance(r);
} return r;
}
void avl_tree::show(avl *p, int l) {
int i;
if (p != NULL) {
show(p->r, l+ 1);
cout<<" ";
if (p == r)
cout << "Root -> ";
for (i = 0; i < l&& p != r; i++)
cout << " ";
cout << p->d;
show(p->l, l + 1);
}
}
void avl_tree::inorder(avl *t) {
if (t == NULL)
return;
inorder(t->l);
cout << t->d << " ";
inorder(t->r);
}
void avl_tree::preorder(avl *t) {
if (t == NULL)
return;
cout << t->d << " ";
preorder(t->l);
preorder(t->r);
}
void avl_tree::postorder(avl *t) {
if (t == NULL)
return;
postorder(t ->l);
postorder(t ->r);
cout << t->d << " ";
}
int main() {
int c, i;
avl_tree avl;
while (1) {
cout<<"menu options"<<endl;
cout << "1.Insert Element into the tree" << endl;
cout << "2.show Balanced AVL Tree" << endl;
cout << "3.InOrder traversal" << endl;
cout << "4.PreOrder traversal" << endl;
cout << "5.PostOrder traversal" << endl;
cout << "6.Exit" << endl;
cout << "Enter your Choice: ";
cin >> c;
switch (c) {
case 1:
cout << "Enter value to be inserted: ";
cin >> i;
r = avl.insert(r, i);
break;
case 2:
if (r == NULL) {
cout << "Tree is Empty" << endl;
continue;
}
cout << "Balanced AVL Tree:" << endl;
avl.show(r, 1);
cout<<endl;
break;
case 3:
cout << "Inorder Traversal:" << endl;
avl.inorder(r);
cout << endl;
break;
case 4:
cout << "Preorder Traversal:" << endl;
avl.preorder(r);
cout << endl;
break;
case 5:
cout << "Postorder Traversal:" << endl;
avl.postorder(r);
cout << endl;
break;
case 6:
exit(1);
break;
default:
cout << "Wrong Choice" << endl;
}
}
return 0;
}
Output:-
menu options
3.InOrder traversal
4.PreOrder traversal
5.PostOrder traversal
6.Exit
3.InOrder traversal
4.PreOrder traversal
5.PostOrder traversal
6.Exit
4.PreOrder traversal
5.PostOrder traversal
6.Exit
3.InOrder traversal
4.PreOrder traversal
5.PostOrder traversal
6.Exit
3.InOrder traversal
4.PreOrder traversal
5.PostOrder traversal
6.Exit
Enter your Choice: 1
3.InOrder traversal
4.PreOrder traversal
5.PostOrder traversal
6.Exit
3.InOrder traversal
4.PreOrder traversal
5.PostOrder traversal
6.Exit
4.PreOrder traversal
5.PostOrder traversal
6.Exit
60 50 40 Root -> 40 30 20 10
3.InOrder traversal
4.PreOrder traversal
5.PostOrder traversal
6.Exit
Inorder Traversal:
10 20 30 40 40 50 60
3.InOrder traversal
4.PreOrder traversal
5.PostOrder traversal
6.Exit
Preorder Traversal:
40 20 10 30 50 40 60
3.InOrder traversal
4.PreOrder traversal
5.PostOrder traversal
6.Exit
Postorder Traversal:
10 30 20 40 60 50 40
3.InOrder traversal
4.PreOrder traversal
5.PostOrder traversal
6.Exit
Enter your Choice: 6
1) Program for binary search
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int n,i,arr[50],search,first,last,middle;
cin>>n;
cout<<"Enter"<<n<<"number:";
for(i=0;i<n;i++)
cin>>arr[i];
cin>>search;
first=0;
last=n-1;
middle=(first+last)/2;
while(first<=last)
if(arr[middle]<search)
first=middle+1;
else if(arr[middle]==search)
cout<<search<<"found at location"<<middle+1<<"\n";
break;
else
last=middle-1;
middle=(first+last)/2;
}
if(first>last)
getch();
OUTPUT:-
Enter 5 number:
12 23 34 45 56
34 found at location 3
2.implementation of binary search tree
#include<iostream.h>
class BST
Struct node
int data;
node* left;
node* right;
};
node* root;
node*makeEmpty(node* t)
if(t==NULL)
return NULL;
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
Return NULL;
if(t==NULL)
t=new node;
t->data=x;
t->left=t->right=NULL;
else if(x<t->data)
t->left=insert(x,t->left);
else if(x>t->data)
t->right=insert(x,t->right);
return t;
node* findMin(node* t)
if(t==NULL)
return NULL;
else if(t->left==NULL)
return t;
else
return findMin(t->left);
node* findMax(node* t)
if(t==NULL)
return NULL;
else if(t->right==NULL)
return t;
else
return findMax(t->right);
node* temp;
if(t==NULL)
return NULL;
else if(x<t->data)
t->left=remove(x,t->left);
else if(x<t->data)
t->right=remove(x,t->right);
else if(t->left&&t->right)
temp=findMin(t->right);
t->data=temp->data;
t->right=remove(t->data,t->right);
else
temp=t;
if(t->left==NULL)
t=t->right;
else if(t->right==NULL)
t=t->left;
delete temp;
return t;
void inorder(node* t)
if(t==NULL)
return;
inorder(t->left);
cout<<t->data<<" ";
inorder(t->right);
if(t==NULL)
return NULL;
else if(x<t->data)
return find(t->left,x);
else if(x>t->data)
return find(t->right,x);
else
return t;
public:
BST()
root=NULL;
~BST()
root=makeEmpty(root);
Void insert(int x)
root=insert(x,root);
Void remove(int x)
{
root=remove(x,root);
void display()
inorder(root);
cout<<endl;
void search(int x)
root=find(root,x);
};
int main()
BST t;
t.insert(20);
t.insert(25);
t.insert(15);
t.insert(10);
t.insert(30);
t.display();
t.remove(20);
t.display();
t.remove(25);
t.display();
t.remove(30);
t.display();
return 0;
OUTPUT:
10 15 20 25 30
10 15 25 30
10 15 30
10 15
12) Implementation recursive and iterative traversals on binary tree
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct tree_node
tree_node *left;
tree_node *right;
int data;
};
class bst
tree_node *root;
public:
bst()
root=NULL;
}
int isempty()
return(root==NULL);
void inordertrav();
void inorder(tree_node*);
void postordertrav();
void postorder(tree_node*);
void preordertrav();
void preorder(tree_node*);
};
tree_node *parent;
p->data=item;
p->left=NULL;
p->right=NULL;
parent=NULL;
if(isempty())
root=p;
else
tree_node *ptr;
ptr=root;
while(ptr!=NULL)
parent=ptr;
if(item>ptr->data)
ptr=ptr->right;
else
ptr=ptr->left;
if(item<parent->data)
parent->left=p;
else
parent->right=p;
}
void bst::inordertrav()
inorder(root);
if(ptr!=NULL)
inorder(ptr->left);
inorder(ptr->right);
void bst::postordertrav()
postorder(root);
}
void bst ::postorder(tree_node*ptr)
if(ptr!=NULL)
postorder(prt->left);
postorder(ptr->right);
void bst::preordertrav()
preorder(root);
void bst::preorder(tree_node*ptr)
if(ptr!=NULL)
preorder(ptr->left);
preorder(ptr->right);
void main()
bst b;
b.insert(52);
b.insert(25);
b.insert(50);
b.insert(15);
b.insert(40);
b.insert(45);
b.insert(20);
cout<<"inorder"<<endl;
b.inordertrav();
cout<<endl<<"postorder"<<endl;
b.postordertrav();
cout<<endl<<"preorder"<<endl;
b.preordertrav();
getch();
OUTPUT:-
Inorder
15 20 25 40 45 50 52
Postorder
20 15 45 40 50 25 52
Preorder
52 25 15 20 50 40 45
1(b)Doubly Linked List using C++
#include<iostream>
class Node {
public:
int key;
int data;
Node * next;
Node * previous;
Node() {
key = 0;
data = 0;
next = NULL;
previous = NULL;
Node(int k, int d) {
key = k;
data = d;
};
class DoublyLinkedList {
public:
Node * head;
DoublyLinkedList() {
head = NULL;
DoublyLinkedList(Node * n) {
head = n;
Node * nodeExists(int k) {
return temp;
void appendNode(Node * n) {
cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;
} else {
if (head == NULL) {
head = n;
} else {
Node * ptr = head;
void prependNode(Node * n) {
cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;
} else {
if (head == NULL) {
head = n;
cout<< "Node Prepended as Head Node" <<endl;
} else {
head = n;
if (ptr == NULL) {
cout<< "No node exists with key value: " << k <<endl;
} else {
} else {
if (nextNode == NULL) {
//inserting in between
else {
void deleteNodeByKey(int k) {
if (ptr == NULL) {
cout<< "No node exists with key value: " << k <<endl;
} else {
} else {
if (nextNode == NULL) {
//deleting in between
else {
}
// 6th update node
if (ptr != NULL) {
} else {
cout<< "Node Doesn't exist with key value : " << k <<endl;
// 7th printing
void printList() {
if (head == NULL) {
} else {
cout<< "(" << temp - > key << "," << temp - > data << ") <--> ";
};
int main() {
DoublyLinkedListobj;
int option;
do {
cout<< "\nWhat operation do you want to perform? Select Option
number. Enter 0 to exit." <<endl;
cin>> option;
//Node n1;
switch (option) {
case 0:
break;
case 1:
cout<< "Append Node Operation \nEnter key & data of the Node to be
Appended" <<endl;
cin>> key1;
cin>> data1;
obj.appendNode(n1);
//cout<<n1.key<<" = "<<n1.data<<endl;
break;
case 2:
cout<< "Prepend Node Operation \nEnter key & data of the Node to be
Prepended" <<endl;
cin>> key1;
cin>> data1;
obj.prependNode(n1);
break;
case 3:
cout<< "Insert Node After Operation \nEnter key of existing Node after
which you want to Insert this New node: " <<endl;
cin>> k1;
cout<< "Enter key & data of the New Node first: " <<endl;
cin>> key1;
cin>> data1;
obj.insertNodeAfter(k1, n1);
break;
case 4:
cin>> k1;
obj.deleteNodeByKey(k1);
break;
case 5:
cout<< "Update Node By Key Operation - \nEnter key & NEW data to
be updated" <<endl;
cin>> key1;
cin>> data1;
obj.updateNodeByKey(key1, data1);
break;
case 6:
obj.printList();
break;
case 7:
system("cls");
break;
default:
cout<< "Enter Proper Option number " <<endl;
return 0;
Output:
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
1
Append node operation
7 8
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
80
Node prepended
What operation do you want to perform? Select option nunber.Enter 0
to exit
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
Enter key of existing node after which you want to insert this new node
3 88
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
5
3 8
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
0
9(a) C++ program to implement Selection sort using class
#include <iostream.h>
constant MAX = 10 ;
class array
private :
intarr[MAX] ;
int count ;
public :
array( ) ;
void sort( ) ;
void display( ) ;
};
array :: array( )
count = 0 ;
arr[i] = 0 ;
arr[count] = item ;
count++ ;
else
int temp ;
if ( arr[i] >arr[j] )
temp = arr[i] ;
arr[i] = arr[j] ;
arr[j] = temp ;
void main( )
array a ;
a.add ( 25 ) ;
a.add ( 17 ) ;
a.add ( 31 ) ;
a.add ( 13 ) ;
a.add ( 2 ) ;
a.display( ) ;
a.sort( ) ;
a.display( ) ;
}
Output:
25 17 31 13 2
2 13 17 25 31
9(c)Merge sort program in c++#include <iostream.h>
const int MAX = 5 ;
class array
{
private :
int *a ;
int size ;
int count ;
public :
array( ) ;
array ( int sz ) ;
void add ( int num ) ;
void display( ) ;
void merge_sort(int low,int high);
void merge(int low,int mid,int high);
~array( ) ;
};
array :: array( )
{
count = size = 0 ;
a = NULL ;
}
array :: array( int sz )
{
count = 0 ;
size = sz ;
a = new int[sz] ;
}
void array :: add ( int num )
{
if ( count < size )
{
a[count] = num ;
count++ ;
}
else
cout << "\nArray is full" << endl ;
}
void array :: display( )
{
for ( int i = 0 ; i < count ; i++ )
cout << a[i] << "\t" ;
cout << endl ;
}
void array :: merge_sort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
void array :: merge(int low,int mid,int high)
{
int h,i,j,b[50],k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++) a[k]=b[k];
}
array :: ~array( )
{
delete a ;
}
int main( )
{
array a ( MAX ) ;
a.add ( 11 ) ;
a.add ( 2 ) ;
a.add ( 9 ) ;
a.add ( 13 ) ;
a.add ( 57 ) ;
cout << "\nMerge sort.\n" ;
a.merge_sort (0,4) ;
Output:
Merge sort.
#include<iostream>
class Node {
public:
int key;
int data;
Node * next;
Node() {
key = 0;
data = 0;
next = NULL;
Node(int k, int d) {
key = k;
data = d;
};
class SinglyLinkedList {
public:
Node * head;
SinglyLinkedList() {
head = NULL;
SinglyLinkedList(Node * n) {
head = n;
Node * nodeExists(int k) {
temp = ptr;
return temp;
void appendNode(Node * n) {
cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;
} else {
if (head == NULL) {
head = n;
} else {
void prependNode(Node * n) {
cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;
} else {
n - > next = head;
head = n;
if (ptr == NULL) {
cout<< "No node exists with key value: " << k <<endl;
} else {
cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;
} else {
void deleteNodeByKey(int k) {
if (head == NULL) {
} else {
temp = currentptr;
currentptr = NULL;
} else {
if (temp != NULL) {
} else {
cout<< "Node Doesn't exist with key value : " << k <<endl;
if (ptr != NULL) {
} else {
cout<< "Node Doesn't exist with key value : " << k <<endl;
// 7th printing
void printList() {
if (head == NULL) {
} else {
};
int main() {
SinglyLinkedList s;
int option;
do {
cin>> option;
//Node n1;
switch (option) {
case 0:
break;
case 1:
cout<< "Append Node Operation \nEnter key & data of the Node to be
Appended" <<endl;
cin>> key1;
cin>> data1;
s.appendNode(n1);
//cout<<n1.key<<" = "<<n1.data<<endl;
break;
case 2:
cout<< "Prepend Node Operation \nEnter key & data of the Node to be
Prepended" <<endl;
cin>> key1;
cin>> data1;
s.prependNode(n1);
break;
case 3:
cout<< "Insert Node After Operation \nEnter key of existing Node after
which you want to Insert this New node: " <<endl;
cin>> k1;
cout<< "Enter key & data of the New Node first: " <<endl;
cin>> key1;
cin>> data1;
s.insertNodeAfter(k1, n1);
break;
case 4:
cin>> k1;
s.deleteNodeByKey(k1);
break;
case 5:
cout<< "Update Node By Key Operation - \nEnter key & NEW data to
be updated" <<endl;
cin>> key1;
cin>> data1;
s.updateNodeByKey(key1, data1);
break;
case 6:
s.printList();
break;
case 7:
system("cls");
break;
default:
Output:
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
Node appended
What operation do you want to perform? Select option nunber.Enter 0
to exit
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
Node already exists with key value? Prepend another node with
different key value
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
Node prepended
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
Enter key of existing node after which you want to insert this new node
Node inserted
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
4
Delete node by key operation
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
10
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
#include<iostream>
class Node {
public:
int key;
int data;
Node * next;
Node() {
key = 0;
data = 0;
next = NULL;
Node(int k, int d) {
key = k;
data = d;
};
class CircularLinkedList {
public:
Node * head;
CircularLinkedList() {
head = NULL;
Node * nodeExists(int k) {
Node * temp = NULL;
if (ptr == NULL) {
return temp;
} else {
do {
temp = ptr;
return temp;
//return temp;
}
// 2. Append a node to the list
endl;
} else {
if (head == NULL) {
head = new_node;
} else {
endl;
} else {
if (head == NULL) {
head = new_node;
} else {
head = new_node;
if (ptr == NULL) {
cout<< "No node exists with key value OF: " << k <<endl;
} else {
if (nodeExists(new_node - > key) != NULL) {
endl;
} else {
} else {
}
// 5. Delete node by unique key
void deleteNodeByKey(int k) {
if (ptr == NULL) {
cout<< "No node exists with key value OF : " << k <<
endl;
} else {
if (ptr == head) {
head = NULL;
} else {
} else {
temp = currentptr;
currentptr = NULL;
} else {
if (ptr != NULL) {
} else {
cout<< "Node Doesn't exist with key value : " << k <<endl;
}
// 7th printing
void printList() {
if (head == NULL) {
} else {
do {
cout<< "(" << temp - > key << "," << temp - > data << "," << temp - >
next << ") --> ";
}
};
int main() {
CircularLinkedListobj;
int option;
do {
cin>> option;
Node * n1 = new Node();
//Node n1;
switch (option) {
case 0:
break;
case 1:
cout<< "Append Node Operation \nEnter key & data of the Node to be
Appended" <<endl;
cin>> key1;
cin>> data1;
obj.appendNode(n1);
//cout<<n1.key<<" = "<<n1.data<<endl;
break;
case 2:
cout<< "Prepend Node Operation \nEnter key & data of the Node to be
Prepended" <<endl;
cin>> key1;
cin>> data1;
obj.prependNode(n1);
break;
case 3:
cout<< "Insert Node After Operation \nEnter key of existing Node after
which you want to Insert this New node: " <<endl;
cin>> k1;
cout<< "Enter key & data of the New Node first: " <<endl;
cin>> key1;
cin>> data1;
break;
case 4:
cin>> k1;
obj.deleteNodeByKey(k1);
break;
case 5:
cout<< "Update Node By Key Operation - \nEnter key & NEW data to
be updated" <<endl;
cin>> key1;
cin>> data1;
obj.updateNodeByKey(key1, data1);
break;
case 6:
obj.printList();
break;
case 7:
system("cls");
break;
default:
return 0;
Output:
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
5 8
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
6 4
Node prepended
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
7 7
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
5
Node unlinked with key value:5
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
5 6
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
Head address:0x656b30
1.appendnode()
2.prependnode()
3.insertnodeafter()
4.deletenodebykey()
5.updatenodebykey()
6.print()
7.clearscreen
0
9(d) Quick sort program in c++#include<iostream.h>
constant MAX = 10 ;
class array
{
private :
intarr[MAX] ;
int count ;
public :
array( ) ;
void add ( int item ) ;
int getcount( ) ;
static int split ( int *, int, int ) ;
void quicksort ( int lower, int upper ) ;
void display( ) ;
};
array :: array( )
{
count = 0 ;
for ( inti = 0 ; i< MAX ; i++ )
arr[i] = 0 ;
}
void array :: add ( int item )
{
if ( count < MAX )
{
arr[count] = item ;
count++ ;
}
else
cout<< "\nArray is full" <<endl ;
}
int array :: getcount( )
{
return count ;
}
void array :: quiksort ( int lower, int upper )
{
if ( upper > lower )
{
inti = split ( arr, lower, upper ) ;
quicksort ( lower, i - 1 ) ;
quicksort ( i + 1, upper ) ;
}
}
int array :: split ( int *a, int lower, int upper )
{
inti, p, q, t ;
p = lower + 1 ;
q = upper ;
i = a[lower] ;
while ( q >= p )
{
while ( a[p] <i )
p++ ;
while ( a[q] >i )
q-- ;
if ( q > p )
{
t = a[p] ;
a[p] = a[q] ;
a[q] = t ;
}
}
t = a[lower] ;
a[lower] = a[q] ;
a[q] = t ;
return q ;
}
void array :: display( )
{
for ( inti = 0 ; i< count ; i++ )
cout<<arr[i] << " " ;
cout<<endl ;
}
int main( )
{
array a ;
a.add ( 11 ) ;
a.add ( 2 ) ;
a.add ( 9 ) ;
a.add ( 13 ) ;
a.add ( 57 ) ;
a.add ( 25 ) ;
a.add ( 17 ) ;
a.add ( 1 ) ;
a.add ( 90 ) ;
a.add ( 3 ) ;
cout<< "\nQuik sort.\n" ;
cout<< "\nArray before sorting:" <<endl ;
a.display( ) ;
int c = a.getcount( ) ;
a.quicksort ( 0, c - 1 ) ;
cout<< "\nArray after quick sorting:" <<endl ;
a.display( ) ;
return 0;
}
Output :
Quik sort.
#include <iostream>
#include <list>
#include <memory>
class Graph
int _V;
bool _directed;
public:
};
Graph::Graph(int V, bool directed) : adj(new std::list<int>[V])
_V = V;
_directed = directed;
adjacency[v].push_back(w);
if (!_directed)
adjacency[w].push_back(v);
void Graph::BreadthFirstSearch(int s)
{
visited[i] = false;
std::list<int> queue;
visited[s] = true;
queue.push_back(s);
std::list<int>::iterator i;
while(!queue.empty())
s = queue.front();
// and enqueue it
if(!visited[*i])
visited[*i] = true;
queue.push_back(*i);
int main()
g.AddEdge(0, 2);
g.AddEdge(0, 3);
g.AddEdge(1, 0);
g.AddEdge(1, 5);
g.AddEdge(2, 5);
g.AddEdge(3, 0);
g.AddEdge(3, 4);
g.AddEdge(4, 6);
g.AddEdge(5, 1);
g.AddEdge(6, 5);
g.BreadthFirstSearch(2);
return 0;
Output:-
Breadth First Traversal from vertex 2:
2510346
#include <iostream>
#include <list>
#include <memory>
class Graph
private:
int _V;
bool _directed;
public:
};
_V = V;
_directed = directed;
}
void Graph::AddEdge(int v, int w)
adjacency[v].push_back(w);
if (!_directed)
adjacency[w].push_back(v);
visited[v] = true;
if (!visited[*i])
DFSUtil(*i, visited);
void Graph::DepthFirstSearch(int v)
visited[i] = false;
DFSUtil(v, visited.get());
}
int main()
g.AddEdge(0, 1);
g.AddEdge(0, 2);
g.AddEdge(0, 3);
g.AddEdge(1, 0);
g.AddEdge(1, 5);
g.AddEdge(2, 5);
g.AddEdge(3, 0);
g.AddEdge(3, 4);
g.AddEdge(4, 6);
g.AddEdge(5, 1);
g.AddEdge(6, 5);
g.DepthFirstSearch(2);
return 0;
Output:-
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout<<"stack over flow";
return;
}
stk[++top]=x;
cout<<"inserted"<<x;
}
void pop()
{
if(top <0)
{ cout <<"stack under flow";
return;
}
cout<<"deleted" <<stk [top--];
}
void display()
{
if(top<0)
{
cout <<"stack empty";
return;
}
for(int i=top; i>=0; i--)
cout<<stk[i] <<" ";
}
};
int main()
{
int ch;
stack st;
while(1)
{
cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return(0);
}
Output:-
Enter ur choice1
inserted23
inserted45
Enter ur choice3
45 78 23
Enter ur choice2
deleted45
Enter ur choice2
deleted78
Enter ur choice3
23
Enter ur choice4
4) b) stack using linked list
#include <iostream>
struct Node {
int data;
};
newnode->data = val;
newnode->next = top;
top = newnode;
void pop() {
if(top==NULL)
cout<<"Stack Underflow"<<endl;
else {
void display() {
if(top==NULL)
cout<<"stack is empty";
else {
ptr = top;
ptr = ptr->next;
cout<<endl;
int main() {
cout<<"4) Exit"<<endl;
do {
cin>>ch;
switch(ch) {
case 1: {
cin>>val;
push(val);
break;
case 2: {
pop();
break;
case 3: {
display();
break;
case 4: {
cout<<"Exit"<<endl;
break;
default: {
cout<<"Invalid Choice"<<endl;
}while(ch!=4);
return 0;
Output:-
1) Push in stack
4) Exit
Enter choice:
Enter choice:
Enter choice:
Enter choice:
Enter choice:
2
The popped element is 2
Enter choice:
stack is empty
Enter choice:
Exit
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
class HashTableEntry{
public:
int k;
int v;
this->k= k;
this->v = v;
};
class HashMapTable{
private:
HashTableEntry**t;
public:
HashMapTable(){
t =new HashTableEntry*[T_S];
t[i]= NULL;
return k % T_S;
int h =HashFunc(k);
h =HashFunc(h +1);
if(t[h]!= NULL)
delete t[h];
}
int SearchKey(int k){
int h =HashFunc(k);
h =HashFunc(h +1);
if(t[h]== NULL)
return-1;
else
return t[h]->v;
int h =HashFunc(k);
while(t[h]!= NULL){
if(t[h]->k == k)
break;
h =HashFunc(h +1);
if(t[h]== NULL){
}else{
delete t[h];
cout<<"Element Deleted"<<endl;
~HashMapTable(){
for(inti=0;i< T_S;i++){
if(t[i]!= NULL)
delete t[i];
delete[] t;
};
int main(){
HashMapTable hash;
int k, v;
int c;
while(1){
cout<<"1.Insert element into the table"<<endl;
cout<<"4.Exit"<<endl;
cin>>c;
switch(c){
case1:
cin>>v;
cin>>k;
hash.Insert(k, v);
break;
case2:
cin>>k;
if(hash.SearchKey(k)==-1){
}else{
cout<<hash.SearchKey(k)<<endl;
break;
case3:
cin>>k;
hash.Remove(k);
break;
case4:
exit(1);
default:
return0;
}
Output
4.Exit
4.Exit
4.Exit
Enter your choice: 1
4.Exit
4.Exit
4.Exit
Element at key 6 : 7
4.Exit
Element Deleted
4.Exit
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
using namespace std;
constant T_S =200;
class HashTableEntry{
public:
int k;
int v;
HashTableEntry(int k,int v){
this->k= k;
this->v = v;
}
};
class HashMapTable{
private:
HashTableEntry**t;
public:
HashMapTable(){
t =newHashTableEntry*[T_S];
for(inti=0;i< T_S;i++){
t[i]= NULL;
}
}
int HashFunc(int k){
return k % T_S;
}
void Insert(int k,int v){
int h =HashFunc(k);
while(t[h]!= NULL && t[h]->k != k){
h =HashFunc(h +1);
}
if(t[h]!= NULL)
delete t[h];
t[h]=newHashTableEntry(k, v);
}
int SearchKey(int k){
int h =HashFunc(k);
while(t[h]!= NULL && t[h]->k != k){
h =HashFunc(h +1);
}
if(t[h]== NULL)
return-1;
else
return t[h]->v;
}
voidRemove(int k){
int h =HashFunc(k);
while(t[h]!= NULL){
if(t[h]->k == k)
break;
h =HashFunc(h +1);
}
if(t[h]== NULL){
cout<<"No Element found at key "<<k<<endl;
return;
}else{
delete t[h];
}
cout<<"Element Deleted"<<endl;
}
~HashMapTable(){
for(inti=0;i< T_S;i++){
if(t[i]!= NULL)
delete t[i];
delete[] t;
}
}
};
int main(){
HashMapTable hash;
int k, v;
int c;
while(1){
cout<<"1.Insert element into the table"<<endl;
cout<<"2.Search element from the key"<<endl;
cout<<"3.Delete element at a key"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your choice: ";
cin>>c;
switch(c){
case1:
cout<<"Enter element to be inserted: ";
cin>>v;
cout<<"Enter key at which element to be inserted: ";
cin>>k;
hash.Insert(k, v);
break;
case2:
cout<<"Enter key of the element to be searched: ";
cin>>k;
if(hash.SearchKey(k)==-1){
cout<<"No element found at key "<<k<<endl;
continue;
}else{
cout<<"Element at key "<<k<<" : ";
cout<<hash.SearchKey(k)<<endl;
}
break;
case3:
cout<<"Enter key of the element to be deleted: ";
cin>>k;
hash.Remove(k);
break;
case4:
exit(1);
default:
cout<<"\nEnter correct option\n";
}
}
return0;
}
Output
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 1
Enter key at which element to be inserted: 1
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 2
Enter key at which element to be inserted: 2
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 4
Enter key at which element to be inserted: 5
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 7
Enter key at which element to be inserted: 6
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 2
Enter key of the element to be searched: 7
No element found at key 7
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 2
Enter key of the element to be searched: 6
Element at key 6 : 7
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 3
Enter key of the element to be deleted: 1
Element Deleted
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 4
19)Write a program on implementation of heap sort
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
Const int MAX = 10 ;
class array
{
private :
intarr[MAX] ;
int count ;
public :
array( ) ;
void add ( intnum ) ;
void makeheap(int ) ;
void heapsort( ) ;
void display( ) ;
};
array :: array( )
{
count = 0 ;
for ( inti = 0 ; i< MAX ; i++ )
arr[MAX] = 0 ;
}
void array :: add ( intnum )
{
if ( count < MAX )
{
arr[count] = num ;
count++ ;
}
else
cout<< "\nArray is full" <<endl ;
}
void array :: makeheap(int c)
{
}
}
void array :: display( )
{
for ( inti = 0 ; i< count ; i++ )
cout<<arr[i] << "\t" ;
cout<<endl ;
}
void main( )
{
array a ;
a.add ( 11 ) ;
a.add ( 2 ) ;
a.add ( 9 ) ;
a.add ( 13 ) ;
a.add ( 57 ) ;
a.add ( 25 ) ;
a.add ( 17 ) ;
a.add ( 1 ) ;
a.add ( 90 ) ;
a.add ( 3 ) ;
a.makeheap(10) ;
cout<< "\nHeap Sort.\n" ;
cout<< "\nBefore Sorting:\n" ;
a.display( ) ;
a.heapsort( ) ;
cout<< "\nAfter Sorting:\n" ;
a.display( ) ;
getch();
}
Output :
Heap sort.
Before sorting:
90 57 25 13 11 9 17 1 2 3
After sorting :
1 2 3 9 11 13 17 25 57 90
16) Write a program to implement shell sort.
#include <iostream.h>
gap = size /2 ;
do
do
swap =0;
temp = a[i] ;
a[ i+ gap]= temp;
swap=1;
}}
gap= gap/2 ;
Intmain()
intarr[10],i,k;
cout<<"Enter 10 values\n";
for(i=0;i<10;i++)
cin>>arr[i];
shell_sort(arr,10);
cout<<endl<<arr[i];
return 0 ;
Output:
Enter 10 values
44
12
45
34
Sorted values
2
3
12
34
44
45
24) Implementation of B-tree
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node{
int value;
node *left;
node *right;
};
class btree
public:
btree();
~btree();
void destroy_tree();
void inorder_print();
void postorder_print();
void preorder_print();
private:
node *root;
};
btree::btree(){
root=NULL;
btree::~btree(){
destroy_tree();
}
if(leaf != NULL){
destroy_tree(leaf->left);
destroy_tree(leaf->right);
delete leaf;
if(leaf->left != NULL){
insert(key , leaf->left);
else
leaf->left->value = key;
leaf->left->left = NULL;
leaf->left->right = NULL;
}
if(leaf->right != NULL){
insert(key, leaf->right);
else
leaf->right->value = key;
leaf->right->right = NULL;
leaf->right->left = NULL;
if(root != NULL){
insert(key, root);
}else{
root = new node;
root->value = key;
root->left = NULL;
root->right = NULL;
if(leaf != NULL){
if(key == leaf->value){
return leaf;
}else{
}else{
return NULL;
}
}
void btree::destroy_tree(){
destroy_tree(root);
void btree::inorder_print(){
inorder_print(root);
if(leaf != NULL){
inorder_print(leaf->left);
inorder_print(leaf->right);
void btree::postorder_print()
{
postorder_print(root);
if(leaf != NULL){
inorder_print(leaf->left);
inorder_print(leaf->right);
void btree::preorder_print(){
preorder_print(root);
if(leaf != NULL){
inorder_print(leaf->right);
void main(){
//btree tree;
clrscr();
tree->insert(10);
tree->insert(6);
tree->insert(14);
tree->insert(5);
tree->insert(8);
tree->insert(11);
tree->insert(18);
tree->inorder_print();
tree->preorder_print();
tree->postorder_print();
delete tree;
getch();
Output:-
5,6,8,10,11,14,18,
10,5,6,8,11,14,18,
5,6,8,11,14,18,10,