0% found this document useful (0 votes)
217 views

DS Practical Journal

This document contains code for implementing various data structures and algorithms in C++, including: 1) Quicksort, shellsort, and sequential search algorithms for sorting and searching arrays. 2) Code for implementing a circular queue with functions for insertion, deletion, and display. 3) Code for implementing a priority queue with functions for insertion and deletion based on priority level and a display function. 4) Code for implementing a deque (double-ended queue) with functions for insertion at both ends, deletion, and traversal/display.

Uploaded by

Anshuman Biswas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
217 views

DS Practical Journal

This document contains code for implementing various data structures and algorithms in C++, including: 1) Quicksort, shellsort, and sequential search algorithms for sorting and searching arrays. 2) Code for implementing a circular queue with functions for insertion, deletion, and display. 3) Code for implementing a priority queue with functions for insertion and deletion based on priority level and a display function. 4) Code for implementing a deque (double-ended queue) with functions for insertion at both ends, deletion, and traversal/display.

Uploaded by

Anshuman Biswas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 123

Write a C++ program for sorting array using quick sort algorithm.

Coding

#include<iostream>
#include<conio.h>
#define SIZE 9
using namespace std;

void swap(int a[],int i,int j)


{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
void print(const int arr[])
{

for(int i=0;i<SIZE;i++)
{
cout<<arr[i]<<" ";
}

cout<<endl;
}

void qsort(int a[],int li, int ri)


{
int left,right,pivot;
if(li>=ri)return;

left=li;
right=ri;

pivot=a[(li+ri)/2];

while(left <= right)


{
while(a[left]<pivot)left++;
while(a[right]>pivot)right--;
if(left<=right)
{
swap(a,left,right);
left++;
right--;
}
print(a);
}
}
int main()
{
int a[SIZE]={1,12,5,26,7,14,3,7,2};
print(a);
qsort(a,0,SIZE-1);
}
Write a C++ program for sorting array using shell sort algorithm.

Coding
#include<iostream>
#include<conio.h>
#define max 5
using namespace std;

class ShellSort
{
int arr[max], n;
public:
void GetData();
void ShowData();
void SortLogic();

};
void ShellSort::GetData()
{
cout<<" How Many elements You require : ";
cin>>n;

for(int i=0;i<n;i++)
{
cin>>arr[i];
}
}
void ShellSort::ShowData()
{
cout<<"\n**Display**\n";
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
void ShellSort::SortLogic()
{
int i,j,temp,increment;

for(increment=n/2; increment>0; increment/=2)


{
for(i=increment;i<n;i++)
{
temp=arr[i];
for(j=i;j>=increment;j-=increment)
{
if(temp<arr[j-increment])
arr[j]=arr[j-increment];
else break;
}
arr[j]=temp;

}
}
}

int main()
{
cout<<"\n****Shell Sort****\n";
ShellSort obj;
obj.GetData();
obj.SortLogic();
obj.ShowData();
//return 0;
getch();
}
Write a C++ program for searching an element using sequential
search algorithm.

Coding
#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
cout<<"Enter the array size ";
int size;
cin>>size;
int array[size],key,i;
for(int j=0;j<size;j++)
{
cout<<"\nEnter "<<j<<" Element : ";
cin>>array[j];
}
cout<<"Enter key to search in array : ";
cin>>key;
for(i=0;i<size;i++)
{
if(key==array[i])
{
cout<<"\nKey found at index number : "<<i;
break;
}
}
if(key!=array[i])
{
cout<<"\nKey not found.";
}
getch();
}
Write a C++ program to implement the Infix to Postfix.

Coding
#include<iostream>
#include<conio.h>
using namespace std;
class stack
{
public:
char stack_array[50];
int top;
stack()
{
top=-1;
}
void push(char symbol)
{
if(full())
{
cout<<"\n stack overflow";
}
else
{
top=top+1;
stack_array[top]=symbol;
}
}
char pop()
{
if (empty())
return ('#');
else
return (stack_array[top--]);
}
int empty()
{
if (top == -1)
return (1);
else
return (0);
}
int full()
{
if (top == 49)
return (1);
else
return (0);
}
};
class Expression
{
char infix[50];
char postfix[50];
public:
void read()
{
cout << "\nEnter an infix expression: ";
cin >> infix;
}
int white_space(char symbol)
{
if (symbol == ' ' || symbol == '\t' || symbol == '\0')
return 1;
else
return 0;
}
void ConvertToPostfix()
{
stack s;
int l, precedence, p;
char entry1, entry2;
p = 0;
for (int i = 0; infix[i] != '\0'; i++)
{
entry1 = infix[i];
if (!white_space(entry1))
{
switch (entry1)
{
case '(':
s.push(entry1);
break;
case ')':
while ((entry2 = s.pop()) != '(')
postfix[p++] = entry2;
break;
case '+':
case '-':
case '*':
case '/':
if (!s.empty())
{
precedence = prec(entry1);
entry2 = s.pop();
while (precedence <= prec(entry2))
{
postfix[p++] = entry2;
if (!s.empty())
entry2 = s.pop();
else
break;
}
if (precedence > prec(entry2))
s.push(entry2);
}
s.push(entry1);
break;
default:
postfix[p++] = entry1;
break;
}
}
}
while (!s.empty())
postfix[p++] = s.pop();
postfix[p] = '\0';
cout << "\nThe postfix expression is: " << postfix << endl;
}
int prec(char symbol)
{
switch (symbol)
{
case '/': return (4);
case '*': return (3);
case '+': return (2);
case '-': return (1);
case '(': return (0);
default: return (-1);
}
}
};
int main()
{
char choice = 'y';
Expression expr;
while (choice == 'y')
{
expr.read();
expr.ConvertToPostfix();
cout << "\nDo you want to continue? (y/n): ";
cin >> choice;
}
}
Write a C++ program for implementing circular queue.

Coding
#include<iostream>
#include<conio.h>
using namespace std;
const int MAX = 5;
class cqueue
{
int a[MAX],front,rear;
public :
cqueue()
{
front=rear=-1;
}
void insert(int );
int deletion();
void display();
};
void cqueue :: insert(int val)
{
if((front==0 && rear==MAX-1) || (rear+1==front))
cout<<" Circular Queue is Full";
else
{
if(rear==MAX-1)
rear=0;
else
rear++;
a[rear]=val;
}
if(front==-1)
front=0;
}
int cqueue :: deletion()
{
int k;
if(front==-1)
cout<<"Circular Queue is Empty";
else
{
k=a[front];
if(front==rear)
front=rear=-1;
else
{
if(front==MAX-1)
front=0;
else
front++;
}
}
return k;
}
void cqueue :: display()
{
int i;
if(front==-1)
cout<<"Circular Queue is Empty";
else
{
if(rear < front)
{
for(i=front;i<=MAX-1;i++)
cout<<a[i]<<" ";
for(i=0;i<=rear;i++)
cout<<a[i]<<" ";
}
else
{
for(i=front;i<=rear;i++)
cout<<a[i]<<" ";
cout<<endl;
}
}
}
int main()
{
cqueue c1;
int ch,val;
char op;
do
{
cout<<"-----------Menu-------------";
cout<<"\n1.Insertion \n2.Deletion \n3.Display \n4.Exit\n";
cout<<"Enter Your Choice:";
cin>>ch;
switch(ch)
{

case 1 :
cout<<"Enter Element to Insert:";
cin>>val;
c1.insert(val);
cout<<"Inserted Element:\n"<<val<<endl;
break;
case 2 :
val=c1.deletion();
cout<<"Deleted Element :\n"<<val<<endl;
break;
case 3 :
c1.display();
break;
}
cout<<"Do you want to continue<Y/N> ?";
cin>>op;
}
while(op=='Y' || op=='y');

}
Write a C++ program for implementing Priorty queue.

Coding
#include<iostream>
#include<conio.h>
#define size 5
using namespace std;
int pq[size];
int front=-1, rear=-1; void check(int item)
{
int i,j;
for(i=0;i<=rear;i++)
{
if(item>=pq[i])
{
for(j=rear+1;j>i;j--)
{
pq[j]=pq[j-1];
}
pq[i]=item;
return;
}
}
pq[i]=item;
}
void insertbypriority(int item)
{
if(rear>=size-1)
{
cout<<"Priority Queue is Full \n";
return;
}
if(front==-1 && rear==-1)
{
front++;
rear++;
pq[rear]=item;
return;
}
else
check(item);
rear++;
}
void deletebypriority(int item)
{
int i;
if((front==-1) && (rear==-1))
{
cout<<"Priority Queue is empty\n";
return;
}
for(i=0;i<=rear;i++)
{
if(item==pq[i])
{
for(;i<rear;i++)
{
pq[i]=pq[i+1];
}
pq[i]=-99;
rear--;
if(rear==-1)
front=-1;
return ;
}
}
cout<<item<<" not found\n";
}
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
cout<<"Priority Queue is Empty\n";
return;
}
for(;i<=rear;i++)
{
cout<<pq[i]<<"\t";
}
}
int main()
{
int item, choice, t;
do
{
cout<<"-------------------Priority queue-------------------"<<endl;
cout<<"Press 1 to Insert in queue\n";
cout<<"Press 2 to Delete from queue \n";
cout<<"Press 3 to Display priority queue \n";
cout<<"\n\nEnter your choice:\t ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n\nEnter item:\t";
cin>>item;
insertbypriority(item);
cout<<"\nItem inserted \n";
break;
case 2:
cout<<"\nEnter item to be deleted: \t";
cin>>item;
deletebypriority(item);
break;
case 3:
display();
break;
}
cout<<"\n\nPress 1 to continue... 2 to exit... \n";
cin>>t;
}
while(t==1);
getch();
}
Write a C++ program for implementing Dequeue.

