C++ Data Structures: Stack & Queue Programs
C++ Data Structures: Stack & Queue Programs
#include <iostream.h>
Output
1) Push in stack
2) Pop from stack
3) Display stack
4) Exit
Enter choice: 1
Enter value to be pushed: 2
b) QUEUE
#include <iostream>
using namespace std;
2
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
int queue[100], n = 100, front = - 1, rear = - 1;
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}
}
void Delete() {
if (front == - 1 || front > rear) {
cout<<"Queue Underflow ";
return ;
} else {
cout<<"Element deleted from queue is : "<< queue[front] <<endl;
front++;;
}
}
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
int main() {
int ch;
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
3
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}
#include <iostream>
using namespace std;
int cqueue[5];
int front = -1, rear = -1, n=5;
4
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
void insertCQ(int val) {
if ((front == 0 && rear == n-1) || (front == rear+1)) {
cout<<"Queue Overflow \n";
return;
}
if (front == -1) {
front = 0;
rear = 0;
} else {
if (rear == n - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue[rear] = val ;
}
void deleteCQ() {
if (front == -1) {
cout<<"Queue Underflow\n";
return ;
}
cout<<"Element deleted from queue is : "<<cqueue[front]<<endl;
if (front == rear) {
front = -1;
rear = -1;
} else {
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void displayCQ() {
int f = front, r = rear;
if (front == -1) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are :\n";
if (f <= r) {
5
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
while (f <= r){
cout<<cqueue[f]<<" ";
f++;
}
} else {
while (f <= n - 1) {
cout<<cqueue[f]<<" ";
f++;
}
f = 0;
while (f <= r) {
cout<<cqueue[f]<<" ";
f++;
}
}
cout<<endl;
}
int main() {
case 2:
deleteCQ();
break;
case 3:
displayCQ();
break;
6
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
case 4:
cout<<"Exit\n";
break;
default: cout<<"Incorrect!\n";
}
} while(ch != 4);
return 0;
}
Output
1)Insert
2)Delete
3)Display
4)Exit
Enter choice : 1
Input for insertion:
Enter choice : 1
Enter choice : 2
Enter choice : 3
Queue elements are :
796
Enter choice : 4
Exit
3. Write C++ programs to implement the following using a single linked list. a) Stack ADT
b) Queue ADT
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* top = NULL;
7
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
void push(int val) {
struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));
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() {
struct Node* ptr;
if(top==NULL)
cout<<"stack is empty";
else {
ptr = top;
cout<<"Stack elements are: ";
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
cout<<endl;
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
8
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
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;
}
4. Write a C++ program to implement Circular queue using Single linked list.
#include <iostream>
using namespace std;
struct node {
int data;
struct node *next;
};
struct node* front = NULL;
struct node* rear = NULL;
struct node* temp;
void Insert() {
int val;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
if (rear == NULL) {
rear = (struct node *)malloc(sizeof(struct node));
rear->next = NULL;
9
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
rear->data = val;
front = rear;
} else {
temp=(struct node *)malloc(sizeof(struct node));
rear->next = temp;
temp->data = val;
temp->next = NULL;
rear = temp;
}
}
void Delete() {
temp = front;
if (front == NULL) {
cout<<"Underflow"<<endl;
return;
}
else
if (temp->next != NULL) {
temp = temp->next;
cout<<"Element deleted from queue is : "<<front->data<<endl;
free(front);
front = temp;
} else {
cout<<"Element deleted from queue is : "<<front->data<<endl;
free(front);
front = NULL;
rear = NULL;
}
}
void Display() {
temp = front;
if ((front == NULL) && (rear == NULL)) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are: ";
while (temp != NULL) {
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
10
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
}
int main() {
int ch;
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}
Output
5. Write a C++ program to implement the double ended queue ADT using double linked
list.
#include <iostream>
using namespace std;
struct Node {
11
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
int data;
struct Node *prev;
struct Node *next;
};
struct Node* head = NULL;
void insert(int newdata) {
struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));
newnode->data = newdata;
newnode->prev = NULL;
newnode->next = head;
if(head != NULL)
head->prev = newnode ;
head = newnode;
}
void display() {
struct Node* ptr;
ptr = head;
while(ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
int main() {
insert(3);
insert(1);
insert(7);
insert(2);
insert(9);
cout<<"The doubly linked list is: ";
display();
return 0;
}
output
#include <iostream>
12
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
using namespace std;
//main program
int main()
{
int n;
return 0;
}
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
13
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
void insert(int,int );
void delte(int);
void display(int);
int search(int);
int search1(int,int);
int tree[40],t=1,s,x,i;
main()
{
int ch,y;
for(i=1;i<40;i++)
tree[i]=-1;
while(1)
{
cout <<"[Link]\[Link]\[Link]\[Link]\[Link]\nEnter your choice:";
cin >> ch;
switch(ch)
{
case 1:
cout <<"enter the element to insert";
cin >> ch;
insert(1,ch);
break;
case 2:
cout <<"enter the element to delete";
cin >>x;
y=search(1);
if(y!=-1) delte(y);
else cout<<"no such element in tree";
break;
case 3:
display(1);
cout<<"\n";
for(int i=0;i<=32;i++)
cout <<i;
cout <<"\n";
break;
case 4:
cout <<"enter the element to search:";
cin >> x;
y=search(1);
14
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
if(y == -1) cout <<"no such element in tree";
else cout <<x << "is in" <<y <<"position";
break;
case 5:
exit(0);
}
}
}
int search(int s)
{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return tree[s];
if(tree[s]>x)
search(2*s);
else if(tree[s]<x)
search(2*s+1);
else
return s;
}
void display(int s)
{
if(t==1)
{cout <<"no element in tree:";
return;}
for(int i=1;i<40;i++)
if(tree[i]==-1)
cout <<" ";
else cout <<tree[i];
return ;
}
8. Write C++ programs for the implementation tree traversal technique A) BFS
#include<iostream.>
#include<queue>
#define NODE 6
using namespace std;
typedef struct node{
int val;
int state; //status
}node;
int graph[NODE][NODE] = {
{0, 1, 1, 1, 0, 0},
{1, 0, 0, 1, 1, 0},
{1, 0, 0, 1, 0, 1},
{1, 1, 1, 0, 1, 1},
{0, 1, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0}
};
void bfs(node *vert, node s){
node u;
int i, j;
queue<node> que;
for(i = 0; i<NODE; i++){
vert[i].state = 0; //not visited
}
vert[[Link]].state = 1;//visited
[Link](s); //insert starting node
while(![Link]()){
u = [Link](); //delete from queue and print
[Link]();
cout << char([Link]+'A') << " ";
for(i = 0; i<NODE; i++){
if(graph[i][[Link]]){
//when the node is non-visited
if(vert[i].state == 0){
17
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
vert[i].state = 1;
[Link](vert[i]);
}
}
}
[Link] = 2;//completed for node u
}
}
int main(){
node vertices[NODE];
node start;
char s;
for(int i = 0; i<NODE; i++){
vertices[i].val = i;
}
s = 'B';//starting vertex B
[Link] = s-'A';
cout << "BFS Traversal: ";
bfs(vertices, start);
cout << endl;
}
Output
BFS Traversal: B A D E C F
9. Write a C++ program that uses recursive functions to traverse a binary search tree. a)
Pre-order b) In-order c) Post-order
#include<iostream>
using namespace std;
struct node {
int data;
struct node *left;
struct node *right;
};
struct node *createNode(int val) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = val;
temp->left = temp->right = NULL;
18
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
return temp;
}
void preorder(struct node *root) {
if (root != NULL) {
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
}
struct node* insertNode(struct node* node, int val) {
if (node == NULL) return createNode(val);
if (val < node->data)
node->left = insertNode(node->left, val);
else if (val > node->data)
node->right = insertNode(node->right, val);
return node;
}
int main() {
struct node *root = NULL;
root = insertNode(root, 4);
insertNode(root, 5);
insertNode(root, 2);
insertNode(root, 9);
insertNode(root, 1);
insertNode(root, 3);
cout<<"Pre-Order traversal of the Binary Search Tree is: ";
preorder(root);
return 0;
}
Output
#include <iostream>
using namespace std;
19
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
class node {
public:
int data;
node* left;
node* right;
};
int height(node* node) {
if (node == NULL)
return 0;
else {
int lDepth = height(node->left);
int rDepth = height(node->right);
if (lDepth > rDepth)
return(lDepth + 1);
else return(rDepth + 1);
}
}
node* insertNode(int data) {
node* Node = new node();
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return(Node);
}
int main() {
node *root = insertNode(4);
root->left = insertNode(5);
root->right = insertNode(0);
root->left->left = insertNode(1);
root->left->right = insertNode(9);
cout<<"The height of the given binary tree is "<<height(root);
return 0;
}
Output
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *left, *right;
Node(int data) {
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
if (root == NULL)
return -100;
int main() {
Output
#Iinclude<iostream.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
int main()
{
int m;
cout <<"Enter no of vertices:";
cin >> n;
cout <<"Enter no of edges:";
cin >> m;
cout <<"\nEDGES \n";
for(k=1; k<=m; k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"Enter initial vertex to traverse from:";
cin >>v;
cout <<"Visitied vertices:";
cout <<v<<" ";
visited[v]=1;
k=1;
while(k<n)
{
22
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
for(j=1; j<=n; j++)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
qu[rare++]=j;
}
v=qu[front++];
cout<<v <<" ";
k++;
visit[v]=0;
visited[v]=1;
}
return 0;
}
13. Write C++ programs to perform the following operations on B-Trees and AVL Trees. a)
Insertion b) Deletion
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);
23
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
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);
24
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
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) {
25
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
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 << "[Link] Element into the tree" << endl;
cout << "[Link] Balanced AVL Tree" << endl;
cout << "[Link] traversal" << endl;
cout << "[Link] traversal" << endl;
cout << "[Link] traversal" << endl;
cout << "[Link]" << endl;
cout << "Enter your Choice: ";
cin >> c;
26
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
switch (c) {
case 1:
cout << "Enter value to be inserted: ";
cin >> i;
r = [Link](r, i);
break;
case 2:
if (r == NULL) {
cout << "Tree is Empty" << endl;
continue;
}
cout << "Balanced AVL Tree:" << endl;
[Link](r, 1);
cout<<endl;
break;
case 3:
cout << "Inorder Traversal:" << endl;
[Link](r);
cout << endl;
break;
case 4:
cout << "Preorder Traversal:" << endl;
[Link](r);
cout << endl;
break;
case 5:
cout << "Postorder Traversal:" << endl;
[Link](r);
cout << endl;
break;
case 6:
exit(1);
break;
default:
cout << "Wrong Choice" << endl;
}
}
return 0;
}
Output
[Link] Element into the tree
27
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
[Link] Balanced AVL Tree
[Link] traversal
[Link] traversal
[Link] traversal
[Link]
14 Write C++ programs for sorting a given list of elements in ascending order using
the following sorting methods.
a) Quick sort
b) Merge sort
#include <iostream>
using namespace std;
int count = 0;
for (int i = start + 1; i <= end; i++) {
if (arr[i] <= pivot)
count++;
}
return pivotIndex;
}
// base case
if (start >= end)
return;
int main()
{
int arr[] = { 9, 3, 4, 2, 1, 8 };
int n = 6;
quickSort(arr, 0, n - 1);
return 0;
}
B) Merge Sort
#include <iostream>
// UTILITY FUNCTIONS
// Function to print an array
void printArray(int A[], int size)
{
for (auto i = 0; i < size; i++)
cout << A[i] << " ";
cout<<endl;
}
// Driver code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
auto arr_size = sizeof(arr) / sizeof(arr[0]);
32
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
#include <bits/stdc++.h>
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << "Minimum number of multiplications is "
<< MatrixChainOrder(arr, 1, N - 1);
return 0;
33
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
}
16. Write a C++ program that uses dynamic programming algorithm to solve the optimal
binary search tree problem
#include <iostream>
using namespace std;
int main() {
int keys[] = {10, 12, 20};
34
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
Output
Cost of Optimal BST is: 142
/*
* HashEntry Class Declaration
*/
class HashEntry
{
public:
int key;
int value;
HashEntry(int key, int value)
{
this->key = key;
this->value = value;
}
};
/*
* HashMap Class Declaration
*/
class HashMap
{
private:
35
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
HashEntry **table;
public:
HashMap()
{
table = new HashEntry * [TABLE_SIZE];
for (int i = 0; i< TABLE_SIZE; i++)
{
table[i] = NULL;
}
}
/*
* Hash Function
*/
int HashFunc(int key)
{
return key % TABLE_SIZE;
}
/*
* Insert Element at a key
*/
void Insert(int key, int value)
{
int hash = HashFunc(key);
while (table[hash] != NULL && table[hash]->key != key)
{
hash = HashFunc(hash + 1);
}
if (table[hash] != NULL)
delete table[hash];
table[hash] = new HashEntry(key, value);
}
/*
* Search Element at a key
*/
int Search(int key)
{
int hash = HashFunc(key);
while (table[hash] != NULL && table[hash]->key != key)
{
hash = HashFunc(hash + 1);
36
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
}
if (table[hash] == NULL)
return -1;
else
return table[hash]->value;
}
/*
* Remove Element at a key
*/
void Remove(int key)
{
int hash = HashFunc(key);
while (table[hash] != NULL)
{
if (table[hash]->key == key)
break;
hash = HashFunc(hash + 1);
}
if (table[hash] == NULL)
{
cout<<"No Element found at key "<<key<<endl;
return;
}
else
{
delete table[hash];
}
cout<<"Element Deleted"<<endl;
}
~HashMap()
{
for (int i = 0; i < TABLE_SIZE; i++)
{
if (table[i] != NULL)
delete table[i];
delete[] table;
}
}
};
37
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
/*
* Main Contains Menu
*/
int main()
{
HashMap hash;
int key, value;
int choice;
while (1)
{
cout<<"\n----------------------"<<endl;
cout<<"Operations on Hash Table"<<endl;
cout<<"\n----------------------"<<endl;
cout<<"[Link] element into the table"<<endl;
cout<<"[Link] element from the key"<<endl;
cout<<"[Link] element at a key"<<endl;
cout<<"[Link]"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter element to be inserted: ";
cin>>value;
cout<<"Enter key at which element to be inserted: ";
cin>>key;
[Link](key, value);
break;
case 2:
cout<<"Enter key of the element to be searched: ";
cin>>key;
if ([Link](key) == -1)
{
cout<<"No element found at key "<<key<<endl;
continue;
}
else
{
cout<<"Element at key "<<key<<" : ";
cout<<[Link](key)<<endl;
38
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
}
break;
case 3:
cout<<"Enter key of the element to be deleted: ";
cin>>key;
[Link](key);
break;
case 4:
exit(1);
default:
cout<<"\nEnter correct option\n";
}
}
return 0;
}
#include <iostream>
39
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
// Driver Code
int main()
{
40
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
deleteRoot(arr, n);
printArray(arr, n);
return 0;
}
class SkipList
{
// Maximum level for this skip list
int MAXLVL;
r = (float)rand()/RAND_MAX;
}
return lvl;
};
current = current->forward[0];
43
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
level = rlevel;
}
srand((unsigned)time(0));
[Link](3);
[Link](6);
[Link](7);
[Link](9);
[Link](12);
[Link](19);
[Link](17);
[Link](26);
[Link](21);
[Link](25);
[Link]();
}
20. Write a C++ Program to Create a Graph using Adjacency Matrix Representation
#include<iostream>
using namespace std;
int vertArr[20][20]; //the adjacency matrix initially 0
int count = 0;
void displayMatrix(int v) {
int i, j;
for(i = 0; i < v; i++) {
for(j = 0; j < v; j++) {
cout << vertArr[i][j] << " ";
}
cout << endl;
}
}
void add_edge(int u, int v) { //function to add edge into the matrix
vertArr[u][v] = 1;
vertArr[v][u] = 1;
}
main(int argc, char* argv[]) {
int v = 6; //there are 6 vertices in the graph
add_edge(0, 4);
45
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
add_edge(0, 3);
add_edge(1, 2);
add_edge(1, 4);
add_edge(1, 5);
add_edge(2, 3);
add_edge(2, 5);
add_edge(5, 3);
add_edge(5, 4);
displayMatrix(v);
}
21. Write a C++ program to implement graph traversal techniques a) BFS b) DFS
#include <bits/stdc++.h>
using namespace std;
// No. of vertices
int V;
public:
// Constructor
Graph(int V);
46
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
Graph::Graph(int V)
{
this->V = V;
[Link](V);
}
void Graph::BFS(int s)
{
// Mark all the vertices as not visited
vector<bool> visited;
[Link](V, false);
while (![Link]()) {
47
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
visited[adjacent] = true;
queue.push_back(adjacent);
}
}
}
}
// Driver code
int main()
{
// Create a graph given in the above diagram
Graph g(4);
[Link](0, 1);
[Link](0, 2);
[Link](1, 2);
[Link](2, 0);
[Link](2, 3);
[Link](3, 3);
return 0;
}
Output
Following is Breadth First Traversal (starting from vertex 2)
2031
Time Complexity: O(V+E), where V is the number of nodes and E is the number of edges.
Auxiliary Space: O(V)
48
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
void Graph::DFS(int v)
{
// Mark the current node as visited and
// print it
visited[v] = true;
cout << v << " ";
// Driver code
49
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
int main()
{
// Create a graph given in the above diagram
Graph g;
[Link](0, 1);
[Link](0, 2);
[Link](1, 2);
[Link](2, 0);
[Link](2, 3);
[Link](3, 3);
// Function call
[Link](2);
return 0;
}
Output
Following is Depth First Traversal (starting from vertex 2)
2013
50
VAAGDEVI DEGREE COLLEGE -MANCHERIAL
DATA STRUCTURE PRACTICAL PROGRAMS
// Driver program
int main()
{
int arr[] = { 60 ,20 ,40 ,70, 30, 10};
int n = sizeof(arr) / sizeof(arr[0]);
//heapify algorithm
// the loop must go reverse you will get after analyzing manually
// (i=n/2 -1) because other nodes/ ele's are leaf nodes
// (i=n/2 -1) for 0 based indexing
// (i=n/2) for 1 based indexing
for(int i=n/2 -1;i>=0;i--){
heapify(arr,n,i);
}
heapSort(arr, n);
return 0;
}
52