C++ Practicals
C++ Practicals
#include<iostream>
#include<stack>
bool isOperator(char x) {
switch (x) {
case '+':
case '-':
case '/':
case '*':
return true;
return false;
stack<string> s;
// length of expression
if (isOperator(pre_exp[i])) {
s.push(temp);
// if symbol is an operand
else {
s.push(string(1, pre_exp[i]));
return s.top();
// Driver Code
int main() {
char pre_exp[20];
cout<<"Enter the expresstion :";
cin>>pre_exp;
return 0;
OUTPUT:
Enter the expresstion :+ABC
Postfix : ABC-+
Enter the expresstion :*A-/BC-/AKL
Postfix : ABC/AK/L--*
Practical 2:
#include <iostream>
void push(int x)
{
stack[++top]=x;
}
int pop()
{
return stack[top--];
}
int main()
{
char expe[20];
char *e;
int n1,n2,n3,num;
cout<<"ENter the expression:";
cin>>expe;
e=expe;
while(*e !='\0')
{
if(isdigit(*e))
{
num=*e-48;
push(num);
}
else
{
n1=pop();
n2=pop();
switch(*e)
{
case '+':
n3=n1+n2;
break;
case '-':
n3=n2-n1;
break;
case '*':
n3=n2*n1;
break;
case '/':
n3=n2/n1;
break;
}
push(n3);
}
e++;
}
cout<<"\n The result of the expression"<<expe<<"="<<pop();
return 0;
}
OUTPUT:
ENter the expression:12+3+1-
The result of the expression 12+3+1-=5
ENter the expression:42*2/3+
The result of the expression 42*2/3+=7
Practical 4:
B]
#include <iostream>
if (n == 1)
cout << "Move disk 1 from rod " << from_rod <<
return;
cout << "Move disk " << n << " from rod " << from_rod <<
// Driver code
int main()
return 0;
OUTPUT:
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 3 from rod A to rod B
Move disk 1 from rod C to rod A
Move disk 2 from rod C to rod B
Move disk 1 from rod A to rod B
Move disk 4 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 2 from rod B to rod A
Move disk 1 from rod C to rod A
Move disk 3 from rod B to rod C
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 3 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 2 from rod B to rod C
Move disk 1 from rod A to rod C
C]
#include <iostream>
// Driver code
int main()
int sum,n;
cin>>n;
for(int i=0;i<n;i++)
sum=sum+i;
OUTPUT:
Entyer the no:5
Sum of 5 is :10
Entyer the no:25
Sum of 25 is :300
Practical 5:
#include <iostream>
class NODE
public:
int data;
NODE *link;
};
NODE *temp;
if(start==NULL)
cout<<"\n---------LIST IS EMPTY----------\n";
return;
temp = start;
while(temp !=NULL)
temp = temp->link;
NODE* getnode()
NODE *p;
p = new NODE;
p->link = NULL;
return p;
NODE *p,*cur;
p=getnode();
cout<<"ENTER DATA:";
cin>>p->data;
if(start==NULL)
return p;
cur=start;
while(cur->link !=NULL)
cur=cur->link;
cur->link=p;
return start;
p=getnode();
cout<<"Enter data";
cin>>p->data;
if(start==NULL)
return p;
prev=cur=start;
while(cur->data !=key)
prev=cur;
cur=cur->link;
if(cur== NULL)
prev->link=p;
p->link = cur;
return start;
NODE *p;
p=getnode();
cout<<"Enter data:";
cin>>p->data;
p->link = start;
start=p;
return start;
int main()
while(1)
{
cout<<"\n****************LINKED LIST*****************"<<endl;
cout<<"1.Insert at Frot"<<endl;
cout<<"2.Insert at End"<<endl;
cout<<"4.Display"<<endl;
cout<<"5.Exit"<<endl;
cin>>choice;
switch(choice)
break;
break;
case 3:
cin>>key;
start = insertbefore(start,key);
break;
/*case 4:
cin>>pos;
start = insertatpos(start,pos);
break;*/
case 4: display(start);
break;
default : return 0;
return 0;
OUTPUT:
****************LINKED LIST*****************
1.Insert at Frot
2.Insert at End
3.Insert Before a given Element
4.Display
5.Exit
Enter Your Choice1
Enter data:2
Node at the beginning Inserted successfully
****************LINKED LIST*****************
1.Insert at Frot
2.Insert at End
3.Insert Before a given Element
4.Display
5.Exit
Enter Your Choice2
ENTER DATA:0
Node at the End inserted successfully
****************LINKED LIST*****************
1.Insert at Frot
2.Insert at End
3.Insert Before a given Element
4.Display
5.Exit
Enter Your Choice3
Enter the key element:
0
Enter data5
Node Inserted Before0
****************LINKED LIST*****************
1.Insert at Frot
2.Insert at End
3.Insert Before a given Element
4.Display
5.Exit
Enter Your Choice4
2 5 0
****************LINKED LIST*****************
1.Insert at Frot
2.Insert at End
3.Insert Before a given Element
4.Display
5.Exit
Enter Your Choice4
2 5 0
****************LINKED LIST*****************
1.Insert at Frot
2.Insert at End
3.Insert Before a given Element
4.Display
5.Exit
Enter Your Choice5
...Program finished with exit code 0
Press ENTER to exit console.
Practical 7:
#include<iostream>
#define max 5
class Dequeue
private:
int DQ[max],front,rear;
public:
Dequeue();
void insertFront(int);
void insertRear(int);
void deleteFront();
void deleteRear();
void display();
};
Dequeue::Dequeue()
front=rear=-1;
if(rear==max-1)
return;
if(front==-1)
front++;
else
DQ[i+1]=DQ[i];
DQ[front]=itm;
rear++;
cout<<"\n*********************insertion is successfull**************";
}
if(rear==max-1)
return;
if(front==-1)
front++;
DQ[++rear]=itm;
cout<<endl<<"*********************insertion is successfull**************";
void Dequeue::display()
for(int temp=front;temp<=rear;temp++)
cout<<DQ[temp]<<" ";
void Dequeue::deleteFront()
if(front==-1)
return;
}
int ele=DQ[front];
if(front==rear)
cout<<"\n\n.......Queue Empty.....!\n\n";
front=rear=-1;
else
front=front+1;
cout<<endl<<"***********************************\n";
void Dequeue::deleteRear()
if(front==-1)
return;
int ele=DQ[rear];
if(front==rear)
cout<<"\n\n.......Queue Empty.....!\n\n";
front=rear=-1;
else
rear=rear-1;
cout<<endl<<"Deleted Element = "<<ele<<endl;
cout<<endl<<"***********************************\n";
int main()
int choic,itm;
Dequeue mydq;
while(1)
cout<<"\n\n*****Dequeue Operation*******\n";
cin>>choic;
switch(choic)
case 1:
cin>>itm;
mydq.insertFront(itm);
break;
case 2:
cin>>itm;
mydq.insertRear(itm);
break;
case 3:
cout<<"Enter the element to be inserted:";
mydq.display();
break;
case 4:
mydq.deleteFront();
break;
case 5:
mydq.deleteRear();
break;
case 6:
exit(1);
break;
return 0;
OUTPUT:
*****Dequeue Operation*******
1.insert at begining
2.insert at end
3.display
4.Deletion from front
5.deletion from end
5.exit
Enter your choice<1-6>:1
Enter the element to be inserted:3
*********************insertion is successfull**************
*****Dequeue Operation*******
1.insert at begining
2.insert at end
3.display
4.Deletion from front
5.deletion from end
5.exit
Enter your choice<1-6>:2
Enter the element to be inserted:6
*********************insertion is successfull**************
*****Dequeue Operation*******
*****Dequeue Operation*******
1.insert at begining
2.insert at end
3.display
4.Deletion from front
5.deletion from end
5.exit
Enter your choice<1-6>:2
Enter the element to be inserted:9
*********************insertion is successfull**************
*****Dequeue Operation*******
1.insert at begining
2.insert at end
3.display
4.Deletion from front
5.deletion from end
5.exit
Enter your choice<1-6>:3
Enter the element to be inserted:
*********************Queue Element are**************
3 6 9
*****Dequeue Operation*******
1.insert at begining
2.insert at end
3.display
4.Deletion from front
5.deletion from end
5.exit
Enter your choice<1-6>:4
Deleted Element = 3
***********************************
*****Dequeue Operation*******
1.insert at begining
2.insert at end
3.display
4.Deletion from front
5.deletion from end
5.exit
Enter your choice<1-6>:5
Deleted Element = 9
***********************************
*****Dequeue Operation*******
1.insert at begining
2.insert at end
3.display
4.Deletion from front
5.deletion from end
5.exit
Enter your choice<1-6>:3
Enter the element to be inserted:
*********************Queue Element are**************
6
*****Dequeue Operation*******
1.insert at begining
2.insert at end
3.display
4.Deletion from front
5.deletion from end
5.exit
Enter your choice<1-6>:5
Practical 8:
A]
#include<iostream>
class NODE
public:
NODE *left;
int data;
NODE *right;
};
NODE* getNode()
p->left=NULL;
p->right=NULL;
return p;
NODE *p;
p=getNode();
if(p==NULL)
return top;
}
p->data=itm;
p->left=NULL;
p->right=top;
if(top!=NULL)
top->left=p;
top=p;
return top;
NODE *cur;
if(top==NULL)
cout<<"\nStack undeflow\n";
return top;
cur=top;
top=top->right;
if(top!=NULL)
top->left=NULL;
cout<<"\nDeleted Item="<<cur->data<<endl;
delete cur;
return top;
NODE *temp;
if(top==NULL)
cout<<"\nStack is empty\n";
return ;
temp=top;
while(temp != NULL)
cout<<temp->data<<" ";
temp=temp->right;
int main()
int opt,itm;
NODE *top=NULL;
while(1)
cout<<"\nMenu....\n1.push\n2.pop\n3.display\n4.exit\n";
cin>>opt;
switch(opt)
case 1:
top=push(itm,top);
break;
case 2:
top=pop(top);
break;
case 3:
Displaystack(top);
break;
default:exit(0);
return 0;
OUTPUT:
Stack Using Doble LInked list******
Menu....
1.push
2.pop
3.display
4.exit
Enter your option:1
Enter item to be inserted :1
Menu....
1.push
2.pop
3.display
4.exit
Enter your option:1
Enter item to be inserted :2
Menu....
1.push
2.pop
3.display
4.exit
Enter your option:3
The contents of list:
2 1
Menu....
1.push
2.pop
3.display
4.exit
Enter your option:2
Deleted Item=2
Menu....
1.push
2.pop
3.display
4.exit
Enter your option:3
The contents of list:
1
Menu....
1.push
2.pop
3.display
4.exit
Enter your option:4
B]
#include<iostream>
class NODE
public:
NODE *left;
int data;
NODE *right;
};
NODE *front=NULL,*tail=NULL;
NODE* getNode()
p->left=NULL;
p->right=NULL;
return p;
void enQueue()
int elem;
cin>>elem;
NODE *p=getNode();
p->data=elem;
if(front==NULL)
front=p;
else
tail->right=p;
p->left=tail;
tail=p;
void deQueue()
if(front==NULL)
{
cout<<"Queue is empty !!\n";
return;
NODE* temp=front;
front=front->right;
if(front!=NULL)
front->left=NULL;
delete temp;
void displayQueue()
NODE *p=front;
if(p==NULL)
return ;
while(p!=NULL)
cout<<p->data<<" ";
p=p->right;
cout<<"**************END*********"<<endl;
int main()
int choic;
while(1)
cout<<"\n\n****QueueFIFO******\n";
cout<<"\n1.EnQueue\n2.DeQueue\n3.display\n4.exit";
cin>>choic;
switch(choic)
case 1:
enQueue();
break;
case 2:
deQueue();
break;
case 3:
displayQueue();
break;
deafult:
exit(0);
return 0;
OUTPUT:
****QueueFIFO******
1.EnQueue
2.DeQueue
3.display
4.exit
Enter your choice<1-4>:1
Enter element to be inserted :2
Element has been inserted in the queue
****QueueFIFO******
1.EnQueue
2.DeQueue
3.display
4.exit
Enter your choice<1-4>:1
Enter element to be inserted :3
Element has been inserted in the queue
****QueueFIFO******
1.EnQueue
2.DeQueue
3.display
4.exit
Enter your choice<1-4>:3
Queue element are :
2 3 **************END*********
****QueueFIFO******
1.EnQueue
2.DeQueue
3.display
4.exit
Enter your choice<1-4>:2
Element removed= 2
****QueueFIFO******
1.EnQueue
2.DeQueue
3.display
4.exit
Enter your choice<1-4>:3
Queue element are :
3 **************END*********
Practical 12:
using namespace std;
#include<iostream>
int mid;
if(low>high)
return -1;
mid=(low+high)/2;
if(key<a[mid])
binarysearch(a,low,mid-1,key);
else
binarysearch(a,mid+1,high,key);
int i;
for(i=0;i<size;i++)
if(key==a[i])
return i;
}
return -1;
int main()
int size,a[20],choice,key,i,index;
cout<<endl<<"......menu......."<<endl;
cout<<"1.linear search"<<endl;
cout<<"2.binary search"<<endl;
cin>>choice;
cin>>size;
for(i=0;i<size;i++)
cin>>a[i];
cin>>key;
if(choice == 1)
index=linearsearch(a,size,key);
else
index=binarysearch(a,0,size-1,key);
if(index==-1)
else
OUTPUT:
......menu.......
1.linear search
2.binary search
enter your choice1
enter the size of array5
enter the element
1
2
3
4
5
enter the search element4
the key is found at position:4
......menu.......
1.linear search
2.binary search
enter your choice2
enter the size of array5
enter the element
4
5
6
7
8
enter the search element6
the key is found at position:3