Coding

/* C++ Program To Implement Doubly Ended Queue*/


#include <iostream>
#include <cstdlib>
using namespace std;

struct node
{
int info;
node *next;
node *prev;

}*head, *tail;

class dqueue
{
public:
int top1, top2;
void insert();
void del();
void display();
dqueue()
{
top1 = 0;
top2 = 0;
head = NULL;
tail = NULL;
}
};

int main()
{
int choice;
dqueue dl;
while (1)
{
cout<<"\n-------------"<<endl;
cout<<"Operations on Deque"<<endl;
cout<<"\n-------------"<<endl;
cout<<"1.Insert Element into the Deque"<<endl;
cout<<"2.Delete Element from the Deque"<<endl;
cout<<"3.Traverse the Deque"<<endl;
cout<<"4.Quit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
cout<<endl;
switch(choice)
{
case 1:
dl.insert();
break;
case 2:
dl.del();
break;
case 3:
dl.display();
break;
case 4:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}

void dqueue::insert()
{
struct node *temp;
int ch, value;
if (top1 + top2 >= 50)
{
cout<<"Dequeue Overflow"<<endl;
return;
}
if (top1 + top2 == 0)
{
cout<<"Enter the value to be inserted: ";
cin>>value;
head = new (struct node);
head->info = value;
head->next = NULL;
head->prev = NULL;
tail = head;
top1++;
cout<<"Element Inserted into empty deque"<<endl;
}
else
{
while (1)
{
cout<<endl;
cout<<"1.Insert Element at first"<<endl;
cout<<"2.Insert Element at last"<<endl;
cout<<"3.Exit"<<endl;
cout<<endl;
cout<<"Enter Your Choice: ";
cin>>ch;
cout<<endl;
switch(ch)
{
case 1:
cout<<"Enter the value to be inserted: ";
cin>>value;
temp = new (struct node);
temp->info = value;
temp->next = head;
temp->prev = NULL;
head->prev = temp;
head = temp;
top1++;
break;
case 2:
cout<<"Enter the value to be inserted: ";
cin>>value;
temp = new (struct node);
temp->info = value;
temp->next = NULL;
temp->prev = tail;
tail->next = temp;
tail = temp;
top2++;
break;
case 3:
return;
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
}
}

void dqueue::del()
{
if (top1 + top2 <= 0)
{
cout<<"Deque Underflow"<<endl;
return;
}
int ch;
while (1)
{
cout<<endl;
cout<<"1.Delete Element at first"<<endl;
cout<<"2.Delete Element at last"<<endl;
cout<<"3.Exit"<<endl;
cout<<endl;
cout<<"Enter Your Choice: ";
cin>>ch;
cout<<endl;
switch(ch)
{
case 1:
head = head->next;
head->prev = NULL;
top1--;
break;
case 2:
tail = tail->prev;
tail->next = NULL;
top2--;
break;
case 3:
return;
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
}
void dqueue::display()
{
struct node *temp;
int ch;
if (top1 + top2 <= 0)
{
cout<<"Deque Underflow"<<endl;
return;
}
while (1)
{
cout<<endl;
cout<<"1.Display Deque from Beginning"<<endl;
cout<<"2.Display Deque from End"<<endl;
cout<<"3.Exit"<<endl;
cout<<endl;
cout<<"Enter Your Choice: ";
cin>>ch;
cout<<endl;
switch (ch)
{
case 1:
temp = head;
cout<<"Deque from Beginning:"<<endl;
while (temp != NULL)
{
cout<<temp->info<<" ";
temp = temp->next;
}
cout<<endl;
break;
case 2:
cout<<"Deque from End:"<<endl;
temp = tail;
while (temp != NULL)
{
cout<<temp->info<<" ";
temp = temp->prev;
}
temp = tail;
cout<<endl;
break;
case 3:
return;
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
}
Write a menu driven program that implements Singly Linked list for
the following operation are Insertion, Deletion, Count & Display.

Coding
# include <iostream>
# include <process.h>
# include <conio.h>
# include <malloc.h>
using namespace std;
int cur_link_list=1;
int display_menu();
struct link_list
{
int no;
struct link_list *next;
};
class link
{
link_list *list;
public:
link_list *head;
link()
{
list=NULL;
head=NULL;
}
void count()
{
int c=0;
cout<<endl;
if(head==NULL)
{
cout<<"Link list is empty !!!";
}
else
{
list=head;
while(list->next!=NULL)
{
c=c+1;
list=list->next;
}
cout<<"Total nodes="<<c;
}
}
void get_list()
{
int no;
list=head;
while(list->next!=NULL)
{
list=list->next;
}
while(1)
{
cout<<"Enter Number :";
cin>>no;
if(no!=0)
{
if(list==NULL)
{
list=new link_list;
head=list;
}
list->no=no;
list->next = new link_list;
list=list->next;
}
else
{
list->next=NULL;
break;
}
}
}
void display()
{
list=head;
cout<<endl;
if (list==NULL)
{
cout<<"Link list is empty !!!";
return;
}
while(list->next!=NULL)
{
cout<<list->no<<"\t";
list=list->next;
}
}
void insert()
{
int ch;
list=head;
cout<<endl;
cout<<"[ 1 ] : Insert at First"<<endl;
cout<<"[ 2 ] : Insert in Middle"<<endl;
cout<<"[ 3 ] : Insert at Last"<<endl;
cout<<"[ 4 ] : Back to main Menu"<<endl;
cout<<"Enter your choice :";
cin>>ch;
link_list *newnode;
newnode=new link_list;
switch(ch)
{
case 1:
cout<<"Enter Number :";
cin>>newnode->no;
list=head;
if(list==NULL)
{
list=newnode;
newnode->next=NULL;
head=list;
}
else
{
newnode->next=list;
head=newnode;
}
break;
case 2:
int no;
cout<<endl;
cout<<"Enter Number after which you want to insert :";
cin>>no;
list=head;
while(list->next !=NULL)
{
if(list->no==no)
{
cout<<"Enter Number to Insert :";
cin>>newnode->no;
newnode->next=list->next;
list->next=newnode;
if(list==head)
{
head=newnode;
}
return;
}
list=list->next;
}
cout<<"Key not found ..."<<endl;
break;
case 3 :
list=head;
while(list->next!=NULL)
{
list=list->next;
}
cout<<"Enter Number :";
cin>>newnode->no;
if(head==NULL)
{
list=newnode;
head=list;
}
else
{
list->next=newnode;
newnode->next=NULL;
}
break;
}
}
void delete_list()
{
cout<<endl;
list=head;
int no;
cout<<"Enter the number to deleted :";
cin>>no;
if(head->no==no)
{
head=head->next;
return;
}
while(list->next!=NULL)
{
if(list->next->no==no)
{
list->next=list->next->next;
return;
}
list=list->next;
}
cout<<"Number not found !!!";
}
void sort()
{
link_list *i,*j,*t;
for(i=head;i->next!=NULL;i=i->next)
{
for(j=head;j->next!=NULL;j=j->next)
{
if(i->no < j->no)
{
t->no=i->no;
i->no=j->no;
j->no=t->no;
}
}
}
}
};
int main()
{
link o;
int a;
char ch1;
while(1)
{
cout<<"\n\n-------------------------------------------------\nPress 1 for insert ";
cout<<"\nPress 2 for delete";
cout<<"\nPress 3 for count";
cout<<"\nPress 4 for display ";
cout<<"\npress 5 for exit ";
cout<<"\nEnter ur choice : ";
cin>>a;
switch(a)
{
case 1:
o.insert();
break;
case 2:
o.delete_list();
break;
case 3:
o.count();
break;
case 4:
o.display();
break;
case 5:
exit(0);
break;
default:
cout<<"invalid";
}}
getch();}
Write a menu driven program that implements Doubly Linked list for
the following operation are Insertion, Deletion, Searching& Display.

Coding
#include <iostream>
#include <conio.h>
#include<process.h>
using namespace std;
struct node
{
int data;
node *next;
node *prev;
};
void addnode();
void delnode();
void display();
void show();
void search();
node *start=NULL, *temp1, *temp2, *temp3;
int main()
{
char ch;
do
{
char i;
cout<<"Press 'a' to add node \nPress 'd' to delete"<<endl;
cout<<"Press 's' for search\nPress 'v' for display \nPress 'e' for backward display"<<endl;
cin>>i;
switch (i)
{
case'a':
addnode();
break;
case'd':
delnode();
break;
case'v' :
display();
break;
case's':
search();
break;
case'e':
show();
break;
default:
cout<<"Bad input"<<endl;
break;
}
cout<<"want to process more y/n"<<endl;
cin>>ch;
}
while(ch!='n');
return 0;
}
void addnode()
{
char r;
temp1=new node;
cout<<"enter int to store"<<endl;
cin>>temp1->data;
cout<<"press 's' to add in start\nPress 'm' for midd \nPress 'e' for end"<<endl;
cin>>r;
switch (r)
{
case's':
if(start==NULL)
{
start=temp1;
temp1->next=NULL;
temp1->prev=NULL;
}
else
{
temp2=start;
temp1->next=temp2;
temp1->prev=NULL;
start=temp1;
temp2->prev=temp1;
}
break;
case'e':
if(start==NULL)
{
start=temp1;
temp1->next=NULL;
temp1->prev=NULL;
}
else
{
temp2=start;
while(temp2->next!=NULL)
temp2=temp2->next;
temp2->next=temp1;
temp1->prev=temp2;
temp1->next=NULL;
}
break;
case'm':
int num;
cout<<"enter node after which you want to enter"<<endl;
cin>>num;
temp2=start;
for(int i=0;i<num;i++)
{
if(start==NULL)
cout<<"given node not found"<<endl;
else
{
temp3=temp2;
temp2=temp2->next;
}
}
temp1->next=temp2;
temp3->next=temp1;
temp1->prev=temp3;
temp2->prev=temp1;
break;
}
}
void display()
{
temp3=start;
if(start==NULL)
cout<<"no node to display"<<endl;
else
{
while(temp3->next!=NULL)
{
cout<<"Data stored is "<<temp3->data<<" at "<<temp3<<endl;
temp3=temp3->next;
}
cout<<"Data stored is "<<temp3->data<<" at "<<temp3<<endl;
}
}
void search()
{
int p;
cout<<"enter no to search"<<endl;
cin>>p;
temp1=start;
while(temp1->next!=NULL)
{
if(temp1->data==p)
{
cout<<temp1->data<<" is stored in "<< temp1->next<<endl;
}
temp1=temp1->next;
}
}
void delnode()
{
char d;
cout<<"press 's' to delete from start,'m' for midd , 'e' for end"<<endl;
cin>>d;
switch (d)
{
case's':
if(start==NULL)
{
cout<<"no node to delete"<<endl;
}
else
{
temp1=start;
start=start->next;
start->prev=NULL;
delete temp1;
}
break;
case'e':
if(start==NULL)
{
cout<<"no node to delete"<<endl;
}
else
{
temp1=start;
while(temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
}
delete temp1;
temp2->next=NULL;
}
break;
case'm':
int num;
cout<<"enter node you want to delete"<<endl;
cin>>num;
temp1=start;
for(int i=1;i<num;i++)
{
if(start==NULL)
cout<<"given node does not exist"<<endl;
else
{
temp2=temp1;
temp1=temp1->next;
}
}
temp3=temp1->next;
temp2->next=temp3;
temp3->prev=temp2;
delete temp1;
break;
}
}
void show()
{
cout<<"backward display"<<endl;
temp3=start;
if(start==NULL)
cout<<"no node to display"<<endl;
else
{
while(temp3->next!=NULL)
{
temp3=temp3->next;
}
while(temp3->prev!=NULL)
{
cout<<"Data stored is "<<temp3->data<<" at "<<temp3<<endl;
temp3=temp3->prev;
}
cout<<"Data stored is "<<temp3->data<<" at "<<temp3<<endl;
}
}
Write a menu driven program that implements Singly Circular Linked
List for the following operation are Insertion, Deletion, Searching &
Display.

Coding
# include <iostream>
# include <process.h>
# include <conio.h>
# include <malloc.h>
using namespace std;

int cur_link_list=1;
int display_menu();
struct link_list
{
int no;
struct link_list *next;
};
class link
{
link_list *list;
public:
link_list *head;
link()
{
list=NULL;
head=NULL;
}
void get_list();
void display_list();
void insert();
void delete_list();
void sort();
void count();
void reverse();
void search();
void copy(link_list, link_list);
};
void link :: get_list()
{
int no;
if(head!=NULL)
{
while(list->next!=head)
{
delete(list);
list=list->next;
}
list=NULL;
head=NULL;
}
while(1)
{
cout<<"Enter Number :";
cin>>no;
if(no!=0)
{
if(list==NULL)
{
list=new link_list;
head=list;
}
list->no=no;
list->next = new link_list;
list=list->next;
}
else
{
list->next=head;
break;
}
}
}
void link :: count()
{
int c=0;
cout<<endl;
if (head==NULL)
{
cout<<"Link list is empty !!!";
}
else
{
list=head;
while(list->next!=head)
{
c=c+1;
list=list->next;
}
cout<<"Total nodes="<<c;
}
}
void link :: display_list()
{
list=head;
cout<<endl;
if (list==NULL)
{
cout<<"Link list is empty !!!";
return;
}
if(list->next==head)
{
cout<<list->no;
return;
}
while(list->next!=head)
{
cout<<list->no<<"\t";
list=list->next;
}
}
void link :: insert()
{
int ch;
list=head;
cout<<endl;
cout<<"[ 1 ] : Insert at First"<<endl;
cout<<"[ 2 ] : Insert in Middle"<<endl;
cout<<"[ 3 ] : Insert at Last"<<endl;
cout<<"[ 4 ] : Back to main Menu"<<endl;
cout<<"Enter your choice :";
cin>>ch;
link_list *newnode;
newnode=new link_list;
switch(ch)
{
case 1:
cout<<"Enter Number :";
cin>>newnode->no;
list=head;
newnode->next=list;
while(list->next!=head)
{
list=list->next;
}
list->next=newnode;
head=newnode;
break;
case 2: int no;
cout<<endl;
cout<<"Enter Number after which you want to insert :";
cin>>no;
list=head;
while(list->next !=head)
{
if(list->no==no)
{
cout<<"Enter Number to Insert :";
cin>>newnode->no;
if(list==head)
{
list=head;
newnode->next=list;
while(list->next!=head)
{
list=list->next;
}
list->next=newnode;
head=newnode;
}
else
{
newnode->next=list->next;
list->next=newnode;
}
return;
}
list=list->next;
}
cout<<"Key not found ..."<<endl;
break;
case 3 :
list=head;
while(list->next!=head)
{
list=list->next;
}
cout<<"Enter Number :";
cin>>newnode->no;
list->next=newnode;
newnode->next=head;
break;
}
}
void link :: search()
{
if(list==NULL)
cout<<"List is empty!!!!!!";
else
{
int no,a=0;
cout<<endl;
cout<<"Enter Number to be search :";
cin>>no;
list=head;
while(list->next !=head)
{
if(list->no==no)
{
cout<<"Search Element found"; a=1; break;
}
list=list->next;
}
if(a==0)
cout<<"Search not found"; getch(); } }
void link :: delete_list()
{
cout<<endl;
list=head;
int no;
cout<<"Enter the number to deleted :";
cin>>no;
if(head->no==no)
{
while(list->next!=head)
{
list=list->next;
}
list->next=head->next;
head=head->next;
return;
}
while(list->next!=head)
{
if(list->next->no==no)
{
list->next=list->next->next;
return; }

list=list->next;
}
cout<<"Number not not found !!!";
}
void link :: sort()
{
link_list *i,*j,*t;
for(i=head;i->next!=head;i=i->next)
{
for(j=head;j->next!=head;j=j->next)
{
if(i->no < j->no)
{
t->no=i->no;
i->no=j->no;
j->no=t->no;
}
}
}
}
void link :: reverse()
{
int a[50];
list=head;
int i=0;
while(list->next!=head)
{
a[i]=list->no;
list=list->next;
i=i+1;
}
int n=i-1;
i=n;
list=head;
while(list->next!=head)
{
list->no=a[i];
cout<<list->no<<"\t";
list=list->next;
i=i-1;
}
}

int main()
{
link l1,l2,l3;
while(1)
{
switch(display_menu())
{
case 1:
l1.get_list();
getch();
break;
case 2:
l1.insert();
getch();
break;
case 3:
l1.display_list();
getch();
break;
case 4:
l1.delete_list();
getch();
break;
case 5:
cout<<endl;
l1.sort();
l1.display_list();
cout<<endl<<endl<<"Linklist sorted !!!";
getch();
break;
case 6:
l1.reverse();
getch();
break;
case 7:
l1.count();
getch();
break;
case 8:
l1.search();
getch();
break;
case 10 :
exit(1);
}
}
}
int display_menu()
{
cout<<endl;
cout<<" [ 1 ] Create List"<<endl;
cout<<" [ 2 ] Insert"<<endl;
cout<<" [ 3 ] Display"<<endl;
cout<<" [ 4 ] Delete"<<endl;
cout<<" [ 5 ] Sort"<<endl;
cout<<" [ 6 ] Reverse"<<endl;
cout<<" [ 7 ] Count"<<endl;
cout<<" [ 8 ] Search"<<endl;
cout<<" [ 9 ] Copy"<<endl;
cout<<" [ 10 ] Exit"<<endl;
cout<<" Enter your choice :";
int ch;
cin>>ch;
return ch;
}
Write a C++ program to create a Binary Search Tree & Traverse the
tree in Inorder, Preorder, Postorder & Search the tree for a given
node and delete the node.

Coding
#include<iostream>
#include<conio.h>
#include<process.h>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
};
class BST
{
public:
node *tree;
BST()
{
tree=NULL;
}
void createTree(node **,int item);
void preOrder(node *);
void inOrder(node *);
void postOrder(node *);
int totalNodes(node *);
int get_level(node *,int);
node **searchElement(node **,int);
};
void BST :: createTree(node **tree,int item)
{
if(*tree == NULL)
{
*tree = new node;
(*tree)->data = item;
(*tree)->left = NULL;
(*tree)->right = NULL;
}
else
{
if( (*tree)->data > item)
createTree( &((*tree)->left),item);
else
createTree( &((*tree)->right),item);
}
}
void BST :: preOrder(node *tree)
{
if( tree!=NULL)
{
cout<<" "<< tree->data;
preOrder(tree->left);
preOrder(tree->right);
}
}
void BST :: inOrder(node *tree)
{
if( tree!=NULL)
{
inOrder( tree->left);
cout<<" "<< tree->data;
inOrder(tree->right);
}
}
void BST :: postOrder(node *tree)
{
if( tree!=NULL)
{
postOrder( tree->left);
postOrder( tree->right);
cout<<" "<<tree->data;
}
}
int BST :: totalNodes(node *tree)
{
if( tree == NULL)
return 0;
else return( totalNodes(tree->left) + totalNodes(tree->right) + 1 );
}
int BST::get_level(node *root,int k)
{
static int level=0;
if(root==NULL)
return 0;
if(root->data==k)
return (level=1);
else if(get_level(root->left,k) || get_level(root->right,k))
return (++level);
else
return 0;
}
node ** BST :: searchElement(node **tree, int item)
{
if( ((*tree)->data == item) || ( (*tree) == NULL) )
return tree;
else if( item < (*tree)->data)
return searchElement( &(*tree)->left, item);
else return searchElement( &(*tree)->right, item);
}
int main()
{
BST obj;
int choice;
int height=0,total=0,n,item;
node **tmp;
while(1)
{
cout<<"\n\n*****BINARY SEARCH TREE OPERATIONS*****\n\n";
cout<<"1) Create Tree\n";
cout<<"2) Traversal\n";
cout<<"3) Search Node\n";
cout<<"4) Total Nodes\n";
cout<<"5) Depth of Node\n";
cout<<"6) Exit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1 :
cout<<"\n\n--Creating Tree--";
cout<<"\nHow many nodes u want to enter : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter value : ";
cin>>item;
obj.createTree(&obj.tree,item);
}
break;
case 2 :
cout<<"\n\nInorder Traversal : ";
obj.inOrder(obj.tree);
cout<<"\n\nPre-order Traversal : ";
obj.preOrder(obj.tree);
cout<<"\n\nPost-order Traversal : ";
obj.postOrder(obj.tree);
getch();
break;
case 3 :
cout<<"\n\n--Search Element--\n";
cout<<"Enter item to searched : ";
cin>>item;
&(*tmp) == obj.searchElement(&obj.tree,item);
if( (*tmp) == NULL)
cout<<"\nSearch Element Not Found";
else
cout<<"\nSearch Element was Found";
getch();
break;
case 4 :
total=obj.totalNodes(obj.tree);

cout<<"\n\nTotal Nodes : "<<total;


getch();
break;
case 5:
cout<<"\n\n--Depth Element--\n";
cout<<"Enter item to searched : ";
cin>>item;
int l;
if( (*tmp) == NULL)
cout<<"\nSearch Element Not Found";
else
cout<<"\nSearch Element was Found";
getch();
break;
case 6 :
exit(1);
}
}
}
Write a C++ program to implement AVL Tree.

Coding

#include <iostream>
#include<ctype.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;

struct node
{
int element;
node *left;
node *right;
int height;
};
typedef struct node *nodeptr;
class bstree
{
public:
void insert(int,nodeptr &);
void del(int, nodeptr &);
int deletemin(nodeptr &);
void find(int,nodeptr &);
nodeptr findmin(nodeptr);
nodeptr findmax(nodeptr);
void makeempty(nodeptr &);
void copy(nodeptr &,nodeptr &);
nodeptr nodecopy(nodeptr &);
void preorder(nodeptr);
void inorder(nodeptr);
void postorder(nodeptr);
int bsheight(nodeptr);
nodeptr srl(nodeptr &);
nodeptr drl(nodeptr &);
nodeptr srr(nodeptr &);
nodeptr drr(nodeptr &);
int max(int,int);
int nonodes(nodeptr);
};
void bstree::insert(int x,nodeptr &p)
{
if (p == NULL)
{
p = new node;
p->element = x;
p->left=NULL;
p->right = NULL;
p->height=0;
if (p==NULL)
{
cout<<"Out of Space\n"<<endl;
}
}
else
{
if (x<p->element)
{
insert(x,p->left);
if ((bsheight(p->left) - bsheight(p->right))==2)
{
if (x < p->left->element)
{
p=srl(p);
}
else
{
p = drl(p);
}
}
}
else if (x>p->element)
{
insert(x,p->right);
if ((bsheight(p->right) - bsheight(p->left))==2)
{
if (x > p->right->element)
{
p=srr(p);
}
else
{
p = drr(p);
}
}
}
else
{
cout<<"Element Exists\n"<<endl;
}
}
int m,n,d;
m=bsheight(p->left);
n=bsheight(p->right);
d=max(m,n);
p->height = d + 1;
}
nodeptr bstree::findmin(nodeptr p)
{
if (p==NULL)
{
cout<<"The tree is empty\n"<<endl;
return p;
}
else
{
while(p->left !=NULL)
{
p=p->left;
}
return p;
}
}
nodeptr bstree::findmax(nodeptr p)
{
if (p==NULL)
{
cout<<"The tree is empty\n"<<endl;
return p;
}
else
{
while(p->right !=NULL)
{
p=p->right;
}
return p;
}
}
void bstree::find(int x,nodeptr &p)
{
if (p==NULL)
{
cout<<"Sorry! element not found\n"<<endl;
}
else
{
if (x < p->element)
{
find(x,p->left);
}
else
{
if (x>p->element)
{
find(x,p->right);
}
else
{
cout<<"Element found!\n"<<endl;
}
}
}
}
void bstree::copy(nodeptr &p,nodeptr &p1)
{
makeempty(p1);
p1 = nodecopy(p);
}
void bstree::makeempty(nodeptr &p)
{
nodeptr d;
if (p != NULL)
{
makeempty(p->left);
makeempty(p->right);
d=p;
free(d);
p=NULL;
}
}
nodeptr bstree::nodecopy(nodeptr &p)
{
nodeptr temp;
if (p==NULL)
{
return p;
}
else
{
temp = new node;
temp->element = p->element;
temp->left = nodecopy(p->left);
temp->right = nodecopy(p->right);
return temp;
}
}
void bstree::del(int x,nodeptr &p)
{
nodeptr d;
if (p==NULL)
{
cout<<"Sorry! element not found\n"<<endl;
}
else if ( x < p->element)
{
del(x,p->left);
}
else if (x > p->element)
{
del(x,p->right);
}
else if ((p->left == NULL) && (p->right == NULL))
{
d=p;
free(d);
p=NULL;
cout<<"Element deleted successfully\n"<<endl;
}
else if (p->left == NULL)
{
d=p;
free(d);
p=p->right;
cout<<"Element deleted successfully\n"<<endl;
}
else if (p->right == NULL)
{
d=p;
p=p->left;
free(d);
cout<<"Element deleted successfully\n"<<endl;
}
else
{
p->element = deletemin(p->right);
}
}
int bstree::deletemin(nodeptr &p)
{
int c;
cout<<"inside deltemin\n"<<endl;
if (p->left == NULL)
{
c=p->element;
p=p->right;
return c;
}
else
{
c=deletemin(p->left);
return c;
}
}
void bstree::preorder(nodeptr p)
{
if (p!=NULL)
{
cout<<p->element<<"\t";
preorder(p->left);
preorder(p->right);
}
}
void bstree::inorder(nodeptr p)
{
if (p!=NULL)
{
inorder(p->left);
cout<<p->element<<"\t";
inorder(p->right);
}
}
void bstree::postorder(nodeptr p)
{
if (p!=NULL)
{
postorder(p->left);
postorder(p->right);
cout<<p->element<<"\t";
}
}

int bstree::max(int value1, int value2)


{
return ((value1 > value2) ? value1 : value2);
}
int bstree::bsheight(nodeptr p)
{
int t;
if (p == NULL)
{
return -1;
}
else
{
t = p->height;
return t;
}
}
nodeptr bstree:: srl(nodeptr &p1)
{
nodeptr p2;
p2 = p1->left;
p1->left = p2->right;
p2->right = p1;
p1->height = max(bsheight(p1->left),bsheight(p1->right)) + 1;
p2->height = max(bsheight(p2->left),p1->height) + 1;
return p2;
}
nodeptr bstree:: srr(nodeptr &p1)
{
nodeptr p2;
p2 = p1->right;
p1->right = p2->left;
p2->left = p1;
p1->height = max(bsheight(p1->left),bsheight(p1->right)) + 1;
p2->height = max(p1->height,bsheight(p2->right)) + 1;
return p2;
}
nodeptr bstree:: drl(nodeptr &p1)
{
p1->left=srr(p1->left);
return srl(p1);
}
nodeptr bstree::drr(nodeptr &p1)
{
p1->right = srl(p1->right);
return srr(p1);
}

int bstree::nonodes(nodeptr p)
{
int count=0;
if (p!=NULL)
{
nonodes(p->left);
nonodes(p->right);
count++;
}
return count;
}

int main()
{
nodeptr root,root1,min,max;
int a,choice,findele,delele;
char ch='y';
bstree bst;
root = NULL;
root1=NULL;
cout<<"\t\t\t\tWELCOME TO AVL TREE"<<endl;
cout<<"\t\t\t\t\n"<<endl;
do
{
cout<<"\t\t:::::::::::::::::::::::::"<<endl;
cout<<"\t\tEnter 1 to insert a new node:"<<endl;
cout<<"\t\tEnter 2 to find the minimum value:"<<endl;
cout<<"\t\tEnter 3 to find the max value:"<<endl;
cout<<"\t\tEnter 4 to search a value:"<<endl;
cout<<"\t\tEnter 5 to delete a value:"<<endl;
cout<<"\t\tEnter 6 to display Preorder:"<<endl;
cout<<"\t\tEnter 7 to display Inorder:"<<endl;
cout<<"\t\tEnter 8 to display Postorder:"<<endl;
cout<<"\t\tEnter 9 to display the height of the tree:"<<endl;
cout<<"\t\tEnter 0 to exit:"<<endl;
cout<<"\t\t\n"<<endl;
cout<<"\nEnter the choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n\t\tADDING NEW NODE"<<endl;
cout<<"\t\t:::::::::::::\n"<<endl;
cout<<"Enter a new value: ";
cin>>a;
bst.insert(a,root);
cout<<"\nThe new value have been added to your tree successfully\n"<<endl;
break;
case 2:
if (root !=NULL)
{
min=bst.findmin(root);
cout<<"\nThe minimum element in the tree is: "<<min->element<<endl;
}
break;
case 3:
if (root !=NULL)
{
max=bst.findmax(root);
cout<<"\nThe maximum element in the tree is: "<<max->element<<endl;
}
break;
case 4:
cout<<"\nEnter node to search: ";
cin>>findele;
if (root != NULL)
{
bst.find(findele,root);
}
break;
case 5:
cout<<"\nEnter node to delete: ";
cin>>delele;
bst.del(delele,root);
bst.inorder(root);
cout<<endl;
break;
case 6:
cout<<"\n\t\tPRE-ORDER TRAVERSAL"<<endl;
bst.preorder(root);
cout<<endl;
break;
case 7:
cout<<"\n\t\tIN-ORDER TRAVERSAL"<<endl;
bst.inorder(root);
cout<<endl;
break;
case 8:
cout<<"\n\t\tPOST ORDER TRAVERSAL"<<endl;
bst.postorder(root);
cout<<endl;
break;
case 9:
cout<<"\n\t\tHEIGHT\n"<<endl;
cout<<"The height of the tree is: "<<bst.bsheight(root)<<endl;
break;
case 0:
cout<<"\n\tThank your for using AVL tree program\n"<<endl;
break;
default:
cout<<"Sorry! wrong input\n"<<endl;
break;
}
system("pause");
system("cls");
}
while(choice != 0);
return 0;
}
Write a C++ program to implement Breath firsttraversal tree.

Coding

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
struct node
{
int value;
struct node *left;
struct node *right;
};
typedef struct node Node;
#define MAX(a,b) a>b?a:b
class BFS
{
public:
int height(Node *root)
{
if(root==NULL)
return 0;
if(!root->left &&!root->right)
return 1;
int lheight=height(root->left);
int rheight=height(root->right);
return MAX(lheight,rheight)+1;
}
void printTreeLevel(Node *root)
{
int h=height(root);
//int i;
for(int i=1;i<=h;i++)
{
cout<<"Level "<<i<<" : ";
printTreeLevelRec(root,i);
cout<<"\n";
}
}
void printTreeLevelRec(Node *node,int desired)
{
if(node==NULL)
return;
if(desired==1)
cout<<node->value<<" ";
printTreeLevelRec(node->left,desired-1);
printTreeLevelRec(node->right,desired-1);
}
Node *create_node(int value)
{
Node *temp=(Node *)malloc(sizeof(Node));
temp->value=value;
temp->right=NULL;
temp->left=NULL;

}
Node * addNode(Node *node,int value)
{
if(node==NULL)
{
return create_node(value);
}
else
{
if(node->value>value)
{
node->left=addNode(node->left,value);
}
else
{
node->right=addNode(node->right,value);
}
}
return node;
}
};
int main()
{
BFS obj;
Node *root=NULL;
Node *last=NULL;
Node *ptrToHead=NULL;
root=obj.addNode(root,30);
root=obj.addNode(root,20);
root=obj.addNode(root,15);
root=obj.addNode(root,25);
root=obj.addNode(root,40);
root=obj.addNode(root,37);
root=obj.addNode(root,45);

obj.printTreeLevel(root);
return 0;
}
Write a C++ program to implement Hashing Techniue Modulo
Diviosn Method using Linear Probing.

Coding

#include<iostream>
#include<conio.h>
#define MAX 10
using namespace std;
struct
{
int key;
}
b[MAX];

int hash(int m)
{
int s=0;
s=m%MAX;
return(s);
}
int main()
{
int i,h,num,j,k,flag,m,l=0,d=0;
char c;
for(i=0;i<MAX;i++)
{
b[i].key=-1;
}
do
{
cout<<"\n\n Enter a number: ";
cin>>num;
h=hash(num);
k=h;
flag=1;
do
{
if(b[k].key==-1)
{
cout<<"\nThe location and is free "<<k;
b[k].key=num;
d++;
flag=0;

}
else
{
if(hash(b[k].key==h))
m=k;
cout<<"\n The slot is not free so step forward"<<k;
k=(k+1)%MAX;
l++;
d++;
}
}
while(flag && k!=NULL);
if (flag)
cout<<"\n FULL";
cout<<"\n\nDo you want to continue? y/n: ";
cin>>c;
}
while(c=='y');
cout<<"\n\t\t Table information";
cout<<"\n\t\t\tIndex\tKey\n";
for(i=0;i<MAX;i++)
cout<<"\n\t\t\t"<<i<<"\t"<<b[i].key;
cout<<"Total no of collision:"<<l;
cout<<"\ndensity = "<<d*100/MAX<<"%";
getch();
return 0;
}
Write a c++ Program to implement Modulo Division Method hashing
technique using quadratic probing.

Coding
#include<iostream>
#include<conio.h>
#define MAX 10
using namespace std;
struct
{
int key;
}
b[MAX];
int hash(int m)
{
int s=0;
s=m%MAX;
return(s);
}
int main()
{
inti,h,num,j,k,flag,m,l=0,d=0,sq=1;
char c;
for(i=0;i<MAX;i++)
{
b[i].key=-1;
}
do
{
cout<<"\n\n Enter a number: ";
cin>>num;
h=hash(num);
k=h;
flag=1;
do
{
if(b[k].key==-1)
{
cout<<"\nThe location and is free "<<k;
b[k].key=num;
d++;
flag=0;
}
else
{
if(hash(b[k].key==h))
m=k;
cout<<"\n The slot is not free so step forward"<<k;
if(i==1)
{
k=(k+(sq*sq))%MAX;
l++;
d++;
sq++;
}
else(i<1)
{

k=(k+(sq*sq))%MAX;
l++;
d++;
sq++;
}
}
}
while(flag && k!=NULL);
if (flag)
cout<<"\n FULL";
cout<<"\n\nDo you want to continue? y/n: ";
cin>>c;
}
while(c=='y');
cout<<"\n\t\t Table information";
cout<<"\n\t\t\tIndex\tKey\n";
for(i=0;i<MAX;i++)
cout<<"\n\t\t\t"<<i<<"\t"<<b[i].key;
cout<<"\n\nTotal no of collision:"<<l;
cout<<"\n density = "<<d*100/MAX<<"%";
getch();
return 0;
}
Write a C++ Program to implement Direct Hashing Method using
Linear Probing.

Coding
Write a C++ Program to implement Fold Boundary Method using
Linear Probing

Coding
#include<iostream.h>
#include<conio.h>

//function prototypes
int input(int);
int process(int);
void output(int,int);
int collision(int,int);

//global variable
int arr[19]={0};

//start of main program


main()
{
int key,hash_key,hash_index,m=0;
cout<<"Hash method: FOLD BOUNDARY";
cout<<"\nCollision resolution method: KEY OFFSET";
cout<<"\nEnter 6 digit numbers.";
cout<<"\nEnter 0 as sentinel.";
while(m<19){

hash_key=input(key);
if (hash_key==0) { return 0;}
else
{ hash_index=process(hash_key);
output(hash_key,hash_index); }
} m++;
cout<<"\nEntered numbers execeeded!"; //end of loop
}//end of main program

//start of input function


int input(int x){
cout<<"\n\nEnter key:";
cin>>x;
return x;}
//end of input function

//start of process function


int process(int x){
int a,b,c,d,e,f;
a=x/100000;
b=x%100000/10000;
int v=(b*10)+a;

c=x%10000/1000;
d=x%1000/100;
int q=(c*10)+d;

e=x%10;
f=x%100/10;
int w=(e*10)+f ;

return (w+v+q)%19+1;
} //end of process function
//start of output function
void output(int key,int adres){
int b,z,index1=0,index2=0,index3=0,index4=0,u;
bool wer;
cout<<"\n\tList\tKey\n";

while (arr[adres-1]!=0)
{ index1=collision(key,adres);
arr[index1-1]=key;break;

if (arr[index1-1]!=0)
{ index2=collision(key,index1);
arr[index2-1]=key;

if(arr[index2-1]!=0)
{ index3=collision(key,index2);
arr[index3-1]=key;
}
}
}

if (arr[adres-1]==0)
{ arr[adres-1]=key; }

for(u=0;u<19;u++){
z=arr[u];
cout<<"\t"<<(u+1)<<".\t"<<z<<endl; }
}

//end of output function


//start of collision resolution method
int collision(int key,int hashed){
int offset,adrs;
offset=(key/19;
adrs=((offset + hashed) % 19 )+1;
return adrs;
}
//end of collision resolution method
Write a C++ Program to implement Fold Shift Method Using Linear
Probing

Coding

#include<iostream.h>
#include<conio.h>

//function prototypes
int input(int);
int process(int);
void output(int,int);
int collision(int,int);

//global variable
int arr[19]={0};

//start of main program


main()
{
int key,hash_key,hash_index,m=0;
cout<<"Hash method: FOLD BOUNDARY";
cout<<"\nCollision resolution method: KEY OFFSET";
cout<<"\nEnter 6 digit numbers.";
cout<<"\nEnter 0 as sentinel.";
while(m<19){

hash_key=input(key);
if (hash_key==0) { return 0;}
else
{ hash_index=process(hash_key);
output(hash_key,hash_index); }
} m++;
cout<<"\nEntered numbers execeeded!"; //end of loop
}//end of main program

//start of input function


int input(int x){
cout<<"\n\nEnter key:";
cin>>x;
return x;}
//end of input function

//start of process function


int process(int x){
int a,b,c,d,e,f;
a=x/100000;
b=x%100000/10000;
int v=b+a;

c=x%10000/1000;
d=x%1000/100;
int q=c+d;

e=x%10;
f=x%100/10;
int w=e+f ;

return (w+v+q)%19+1;
} //end of process function
//start of output function
void output(int key,int adres){
int b,z,index1=0,index2=0,index3=0,index4=0,u;
bool wer;
cout<<"\n\tList\tKey\n";

while (arr[adres-1]!=0)
{ index1=collision(key,adres);
arr[index1-1]=key;break;

if (arr[index1-1]!=0)
{ index2=collision(key,index1);
arr[index2-1]=key;

if(arr[index2-1]!=0)
{ index3=collision(key,index2);
arr[index3-1]=key;
}
}
}

if (arr[adres-1]==0)
{ arr[adres-1]=key; }

for(u=0;u<19;u++){
z=arr[u];
cout<<"\t"<<(u+1)<<".\t"<<z<<endl; }
}

//end of output function


//start of collision resolution method
int collision(int key,int hashed){
int offset,adrs;
offset=(key/19;
adrs=((offset + hashed) % 19 )+1;
return adrs;
}
//end of collision resolution method
Write a C++ program to implement Maximum Heap Tree using array.

Coding

#include<iostream>
#include<conio.h>
#define MAX 100
using namespace std;
void heapif(int *a,int i,int n);
int main()
{
int i,n,a[MAX];
cout<<"\n Enter the number of elements: ";
cin>>n;
cout<<"\n Enter the Elements:";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=n/2;i>=0;i--)
{
heapif(a,i,n);
}
cout<<"\n The array elements after constructing the heap: \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
getch();
}
void heapif(int *a,int i,int n)
{
int l,r,largest,temp;
l=2*i+1;
r=2*i+2;
if(l<n && a[l]>a[i])
{
largest=l;
}
else
{
largest=i;
}
if(r<n && a[r]>a[largest])
{
largest=r;
}
if(largest!=i)
{
temp=a[i];
a[i]=a[largest];
a[largest]=temp;
heapif(a,largest,n);
}
}
Write a C++ program to implement Minimum Heap Tree using array.

Coding

#include<iostream>
#include<conio.h>
#define MAX 100
using namespace std;
void heapif(int *a,int i,int n);
int main()
{
int i,n,a[MAX];
cout<<"\n Enter the number of elements: ";
cin>>n;
cout<<"\n Enter the Elements:";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=n/2;i>=0;i--)
{
heapif(a,i,n);
}
cout<<"\n The array elements after constructing the heap: \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
getch();
}
void heapif(int *a,int i,int n)
{
int l,r,largest,temp;
l=2*i+1;
r=2*i+2;
if(l<n && a[l]<a[i])
{
largest=l;
}
else
{
largest=i;
}
if(r<n && a[r]<a[largest])
{
largest=r;
}
if(largest!=i)
{
temp=a[i];
a[i]=a[largest];
a[largest]=temp;
heapif(a,largest,n);
}
}
Write a C++ program to perform insert & delete operation using heap
sort array.

Coding

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int array[20],n;
void display()
{
int i;
if(n==0)
{
cout<<"Heap is empty\n";
return;
}
for(i=0;i<n;i++)
cout<<array[i]<<"\t";
cout<<endl;
}
void insert(int num,int location)
{
int parentnode;
while(location>0)
{
parentnode=(location-1)/2;
if(num<=array[parentnode])
{
array[location]=num;
return;
}
array[location]=array[parentnode];
location=parentnode;
}
array[0]=num;
}
void Delete(int num)
{
int left,right,i,temp,parentnode;
for(i=0;i<num;i++)
{
if(num==array[i])
break;
}
if(num!=array[i])
{
cout<<num<<" Not found in heap list\n";
return;
}
array[i]=array[n-1];
n=n-1;
parentnode=(i-1)/2;
if(array[i]>array[parentnode])
{
insert(array[i],i);
return;
}
left=2*i+1;
right=2*i+2;
while(right<n)
{
if(array[i]>=array[left] && array[i]>=array[right])
return;
if(array[right]<=array[left])
{
temp=array[i];
array[i]=array[left];
array[left]=temp;
i=left;
}
else
{
temp=array[i];
array[i]=array[right];
array[right]=temp;
i=right;
}
left=2*i+1;
right=2*i+2;
}
if(left==n-1)
{
temp=array[i];
array[i]=array[left];
array[left]=temp;
}
}
int main()
{
int choice,num,t;
n=0;
do
{
cout<<"1.Insert the element\n";
cout<<"2.Delete the element\n";
cout<<"3.Display all element\n";
cout<<"4.quit\n";
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the element to be inserted:";
cin>>num;
insert(num,n);
n=n+1;
break;
case 2:
cout<<"Enter the element to be deleted:";
cin>>num;
Delete(num);
break;
case 3:
display();
break;
case 4:
exit(0);
}
cout<<"Enter 1:to continue...\n";
cout<<"Enter 2:to exit...\n\n";
cin>>t;
}
while(t==1);
getch();
}
Write a C++ program to implement Breadth First Search in Graph.

Coding
#include<iostream>
#include<conio.h>
using namespace std;
int route[10][10],i,j,k,m,n,queue[10],rear,v,visit[10],visited[10],front;
int main()
{
cout<<"Enter No. of Vertices: ";
cin>>n;
cout<<"Enter No. of Edges: ";
cin>>m;
cout<<"Edges between Vertices: \n";
for(k=1;k<=m;k++)
{
cin>>i>>j;
route[i][j]=1;
}
cout<<"Enter Initial Vertex to Insert: ";
cin>>v;
cout<<"BFS Order is: ";
cout<<v<<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=1;j<=n;j++)
if(route[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
queue[rear++]=j;

}
v=queue[front++];
cout<<v<<" ";
k++;
visit[v]=0;
visited[v]=1;
}
getch();
return 0;
}
Write a C++ program to implement Depth First Search Traversal in
Tree.

Coding

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,stk[10],top,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 <<"DFS ORDER OF VISITED VERTICES:";
cout << v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n; j>=1; j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
cout<<v << " ";
k++;
visit[v]=0;
visited[v]=1;
}
getch();
return 0;
}
Write a C++ program to implement Insert & Delete Matrix.

