Professional Documents
Culture Documents
LIST OF EXPERIMENTS
S.NO.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
i. Array Implementation
ii. Linked List Implementation
Stack Application String Reverse
i. Array Implementation
ii. Linked List Implementation
Stack Application (expression conversion ) - Infix to
prefix
i. Array Implementation
ii. Linked List Implementation
QUEUE ADT
i. Array Implementation
ii. Linked List Implementation
Search Tree ADT Binary Search Tree
Quick Sort
OUTPUT
*** STUDENT DETAILS
***
Enter name, Depatment,
regno and college :
Raja
CSE
210
SSMIET
NAME : Raja
DEPARTMENT : CSE
REG. NO : 210
COLLEGE : SSMIET
OUTPUT
*** SUM OF N
NATURAL
NUMBERS ***
Enter the no. of terms : 3
The sum of 3 natural
numbers : 6
number :"<<sum;
OUTPUT
Compilatiopn 1
*** Displaying time in
two formats ***
Enter the time
18 18 34
24 hours format 18:18:34
12 hours format 6:18:34
OUTPUT
Compilatiopn 2
*** Displaying time in
two formats ***
Enter the time
10 23 45
24 hours format 10:23:45
12 hours format 10:23:45
OUTPUT
Compilatiopn 3
*** Displaying time in
two formats ***
Enter the time
54 23 45
WRONG INPUT DATA
obj.display();
getch();
}
b. FACTORIAL OF THE GIVEN NUMBER
#include<iostream.h>
#include<conio.h>
class factorial
{
public:
intn,i,fact;
void getdata();
void calc();
void display();
};
OUTPUT Compilation 1
void factorial::getdata()
{
*** FACTORIAL OF THE GIVEN NUMBER ***
cout<<"\nEnter the number : ";
Enter the number : 5
cin>>n;
The factorial of the given number is 120
}
void factorial::calc()
{
fact=1;
OUTPUT Compilation 2
if(n==0)
display();
*** FACTORIAL OF THE GIVEN NUMBER ***
else
Enter the number : 0
{
The factorial of the given number is 1
for(i=1;i<=n;i++)
fact=fact*i;
}
}
void factorial::display()
{
cout<<"\nThe factorial of the given number is"<<f;
}
void main()
{
clrscr();
cout<<"*** FACTORIAL OF THE GIVEN NUMBER ***\n";
factorial obj;
obj.getdata();
obj.calc();
obj.display();
getch();
}
c. REFERENCE VARIABLE
#include<iostream.h>
#include<conio.h>
class reference
{
public:
void display();
};
void reference::display()
{
float total=100;
float &sum=total;
cout<<Initial Values of total & Sum : \n\n;
cout<<'\n'<<"Total"<<'\t'<<total;
cout<<'\n'<<"Sum"<<'\t'<<sum<<endl;
cout<<\nAfter adding 10 to total :\n;
total=total+10;
cout<<'\n'<<"Total"<<'\t'<<total;
cout<<'\n'<<"Sum"<<'\t'<<sum;
cout<<\nAfter assigning 0 to sum :\n;
sum=0;
cout<<'\n'<<"total"<<'\t'<<total;
cout<<'\n'<<"sum"<<'\t'<<sum;
}
void main()
{
clrscr();
cout<<"*** REFERENCE VARIABLE ***\n";
reference obj;
obj.display();
getch();
}
OUTPUT
*** REFERENCE
VARIABLE ***
Initial Values of total &
sum :
Total 100
Sum 100
After adding 1o to total
Total 110
Sum 110
After assigning 0 to sum
Total 0
Sum 0
e. FUNCTION OVERLOADING
#include<iostream.h>
#include<conio.h>
class calc
{
inta,b,c;
float avg;
public:
void average(inta,int b);
void average(inta,intb,int c);
};
void calc::average(inta,int b)
{
avg=(a+b)/2;
cout<<"\nThe average of <<a<< and <<b<< is"<<"\t"<<avg;
}
void calc::average(inta,intb,int c)
{
avg=(a+b+c)/3;
cout<<"\nThe average of <<a<< , <<b<< and <<c<< is"<<"\t"<<avg;
}
void main()
{
clrscr();
calc obj
cout<<"*** AVERAGE USING FUNCTION OVERLOADING ***\n\n";
obj.average(10,20);
obj.average(10,20,30);
getch();
}
OUTPUT
*** AVERAGE USING
FUNCTION
OVERLOADING ***
The average of 10 and 20
is 15
The average of 10 , 20 and
30 is
20
f. OPERATOR Overloading
(i)
#include<iostream.h>
#include<conio.h>
class unary
{
intx,y,z;
inta,b,c;
public:
void getdata(inta,intb,int c);
void display();
void operator -();
};
void unary::getdata(inta,intb,int c)
{
cout<<endl;
x=a;
y=b;
z=c;
}
void unary::display()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z<<endl;
}
void unary::operator -()
{
x=-x;
y=-y;
z=-z;
}
void main()
{
clrscr();
cout<<"*** Unary operator overloading ***\n\n";
unary obj;
intm,n,o;
cout<<"\nEnter the values of 3 variables: \n";
cin>>m>>n>>o;
s.getdata(m,n,o);
cout<<"\nValues before overloading\n";
s.display();
//Unary operator is applied on the object created
OUTPUT
*** Unary operator
overloading ***
Enter the values of 3
variables:
5
-6
7
Values before overloading
x=5
y = -6
z=7
Values after overloading
x = -5
y=6
z = -7
s.display();
-s;
cout<<"\nValues after overloading\n";
s.display();
getch();
}
(ii)
#include<iostream.h>
#include<conio.h>
class complex
{
double a,b;
public:
complex()
{
}
complex(double real,double img)
{
a=real;
b=img;
}
complex operator +(complex c)
{
complex temp;
temp.a=a+c.a;
temp.b=b+c.b;
return(temp);
}
complex operator -(complex c)
{
complex temp;
temp.a=a-c.a;
temp.b=b-c.b;
return(temp);
}
complex operator *(complex c)
{
complex temp;
temp.a=a*c.a;
temp.b=b*c.b;
return(temp);
}
complex operator /(complex c)
{
complex temp;
temp.a=a/c.a;
temp.b=b/c.b;
return(temp);
}
void display()
{
cout<<a<<"+"<<b<<"j";
}
};
void main()
{
clrscr();
complex c1,c2,c3;
cout<<"*** Binary Operator overloading ***";
c1=complex(8,4);
c2=complex(2,2);
cout<<"\nTwo complex numbers \n";
cout<<"c1=";
c1.display();
cout<<"\nc2=";
c2.display();
cout<<"\n\nAddition : c3 = c1 + c2";
c3=c1+c2;
cout<<"\nRESULT = ";
c3.display();
cout<<"\n\nSubtraction : c3 = c1 - c2";
c3=c1-c2;
cout<<"\nRESULT = ";
c3.display();
cout<<"\n\nMultiplication : c3 = c1 * c2";
c3=c1*c2;
cout<<"\nRESULT = ";
c3.display();
cout<<"\n\nDivision : c3 = c1 / c2";
c3=c1/c2;
cout<<"\nRESULT = ";
c3.display();
getch();
}
OUTPUT
void arr::search()
{
cout<<"\nEnter the element to be searched";
cin>>sea;
for(i=0;i<x;i++)
{
if(sea==m[i])
{
cout<<"\nThe position of the element is"<<i+1;
}
else
{
cout<<"\nElement is not in the given list of arrays";
}
}
}
void arr::erase()
{
cout<<"\nEnter the element to be deleted";
cin>>del;
for(i=0;i<x;i++)
{
if(del==m[i])
{
m[i]=0;
}
else
{
cout<<"\nm[i]="<<m[i];
}
}
}
void arr::insert()
{
cout<<"\nenter the no.of variables to be inserted";
cin>>p;
cout<<"\ninsert the variables";
for(i=0;i<p;i++)
{
cin>>m[x++];
}
}
void main()
{
clrscr();
arr a;
int n;
do
{
cout<<"\n1.create\n2.display\n3.search\n4.delete\n5.insert\n6.exit";
cout<<"\nEnter the choice";
cin>>n;
switch(n)
{
case 1: a.create();
break;
case 2: a.display();
break;
case 3: a.search();
break;
case 4: a.erase();
break;
case 5: a.insert();
break;
case 6: exit(1);
break;
default:cout<<"\nINVALID";
break;
}
}while(n<6);
getch();
}
OUTPUT
1. create
2. display
3. search
4. erase
5. insert
6. exit
Enter the choice 1
Enter the no. of elements 5
Enter the array elements 1
2
3
4
5
1. create
2. display
3. search
4. erase
5. insert
6. exit
Enter the choice 2
The entered array is
1
2
3
4
5
1. create
2.display
3.search
4.erase
5.insert
6.exit
Enterthechoice3
Entertheelementtobesearched2
Notfound
Locationoftheelementis2
Notfound
Notfound
Notfound
1.create
2.display
3.search
4.erase
5.insert
6.exit
Enterthechoice4
Entertheelementtobedeleted5
m[i]=1
m[i]=2
m[i]=3
m[i]=4
1.create
2.display
3.search
4.erase
5.insert
6.exit
Enterthechoice2
Theenteredarrayis
1
2
3
4
0
1.create
2.display
3.search
4.erase
5.insert
6.exit
Enterthechoice5
Entertheno.ofelementstobeinserted3
Insertthevariables99
88
77
1.create
2.display
3.search
4.erase
5.insert
6.exit
Enterthechoice6
delete temp;
break;
}
prev=temp;
temp=temp->next;
}
}
void sll::delbeg()
{
temp=head;
head=head->next;
delete temp;
}
void sll::inspos()
{
temp=head;
nnode=new node;
cout<<" enter the element after which new element is to be inserted";
cin>>pos;
while(temp->next!=NULL)
{
if(pos==temp->data)
{
cout<<"\nEnter data to be inserted";
cin>>nnode->data;
nnode->next=temp->next;
temp->next=nnode;
break;
}
else
{
temp=temp->next;
}
}
}
void sll::insend()
{
temp=head;
nnode=new node;
cout<<"\n enter the data:";
cin>>nnode->data;
nnode->next=NULL;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=nnode;
}
void sll::insbeg()
{
temp=head;
nnode=new node;
case 4: s.delbeg();
s.display();
break;
case 5: s.delend();
s.display();
break;
case 6: s.delpos();
s.display();
break;
case 7: s.insbeg();
s.display();
break;
case 8: s.insend();
s.display();
break;
case 9: s.inspos();
s.display();
break;
}
}while(m<10);
getch();
}
OUTPUT
SINGLE LINKED LIST
1.create
2.display
3.search
4.delete at the beginning
5.delete at the end
6.delete at the given position
7.insert at the beginning
8.insert at the end
9.insert at the given position
10.exit
enter you choice:1
enter the number of element to be created:5
enter the new data:10
enter the new data:20
enter the new data:30
enter the new data:40
enter the new data:50
1.create
2.display
3.search
4.delete at the beginning
5.delete at the end
6.delete at the given position
7.insert at the beginning
8.insert at the end
9.insert at the given position
10.exit
4.
==============
//cursorHeader.h
==============
#include<iostream.h>
#include<conio.h>
#define SPACE_SIZE 10
struct Node
{
int data;
int Next;
};
typedef int PtrToNode;
typedef PtrToNode POSITION;
typedef PtrToNode LIST;
struct Node cursor[SPACE_SIZE];
void InitializeCursor()
{
int i;
for(i=0;i<=SPACE_SIZE-1;i++)
{
cursor[i].Next=i+1;
cursor[i].data=0;
}
cursor[SPACE_SIZE-1].Next=-1;
}
POSITION CursorAlloc()
{
POSITION P;
P=cursor[0].Next;
cursor[0].Next=cursor[P].Next;
cursor[P].data=-1;
cursor[P].Next=-1;
return P;
}
void CursorFree(POSITION P)
{
cursor[P].Next=cursor[0].Next;
cursor[0].Next=P;
cursor[P].data=0;
}
void Insert(int X,POSITION P)
{
POSITION Temp;
Temp=CursorAlloc();
if(Temp==-1)
cout<<"\nOut of space";
else if(cursor[P].data==0)
cout<<"\nPosition is not in the list";
else
{
cursor[Temp].data=X;
cursor[Temp].Next=cursor[P].Next;
cursor[P].Next=Temp;
}
}
int IsLast(POSITION P)
{
return cursor[P].Next==-1;
}
int IsEmpty(LIST L)
{
return cursor[L].Next==-1;
}
POSITION Find(int X,LIST L)
{
POSITION Temp;
Temp=cursor[L].Next;
while(Temp!=-1&&cursor[Temp].data!=X)
Temp=cursor[Temp].Next;
return Temp;
}
POSITION FindPrevious(int X,LIST L)
{
POSITION Temp;
Temp=L;
while(Temp!=-1&&cursor[cursor[Temp].Next].data!=X)
Temp=cursor[Temp].Next;
return Temp;
}
void Delete(int X,LIST L)
{
POSITION P,Temp;
P=FindPrevious(X,L);
if(!IsLast(P))
{
Temp=cursor[P].Next;
cursor[P].Next=cursor[Temp].Next;
CursorFree(Temp);
}
}
void MakeEmpty(LIST L)
{
while(!IsEmpty(L))
Delete(cursor[cursor[L].Next].data,L);
}
void Display()
{
int i;
for(i=0;i<=SPACE_SIZE-1;i++)
cout<<"\n"<<i<<"\t"<<cursor[i].data<<"\t"<<cursor[i].Next;
}
==============
// cursorimpl.cpp
==============
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include"cursorHeader.h"
class cursorDemo
{
public:
LIST L;
cursorDemo()
{
L=-1;
}
POSITION P;
int choice,place,x;
void menu();
};
void cursorDemo::menu()
{
while(1)
{
cout<<"\n1.Create\n2.Insert\n3.Delete\n4.MakeEmpty\n5.Display\n6.Find\n7.Exit";
cout<<"\nEnter ur choice:\t";
cin>>choice;
switch(choice)
{
case 1:
if(L==-1)
{
InitializeCursor();
L=CursorAlloc();
cout<<"List created.. ";
}
else
cout<<"\nList is already created";
break;
case 2:
if(L==-1)
cout<<"\nList is not yet initialized";
else
{
cout<<"\nWhere u want to insert?";
cin>>place;
cout<<"\nEnter the element to insert";
cin>>x;
Insert(x,place);
}
break;
case 3:
if(L==-1)
cout<<"\nList is not yet initialized";
else
{
cout<<"\nWhich element you want to delete?";
cin>>x;
Delete(x,L);
}
break;
case 4:
if(L==-1)
cout<<"\nList is not yet initialized";
else
MakeEmpty(L);
break;
case 5:
if(L==-1)
cout<<"\nList is not yet initialized";
else
Display();
break;
case 6:
if(L==-1)
cout<<"\nList is not yet initialized";
else
{
cout<<"\nWhich element you want to search?";
cin>>x;
P=Find(x,L);
cout<<"\nThe element is at "<<P;
}
break;
case 7:
exit(0);
default:
cout<<"Enter a valid choice..";
}
cout<<"\nEnter any key to continue..";
getch();
clrscr();
}
}
void main()
{
clrscr();
cout<<"*** Cursor Implementation of List ADT ***\n";
cursorDemo obj;
obj.menu();
}
Note
1. Save both the files cursorHeader.h and cursorimpl.cpp and execute the cpp file
5.
STACK ADT
a. ARRAY IMPLEMENTATION
#include<iostream.h>
#include<conio.h>
#define size 3
class stack
{
public:
int s[size];
inttop,i,n;
stack()
{
top=-1;
}
intisempty();
intisfull();
void display();
void push(int n);
void pop();
};
int stack::isempty()
{
if(top==-1)
{
return 1;
}
else
{
return 0;
}
}
int stack::isfull()
{
if(top==size-1)
{
return 1;
}
else
{
return 0;
}
}
void stack::push(int n)
{
{
top++;
s[top]=n;
cout<<"\nelement"<<n<<"is inserted";
}
}
void stack::pop()
{
int a;
a=top;
top--;
cout<<"\nThe element"<<s[a]<<"is deleted";
}
void stack::display()
{
if(top==-1)
{
cout<<"\nNO ELEMENTS IN STACK";
}
else
{
for(i=top;i>=0;i--)
{
cout<<"\n"<<s[i];
}
}
}
void main()
{
clrscr();
stack obj;
intch,item,n;
do
{
cout<<"\n1.push\n2.pop\n3.display\n";
cout<<"\nEnter the choice";
cin>>ch;
switch(ch)
{
case 1: if(obj.isfull())
\cout<<"\nSTACK OVERFLOW:INSERTION/CREATION IMPOSSIBLE";
else
{
OUTPUT
Array implementation using stack adt
1.push
2.pop
3.display
4.exit
Enter your choice:1
Enter the element:6
Element 6 is insersted on to the stack
1.push
2.pop
3.display
4.exit
Enter your choice:1
Enter the element:5
Element 5 is inserted on to the stack
1.push
2.pop
3.display
4.exit
Enter your choice:3
6
5
1.push
2.pop
3.display
4.exit
Enter your choice:1
Stack is over flow you cannot insert an element
1.push
2.pop
3.display
4.exit
Enter your choice:2
Element 6 is deleted from the stack
1.push
2.pop
3.display
4.exit
Enter your choice:3
5
1.push
2.pop
3.display
4.exit
enter your choice:4
if(top==NULL)
{
snode->next=NULL;
top=snode;
}
else
{
snode->next=top;
top=snode;
}
}
}
void llstack::pop()
{
cout<<"\nDELETION";
if(top==NULL)
{
cout<<"\nstack underflow";
}
else
{
temp=top;
top=top->next;
delete temp;
}
}
void llstack::display()
{
temp=top;
while(temp->next!=NULL)
{
cout<<"\n"<<temp->data;
temp=temp->next;
}
cout<<"\n"<<temp->data;
}
void llstack::topstack()
{
temp=top;
cout<<"\nThe element is:"<<temp->data;
}
void main()
{
clrscr();
llstack s;
intch;
do
{
cout<<"\n1.push\n2.pop\n3.display\n4.top element of the stack";
cout<<"\nenter the choice";
cin>>ch;
switch(ch)
{
case 1: s.push();
s.display();
break;
case 2: s.pop();
s.display();
break;
case 3: s.display();
break;
case 4: s.topstack();
break;
default: cout<<"\nEnter the correct choice";
break;
}
}while(ch<5);
getch();
}
OUTPUT
Linked list implememntation of stack
Menus
1.push
2.pop
3.display
Enter your choice:1
Enter the no.of elements to be inserted:2
Enter the element:2
Enter the element:12
Menus
1.push
2.pop
3.display
Enter your choice:3
12
2
Menus
1.push
2.pop
3.display
Enter your choice:4
Top element is 12
Menus
1.push
2.pop
3.display
Enter your choice :2
12 is deleted from stack
Menus
1.push
2.pop
3.display
Enter your choice:2
stack underflow cannot delete
Menus
1.push
2.pop
3.display
Enter your choice:4
Top element is 0
4.top element
5.exit
4.top element
5.exit
4.top element
5.exit
4.top element
5.exit
4.top element
5.exit
4.top element
5.exit
Menus
1.push
2.pop
Enter your choice:5
3.display
4.top element
5.exit
6. SOURCE FILES
(i) Program source file for stack application 1 Checking parenthesis
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//Inclusion of necessary header files is must for the execution of this program
#define SIZE 10
void main()
{
char item;
char ans,bracket[SIZE];
stk_class obj;
int i;
clrscr();
cout<<"*** STACK APPLICATION 1 - WELL FORMED PARANTHESIS ***\n\n";
cout<<"\n Enter an expression(add $ at end) : ";
cin>>bracket;
i=0;
if(bracket[i]==')')
cout<<"\n Expression never starts with opening paranthesis..";
else
{
do
{
while(bracket[i]=='(')
{
obj.push(bracket[i]);
i++;
}
while(bracket[i]==')')
{
item=obj.pop();
i++;
}
}
while(bracket[i]!='$');
if(!obj.stempty())
cout<<"\n No balanced paranthesis in the given expression";
else
cout<<"\n Entered expression has well formed parenthesis";
}
getch();
}
}
int stk_class::stfull()
{
if(top==SIZE)
return 1;
else
return 0;
}
char stk_class::pop()
{
char item;
item=s[top];
top--;
return(item);
}
}
void stk_class::push(double val)
{
top++;
s[top]=val;
}
double stk_class::pop()
{
double val;
val=s[top];
top--;
return(val);
}
(iii)
{
New->data=item;
New->next=top;
top=New;
}
}
int stk_class::stempty()
{
if(top==NULL)
return 1;
else
return 0;
}
void stk_class::pop()
{
node *temp;
temp=top;
top=top->next;
delete temp;
}
node *temp;
item=top->data;
temp=top;
top=top->next;
delete temp;
return item;
}
For the program code (i) and (iv), include the header files of (ii) and (iii) for its execution.
case '*':
case '/':return 4;
case '^':return 6;
case '(':
case ')':
case '#':return 1;
}
}
int intopo::isoperator(char symbol)
{
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':return 1;
default:return 0;
}
}
void intopo::push(char item)
{
top++;
stack[top]=item;
}
char intopo::pop()
{
char a;
a=stack[top];
top--;
return a;
}
void intopo::convertip(char infix[],char postfix[])
{
int i,symbol,j=0;
stack[++top]='#';
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
if(isoperator(symbol)==0)
{
postfix[j]=symbol;
j++;
}
else
{
if(symbol=='(')
push(symbol);
else if(symbol==')')
{
while(stack[top]!='(')
{
postfix[j]=pop();
j++;
}
pop();//pop out (.
}
else
{
if(prcd(symbol)>prcd(stack[top]))
push(symbol);
else
{
while(prcd(symbol)<=prcd(stack[top]))
{
postfix[j]=pop();
j++;
}
push(symbol);
}
}
}
}
while(stack[top]!='#')
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';//null terminate string.
}
void main()
OUTPUT compilation
{
1
char infix[20],postfix[20];
clrscr();
*** Infix to postfix
intopo in;
conversion using stack
cout<<*** Infix to postfix conversion using stack array ***\n;
array ***
cout<<"Enter the valid infix string: ";
Enter the valid infix
cin>>infix;
string: 4+5*6-7
in.convertip(infix,postfix);
The corresponding postfix
cout<<"The corresponding postfix string is: ;
string is: 456*+7cout<<postfix;
getch();
OUTPUT compilation
}
2
*** Infix to postfix
conversion using stack
array ***
Enter the valid infix
string:a+b*c-d
The corresponding postfix
string is: abc*+d-
case '*':
case '/':return 4;
case '^':return 6;
case '(':
case ')':
case '#':return 1;
}
}
int intopo::isoperator(char symbol)
{
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':return 1;
default:return 0;
}
}
void intopo::push(char item)
{
nnode=new stack;
nnode->data=item;
nnode->next=NULL;
if(top==NULL)
top=nnode;
else
{
nnode->next=top;
top=nnode;
}
}
char intopo::pop()
{
char a;
a=top->data;
nnode=top;
top=top->next;
delete nnode;
return a;
}
void intopo::convertip(char infix[],char postfix[])
{
int i,symbol,j=0;
nnode=new stack;
nnode->data='#';
nnode->next=top;
top=nnode;
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
if(isoperator(symbol)==0)
{
postfix[j]=symbol;
j++;
}
else
{
if(symbol=='(')
push(symbol);
else if(symbol==')')
{
while(top->data!='(')
{
postfix[j]=pop();
j++;
}
pop();//pop out (.
}
else
{
if(prcd(symbol)>prcd(top->data))
push(symbol);
else
{
while(prcd(symbol)<=prcd(top->data))
{
postfix[j]=pop();
j++;
}
push(symbol);
}
}
}
}
while(top->data!='#')
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';//null terminate string.
}
void main()
{
char infix[20],postfix[20];
clrscr();
intopo in;
cout<<"*** Infix to postfix conversion using linked stack ***\n";
cout<<"Enter the valid infix string: ";
cin>>infix;
in.convertip(infix,postfix);
OUTPUT compilation
1
*** Infix to postfix
conversion using linked
stack ***
Enter the valid infix
string: 4+5*6-7
The corresponding postfix
string is: 456*+7OUTPUT compilation
2
8.
a.
class
{
public:
int top,i,len;
char Stack[MAX];
void getData();
void push(char);
void display();
char pop();
};
void stackDemo :: push(char x)
{
if(top==MAX-1)
{
cout<<"stack overflow\n";
return;
}
Stack[++top]=x;
return;
}
char stackDemo :: pop(void)
{
char y;
y=Stack[top];
Stack[top--]=0;
return y;
}
void stackDemo :: getData()
{
char stk[20];
cout<<"\nEnter the string : ";
cin>>stk;
len=strlen(stk);
for(i=0;i<len;i++)
push(stk[i]);
}
void stackDemo :: display()
{
cout<<"\nThe reversed string is :";
for(i=0;i<len;i++)
cout<<pop();
}
int main()
{
char Stack[MAX];
clrscr();
stackDemo obj;
cout<<"*** STACK APPLICATION - STRING REVERSE *** \n";
obj.getData();
obj.display();
getch();
return 0;
}
OUTPUT
*** STACK
APPLICATION STRING
REVERSE ***
Enter the string :
stack application
The reversed
string is :
noitacilppa kcats
{
snode->next=NULL;
top=snode;
}
else
{
snode->next=top;
top=snode;
}
}
char llstack::pop()
{
char ch;
if(top!=NULL)
{
temp=top;
top=top->next;
ch=temp->data;
return (ch);
}
}
void llstack::display()
{
temp=top;
cout<<"\nThe reversed string is : ";
while(temp!=NULL)
{
cout<<temp->data;
temp=temp->next;
}
}
void llstack :: getData()
{
cout<<"\nEnter the string : ";
cin>>stk;
int len=strlen(stk);
for(int i=0;i<len;i++)
push(stk[i]);
}
void main()
{
clrscr();
llstack s;
cout<<"*** Linked stack implementation - String Reverse ***\n";
s.getData();
s.display();
getch();
}
OUTPUT
*** Linked stack
implementation
String Reverse ***
Enter the string :
stack application
The reversed
string is :
noitacilppa kcats
};
void infix :: push(char elem)
{
snode=new node;
snode->data=elem;
snode->next=NULL;
if(top==NULL)
top=snode;
else
{
snode->next=top;
top=snode;
}
}
char infix :: pop()
{
char c;
c=top->data;
top=top->next;
return c;
}
int infix::pr(char elem)
{
switch(elem)
{
case '#': return 0;
case ')': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}
void infix::getData()
{
cout<<"\n\nRead the Infix Expression : ";
cin>>infx;
push('#');
strrev(infx);
while( (ch=infx[i++]) != '\0')
{
if( ch == ')') push(ch);
else if(isalnum(ch)) prfx[k++]=ch;
else if( ch == '(')
{
while( top->data != ')')
prfx[k++]=pop();
elem=pop(); /* Remove ) */
}
else
{
void main()
{
clrscr();
cout<<"*** Stack Application - Infix to prefix conversion using linked stack***\n";
infix obj;
obj.getData();
getch();
}
OUTPUT
*** Stack
Application - Infix
to prefix
conversion using
linked stack***
Enter the Infix
Expression : a+b
Given Infix
Expression: a+b Prefix
Expression: +ab
{
cout<<"Enter the variable";
cin>>x;
rear++;
q[rear]=x;
}
}
void arrqueue::dequeue()
{
if(front==SIZE)
{
cout<<"\nUNDERFLOW";
}
else
{
q[a]=q[front];
front++;
cout<<"\nThe element"<<q[a]<<"is deleted";
}
}
void arrqueue::display()
{
for(i=front;i<=rear;i++)
{
cout<<"\n"<<q[i];
}
}
void arrqueue::fiqueue()
{
cout<<"\n"<<q[front]<<"is the first element in the queue";
}
void main()
{
clrscr();
intch;
arrqueue a;
do
{
cout<<"\n1.enqueue\n2.dequeue\n3.display\n4.first element in queue";
cout<<"\nEnter the choice";
cin>>ch;
switch(ch)
{
case 1:a.creation();
break;
case 2:a.dequeue();
break;
case 3:a.display();
break;
case 4:a.fiqueue();
break;
default:cout<<"\nEnter the correct choice";
break;
}
}while(ch<5);
getch();
}
cin>>qnode->data;
qnode->next=NULL;
{
if(front==NULL)
{
front=qnode;
rear=qnode;
}
else
{
rear->next=qnode;
rear=qnode;
}
}
}
}
void queuel::dequeue()
{
temp=front;
front=front->next;
cout<<"\nThe element"<<"\t"<<temp->data<<"\t"<<"is deleted";
delete temp;
}
void queuel::display()
{
temp=front;
while(temp!=NULL)
{
cout<<"\n"<<temp->data;
temp=temp->next;
}
}
void queuel::fiqueue()
{
temp=front;
cout<<"\nThe first element of the queue is"<<"\t"<<temp->data;
}
void main()
{
clrscr();
intch;
queuel s;
do
{
cout<<"\n1.Enqueue\n2.Dequeue\n3.Display\n4.First element";
cout<<"\nEnter the choice";
cin>>ch;
switch(ch)
{
case 1:
s.enqueue();
break;
case 2:
case 3:
case 4:
default:
s.dequeue();
break;
s.display();
break;
s.fiqueue();
break;
cout<<"\nwrong number";
break;
}
}while(ch<6);
getch();
}
root = NULL;
}
};
int main()
{
int choice, num;
clrscr();
BST bst;
node *temp;
while (1)
{
cout<<"1.Insert Element "<<endl;
cout<<"2.Delete Element "<<endl;
cout<<"3.Inorder Traversal"<<endl;
cout<<"4.Preorder Traversal"<<endl;
cout<<"5.Postorder Traversal"<<endl;
cout<<"6.Display"<<endl;
cout<<"7.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
temp = new node;
cout<<"Enter the number to be inserted : ";
cin>>temp->info;
bst.insert(root, temp);
break;
case 2:
if (root == NULL)
{
cout<<"Tree is empty, nothing to delete"<<endl;
continue;
}
cout<<"Enter the number to be deleted : ";
cin>>num;
bst.del(num);
break;
case 3:
cout<<"Inorder Traversal of BST:"<<endl;
bst.inorder(root);
cout<<endl;
break;
case 4:
if (item == ptr->info)
{
*loc = ptr;
*par = ptrsave;
return;
}
ptrsave = ptr;
if (item < ptr->info)
ptr = ptr->left;
else
ptr = ptr->right;
}
*loc = NULL;
*par = ptrsave;
}
void BST::insert(node *tree, node *newnode)
{
if (root == NULL)
{
root = new node;
root->info = newnode->info;
root->left = NULL;
root->right = NULL;
cout<<"Root Node is Added"<<endl;
return;
}
if (tree->info == newnode->info)
{
cout<<"Element already in the tree"<<endl;
return;
}
if (tree->info > newnode->info)
{
if (tree->left != NULL)
{
insert(tree->left, newnode);
}
else
{
tree->left = newnode;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
cout<<"Node Added To Left"<<endl;
return;
}
}
else
{
if (tree->right != NULL)
{
insert(tree->right, newnode);
}
else
{
tree->right = newnode;
(tree->right)->left = NULL;
(tree->right)->right = NULL;
cout<<"Node Added To Right"<<endl;
return;
}
}
}
void BST::del(int item)
{
node *parent, *location;
if (root == NULL)
{
cout<<"Tree empty"<<endl;
return;
}
find(item, &parent, &location);
if (location == NULL)
{
cout<<"Item not present in tree"<<endl;
return;
}
if (location->left == NULL && location->right == NULL)
case_a(parent, location);
if (location->left != NULL && location->right == NULL)
case_b(parent, location);
if (location->left == NULL && location->right != NULL)
case_b(parent, location);
if (location->left != NULL && location->right != NULL)
case_c(parent, location);
free(location);
}
void BST::case_a(node *par, node *loc )
{
if (par == NULL)
{
root = NULL;
}
else
{
if (loc == par->left)
par->left = NULL;
else
par->right = NULL;
}
}
void BST::case_b(node *par, node *loc)
{
node *child;
if (loc->left != NULL)
child = loc->left;
else
child = loc->right;
if (par == NULL)
{
root = child;
}
else
{
if (loc == par->left)
par->left = child;
else
par->right = child;
}
}
void BST::case_c(node *par, node *loc)
{
node *ptr, *ptrsave, *suc, *parsuc;
ptrsave = loc;
ptr = loc->right;
while (ptr->left != NULL)
{
ptrsave = ptr;
ptr = ptr->left;
}
suc = ptr;
parsuc = ptrsave;
if (suc->left == NULL && suc->right == NULL)
case_a(parsuc, suc);
else
case_b(parsuc, suc);
if (par == NULL)
root = suc;
else
{
if (loc == par->left)
par->left = suc;
else
par->right = suc;
}
suc->left = loc->left;
suc->right = loc->right;
}
void BST::preorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
cout<<ptr->info<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
void BST::inorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
inorder(ptr->left);
cout<<ptr->info<<" ";
inorder(ptr->right);
}
}
void BST::postorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->info<<" ";
}
}
void BST::display(node *ptr, int level)
{
int i;
if (ptr != NULL)
{
display(ptr->right, level+1);
cout<<endl;
if (ptr == root)
cout<<"Root->: ";
else
{
for (i = 0;i < level;i++)
cout<<"
";
}
cout<<ptr->info;
display(ptr->left, level+1);
}
}
p=p+1;
}
}
return p;
}
voidqsort::Sort(int a[], int beg, int end)
{
if(beg<end)
{
int p=Partition(a,beg,end);
Sort(a,beg,p-1);
Sort(a,p+1,end);
}
}
void main()
{
clrscr();
qsort q;
int a[100],i,n,beg,end;
cout<<"\n*** QUICK SORT ***\n";
cout<<"Enter the number of Elements : ";
cin>>n;
cout<<"\nEnter the elements : \n";
for(i=1;i<=n;i++)
cin>>a[i];
beg=1;
end=n;
q.Sort(a,beg,end);
cout<<"\nThe sorted elements are : \n";
for(i=1;i<=n;i++)
cout<<a[i]<<"\t";
getch();
}