1. IMPLEMENTATION OF QUEUE USING LINKED LIST
#include<iostream.h>
#include<process.h>
#include<conio.h>
class equeue
{
struct node
{
int info;
node *next;
}*front,*rear;
public:equeue()
{
front=rear=NULL;
}
void insert(void);
void deleted(void);
void display(void);
};
void equeue::insert()
{
node *temp=new node;
cout<<"n Enter Value To Insert:";
cin>>temp->info;
temp->next=NULL;
if(front==NULL)
{
front=rear=temp;
}
else
{
rear->next=temp;
rear=rear->next;
}
}
void equeue::deleted()
{
if(front==NULL)
{
cout<<"n Queue Is Emptyn";
}
else
2. {
if(front==rear)
{
cout<<"n Deleted Element Is :"<<front->info;
front=rear=NULL;
}
else
{
node *temp=front;
cout<<"n Deleted Element Is :"<<temp->info;
front=front->next;
delete(temp);
}
}
}
void equeue::display()
{
if(front==NULL)
cout<<"Queue Is Emptyn";
else
{
node *t=front;
while(t->next!=NULL)
{
cout<<t->info<<"n";
t=t->next;
}
cout<<t->info;
}
}
void main()
{
equeue c;
int ch;
clrscr();
while(1)
{
cout<<"n 1.Insertn 2.Deleten 3.Displayn 4.Exitn";
cout<<"n Enter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:c.insert();
3. break;
case 2:c.deleted();
break;
case 3:c.display();
break;
case 4:exit(0);
break;
default:cout<<"n Enter The Right Choice";
}
}
}
OUTPUT:
1.Insert
2.Delete
3.Display
4.Exit
Enter Your Choice: 1
Enter the value to insert: 11
1.Insert
2.Delete
3.Display
4.Exit
Enter Your Choice: 1
Enter the value to insert:22
1.Insert
2.Delete
3.Display
4.Exit
Enter Your Choice: 1
Enter the value to insert:33
1.Insert
2.Delete
3.Display
4.Exit
Enter Your Choice: 3
11 22 33
4. 1.Insert
2.Delete
3.Display
4.Exit
Enter Your Choice:2
Deleted Element is: 11
1.Insert
2.Delete
3.Display
4.Exit
Enter Your Choice:2
Deleted Element is: 22
1.Insert
2.Delete
3.Display
4.Exit
Enter Your Choice:2
Deleted Element is: 33
1.Insert
2.Delete
3.Display
4.Exit
Enter Your Choice:3
Queue is Empty
8. }
void main()
{
int ch1,ch2,num,nd;
dllist st;
clrscr();
while(1)
{
cout<<"n1.Insertn2.Deleten3.Displayn4.Exitn";
cin>>ch1;
switch(ch1)
{
case 1:
{
cout<<"nInsert :n1.Beginningn2.Endn3.Middlen";
cin>>ch2;
switch(ch2)
{
case 1:
{
cout<<"enter the element:";
cin>>num;
st.insertbegin(num);
break;
}
case 2:
{
cout<<"enter the element:";
cin>>num;
st.insertend(num);
break;
}
case 3:
{
cout<<"enter the node after which to insert:";
cin>>nd;
cout<<"enter the element:";
cin>>num;
st.insertmiddle(num,nd);
break;
}
default: cout<<"enter the correct choice";
}
break;
9. }
case 2:
{
cout<<"nDelete:n1.Beginningn2.Endn3.Middle";
cin>>ch2;
switch(ch2)
{
case 1:
{
cout<<"Deletion fron beginning:";
st.deletebegin();
break;
}
case 2:
{
cout<<"Deletion from the end:";
st.deleteend();
break;
}
case 3:
{
cout<<"enter the node to be deleted:";
cin>>nd;
st.deletemiddle(nd);
break;
}
default: cout<<"enter the correct choice";
}
break;
}
case 3:
{
st.display();
break;
}
case 4:exit(0);
default: cout<<"enter the correct choice";
}
}
}
10. OUTPUT:
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 1
Insert:
1.Beginning
2.End
3.Middle
1
Enter the element:11
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Insert:
1.Beginning
2.End
3.Middle
2
Enter the element:22
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Insert:
1.Beginning
2.End
3.Middle
3
Enter the node after which to insert:11
Enter the element:33
1.Insert
2.Delete
3.Display
4.Exit
11. Enter your choice:3
11->33->22
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:2
Delete:
1.Beginning
2.End
3.Middle
3
Enter the node to be deleted: 33
Deleted element :33
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:2
Delete:
1.Beginning
2.End
3.Middle
1
Deletion from the beginning 11
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:2
Delete:
1.Beginning
2.End
3.Middle
2
Deletion from the end 22
12. IMPLEMNTATION OF BINARY SEARCH TREE NON RECURSIVE
TRAVERSAL
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
#include<process.h>
struct bstree
{
int data;
struct bstree*left;
struct bstree*right;
}*root=NULL;
class bst
{
private:
public :
void insert(bstree *,int);
void inorder_non_rec(bstree*);
void preorder_non_rec(bstree*);
void postorder_non_rec(bstree*);
};
class stack
{
int top;
bstree *stackel[20];
public:
stack()
{
top=-1;
}
void push(bstree *);
bstree* pop();
int empty()
{
if(top==-1)
return(1);
else
return(0);
}
};
16. void bst::inorder_non_rec(bstree *root)
{
stack stk;
bstree *temp;
if(root!=NULL)
{
temp=root;
do
{
while(temp!=NULL)
{
stk.push(temp);
temp=temp->left;
}
if(!stk.empty())
{
temp=stk.pop();
cout<<temp->data<<" ";
temp=temp->right;
}
else
break;
}while(1);
}
else
cout<<"Empty tree";
}
void main()
{
int info,opt;
char choice;
clrscr();
bst b;
do
{
cout<<"n 1-Insert n 2-Inorder n 3-Preordern 4-Postord n 5. Exitn";
cout<<"n ENTER YOUR CHOICE:";
cin>>opt;
switch(opt)
{
case 1: cout<<"n enter the number :";
cin>>info;
b.insert(root,info);
break;
17. case 2: cout<<"n Inorder n non recursive display is:";
b.inorder_non_rec(root);
break;
case 3: cout<<"nPreorder n non recursive display is:";
b.preorder_non_rec(root);
break;
case 4: cout<<"n Post order n non recursive display is:";
b.postorder_non_rec(root);
break;
case 5: exit(0);
}
}while(1);
}
OUTPUT
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 1
Enter the element: 2
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 1
Enter the element: 67
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 1
Enter the element: 12
1.Insert
2.Inorder
3.Preorder
18. 4.Postorder
5.Exit
Enter your choice: 1
Enter the element: 45
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 1
Enter the element: 74
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 1
Enter the element: 21
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 2
In order
Non recursive display: 2 12 21 45 67 74
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 3
Pre order
Non recursive display: 2 67 12 45 21 74
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 4
Post order
Non recursive display: 21 45 12 74 67 2
19. IMPLEMENTATION OF BINARY SEARCH TREE RECURSIVE TRAVERSAL
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
#include<process.h>
struct bstree
{
int data;
struct bstree*left;
struct bstree*right;
}*root=NULL;
class bst
{
private:
public :
void insert(bstree *,int);
void inorder(bstree*);
void postorder(bstree *);
void preorder(bstree *);
};
class stack
{
int top;
bstree *stackel[20];
public:
stack()
{
top=-1;
}
void push(bstree *);
bstree* pop();
int empty()
{
if(top==-1)
return(1);
else
return(0);
}
};
22. void bst :: preorder(struct bstree*head)
{
if( head != NULL)
{
cout<<head->data;
preorder(head->left);
preorder(head->right);
}
}
void bst::inorder(bstree *root)
{
stack stk;
bstree *temp;
temp=root;
if(temp!=NULL)
{
inorder(temp->left);
cout<<temp->data;
inorder(temp->right);
}
}
void main()
{
int info,opt;
char choice;
clrscr();
bst b;
do
{
cout<<"n 1-Insert n 2-Inorder n 3-Preordern 4-Postorder n 5. Exitn";
cout<<"n ENTER YOUR CHOICE:";
cin>>opt;
switch(opt)
{
case 1: cout<<"n enter the number :";
cin>>info;
b.insert(root,info);
break;
case 2:cout<<"n recursive display is:";
b.inorder(root);
break;
23. case 3:cout<<"n recursive display is:";
b.preorder(root);
break;
case 4:cout<<"n recursive display is:";
b.postorder(root);
break;
case 5: exit(0);
}
}while(1);
}
OUTPUT
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 1
Enter the element: 2
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 1
Enter the element: 67
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 1
Enter the element: 12
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 1
24. Enter the element: 45
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 1
Enter the element: 74
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 1
Enter the element: 21
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 2
In order
Recursive display: 2 12 21 45 67 74
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 3
Pre order
Recursive display: 2 67 12 45 21 74
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 4
Post order
Recursive display: 21 45 12 74 67 2
25. IMPLEMENTATION OF Breadth First Search
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
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";
cin >>v;
cout <<"Visitied verticesn";
cout << v;
visited[v]=1;
k=1;
while(k<n)
{
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;
}
}
26. OUTPUT:
Enter the no of vertices: 9
Enter the no of edges:9
EDGES
1 2
2 3
1 5
1 4
4 7
7 8
8 9
2 6
5 7
Enter initial vertex: 1
Visited vertices
1 2 4 5 3 6 7 8 9
27. IMPLEMENTATION OF Depth First Search
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente 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";
cin >>v;
cout <<" 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;
}
}
28. OUTPUT:
Enter the no of vertices: 9
Enter the no of edges:9
EDGES
1 2
2 3
1 5
1 4
4 7
7 8
8 9
2 6
5 7
Enter initial vertex: 1
Visited vertices
1 2 3 6 4 7 8 9 5
29. IMPLEMENTATION OF HEAP SORT
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void heapify(int a[],int i);
void buildheap(int a[]);
void heapsort(int a[]);
int n;
void main()
{
int a[100];
clrscr();
cout<<"enter n valuen";
cin>>n;
int l=n;
cout<<"enter array of elementsn";
for(int i=0;i<n;i++)
cin>>a[i];
heapsort(a);
cout<<"the sorted list of elements are:"<<endl;
for(i=0;i<l;i++)
cout<<a[i]<<endl;
getch();
}
void heapify(int a[],int i)
{
int l,r,large;
l=2*i+1;
r=2*i+2;
if(l<n&&a[l]>=a[i])
large=1;
else
large=i;
if(r<n&&a[r]>=a[large])
large=r;
if(i!=large)
{
int t;
t=a[i];
a[i]=a[large];
a[large]=t;
heapify(a,large);
}
}
30. void buildheap(int a[])
{
int i;
for(i=(n/2)-1;i>=0;i--)
heapify(a,i);
}
void heapsort(int a[])
{
buildheap(a);
for(int i=n-1;i>=1;i--)
{
int t;
t=a[0];
a[0]=a[i];
a[i]=t;
n--;
heapify(a,0);
}
}
OUTPUT:
enter n value
4
enter array of elements
5
6
3
2
the sorted list of elements are:
2
3
5
6
31. IMPLEMENTATION OF PROGRAM TO CONVERT INFIX TO POSTFIX FORM
#include<iostream.h>
#include<conio.h>
#include<string.h>
#define MAXSIZE 100
class STACK_ARRAY
{
int stack[MAXSIZE];
int Top;
public:
STACK_ARRAY()
{
Top=-1;
}
void push(char);
char pop();
int prec(char);
void Infix_Postfix();
};
void STACK_ARRAY::push(char item)
{
if (Top == MAXSIZE-1)
{
cout<<"nThe Stack Is Full";
getch();
}
else
stack[++Top]=item;
}
char STACK_ARRAY::pop()
{
char item='#';
if (Top == -1)
cout<<"nThe Stack Is Empty. Invalid Infix expression";
else
item=stack[Top--];
return(item);
}
int STACK_ARRAY::prec(char symbol)
{
switch(symbol)
{
case '(':
return(1);
32. case ')':
return(2);
case '+':
case '-':
return(3);
case '*':
case '/':
case '%':
return(4);
case '^':
return(5);
default:
return(0);
}
}
void STACK_ARRAY::Infix_Postfix()
{
int len,priority;
char infix[MAXSIZE],postfix[MAXSIZE],ch;
cout<<"nnEnter the infix expression = ";
cin>>infix;
len=strlen(infix);
infix[len++]=')';
push('(');
for(int i=0,j=0;i<len;i++)
{
switch(prec(infix[i]))
{
case 1:
push(infix[i]);
break;
case 2:
ch=pop();
while(ch != '(')
{
postfix[j++]=ch;
ch=pop();
}
break;
case 3:
ch=pop();
while(prec(ch) >= 3)
{
postfix[j++]=ch;
ch=pop();
34. }while(choice == 'Y' || choice == 'y');
}
OUTPUT:
Enter the infix expression:
A+B
The postfix expression is
AB+
Do you wish to continue(Y/y)= y
Enter the infix expression:
A+(B-C)
The postfix expression is
ABC-+
Do you wish to continue(Y/y)=n