Coding

#include<iostream>
#include<process.h>
#include<conio.h>
#define max 20
using namespace std;

void Create_graph();
void Insert_node();
void delete_node(int u);
void display();
void insert_edge(int u,int v);
void del_edge(int u,int v);
int adj[max][max];
int n;
int main()
{
int choice;
int node,origin,dest,u,v;
Create_graph();
while(1)
{
cout<<"\n1. Insert node ";
cout<<"\n2. Delete node ";
cout<<"\n3. Display";
cout<<"\n4. Insert egde";
cout<<"\n5. Delete egde";
cout<<"\n6. Exit";
cout<<"\nEnter choice : ";
cin>>choice;
switch(choice)
{
case 1:
Insert_node();
break;
case 2:
cout<<"\nEnter node to delete : " ;
fflush(stdin);
cin>>node;
delete_node(node);
break;
case 3:
display();
break;
case 4:
cout<<"\nEnter insert edge :";
cin>>u>>v;
insert_edge(u,v);
break;
case 5:
cout<<"\nEnter delete edge :";
cin>>u>>v;
del_edge(u,v);
break;
case 6:
exit(0);
default:
cout<<"Wrong Choice ";
}
}
getch();
}

void Create_graph()
{
int i,maxedge,origin,dest;
cout<<"\nEnter no. of nodes : ";
cin>>n;
maxedge=n*(n-1);
for(int i=1;i<=maxedge;i++)
{
cout<<"\nEnter edge "<<i<<" : (0,0) to quit ";
cin>>origin>>dest;
if(origin==0 && dest==0)
break;
if(origin>n || dest >n || origin <=0 || dest <=0)
{
cout<<"\nInvalid Edge ";
i--;
}
else
adj[origin][dest]=1;
}
}
void display()
{
int i,j;
for(int i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<" "<<adj[i][j];
}
cout<<"\n";
}
}
void Insert_node()
{
int i;
n++;
cout<<"\nThe inserted node is "<<n<<" \n ";
for(i=1;i<=n;i++)
{
adj[i][n]=0;
adj[n][i]=0;
}
}
void delete_node(int u)
{
int i,j;
if(n==0)
{
cout<<"\nGraph is empty ";
return;
}
for(i=u;i<=n;i++)
{
for(j=1;j<=n;j++)
{
adj[j][i]= adj[j][i+1];
adj[i][j]=adj[i+1][j];
}
n--;
}
}
void insert_edge(int u,int v)
{
if(u>n)
{
cout<<"\nSource node not exist";
return;
}
if(v>n)
{
cout<<"\nDestination does not exist";
return;
}
adj[u][v]=1;
}
void del_edge(int u,int v)
{
if(u>v || v>n || adj[u][v]==0)
{

cout<<"\nEdge does not exist";


return;
}
adj[u][v]=0;
}
Write a C++ program for implementation of graph traversal &
minimum spanning tree using Prim’s Algorithm.

Coding

#include<iostream>
#include<conio.h>
#define Row 7
#define col 7
#define infi 9999
using namespace std;
class Prims
{
int graph[Row][col],nodes;
public:
prims();
void create_graph();
void Prims_Algo();
};
Prims::prims()
{
for(int i=0;i<Row;i++)
for(int j=0;j<col;j++)
graph[i][j]=0;
}
void Prims::create_graph()
{
int i,j;
cout<<"Enter a nodes: \n";
cin>>nodes;
cout<<"Enter Row matrix: \n";
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
cin>>graph[i][j];
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
if(graph[i][j]==0)
graph[i][j]=infi;
}
}
}
void Prims::Prims_Algo()
{
int selected[Row],i,j,n;
int f=0,t=1,min,x,y;
for(i=0;i<nodes;i++)
selected[i]=f;
selected[0]=t;
n=0;
while(n<nodes-1)
{
min=infi;
for(i=0;i<nodes;i++)
{
if(selected[i]==t)
{
for(j=0;j<nodes;j++)
{
if(selected[j]==f)
{
if(min>graph[j][i])
{
min=graph[i][j];
x=i;
y=j;
}
}
}
}
}
selected [y]=t;
cout<<"\n"<<x+1<<"-->"<<y+1;
n=n+1;
}
}
int main()
{
Prims p;
cout<<"Adjacent Matrix: \n";
p.create_graph();
cout<<"Minimum Cost Spanning Tree: \n";
p.Prims_Algo();
p.prims();
getch();
return 0;
}
Write a C++ program for implementation of graph traversal &
minimum spanning tree using Kruskal’s Algorithm.

Coding

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{
clrscr();
printf("\n\tImplementation of Kruskal's algorithm\n");
printf("\nEnter the no. of vertices:");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("The edges of Minimum Cost Spanning Tree are\n");
while(ne < n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j <= n;j++)
{
if(cost[i][j] < min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost = %d\n",mincost);
getch();
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}
Write a C++ program to implement a Warshall’s Algorithm.

Coding

#include <iostream>
#include <conio.h>
using namespace std;

void floyds(int b[][7])


{
int i, j, k;
for (k = 0; k < 7; k++)
{
for (i = 0; i < 7; i++)
{
for (j = 0; j < 7; j++)
{
if ((b[i][k] * b[k][j] != 0) && (i != j))
{
if ((b[i][k] + b[k][j] < b[i][j]) || (b[i][j] == 0))
{
b[i][j] = b[i][k] + b[k][j];
}
}
}
}
}
for (i = 0; i < 7; i++)
{
cout<<"\nMinimum Cost With Respect to Node:"<<i<<"\n -->";
for (j = 0; j < 7; j++)
{
cout<<b[i][j]<<"\t";
}
cout<<"\n\n";
}
}
int main()
{
int b[7][7];
cout<<"\nENTER VALUES OF ADJACENCY MATRIX\n\n";
for (int i = 0; i < 7; i++)
{
cout<<"Enter values for "<<(i+1)<<" row"<<endl;
for (int j = 0; j < 7; j++)
{
cin>>b[i][j];
}
}
floyds(b);
getch();
}
Write a C++ program for implementation of shortest path algorithm
for given undirected graph using Djikstra’s Algorithm.

Coding
#include<iostream>
#include "stdio.h"
#include "conio.h"
#define infinity 999
using namespace std;
void dij(int n,int v,int cost[10][10],int dist[])
{
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
{
min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
min=dist[w],u=w;
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w];
}
}

main()
{
int n,v,i,j,cost[10][10],dist[10];

printf("\n Enter the number of nodes:");


scanf("%d",&n);
printf("\n Enter the cost matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity;
}
printf("\n Enter the source matrix:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("\n Shortest path:\n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%d\n",v,i,dist[i]);
getch();
}

You might also like