Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 32

Practical 1:

#include<iostream>

#include<stack>

using namespace std;

// funtion to check if character is operator or not

bool isOperator(char x) {

switch (x) {

case '+':

case '-':

case '/':

case '*':

return true;

return false;

// Convert prefix to Postfix expression

string preToPost(string pre_exp) {

stack<string> s;

// length of expression

int length = pre_exp.size();

// reading from right to left

for (int i = length - 1; i >= 0; i--) {


// check if symbol is operator

if (isOperator(pre_exp[i])) {

// pop two operands from stack

string op1 = s.top(); s.pop();

string op2 = s.top(); s.pop();

// concat the operands and operator

string temp = op1 + op2 + pre_exp[i];

// Push string temp back to stack

s.push(temp);

// if symbol is an operand

else {

// push the operand to the stack

s.push(string(1, pre_exp[i]));

// stack contains only the Postfix expression

return s.top();

// Driver Code

int main() {

char pre_exp[20];
cout<<"Enter the expresstion :";

cin>>pre_exp;

//string pre_exp = "*-A/BC-/AKL";

cout << "Postfix : " << preToPost(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>

using namespace std;


int stack[20];
int top=-1;

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>

using namespace std;

void towerOfHanoi(int n, char from_rod,

char to_rod, char aux_rod)

if (n == 1)

cout << "Move disk 1 from rod " << from_rod <<

" to rod " << to_rod<<endl;

return;

towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);

cout << "Move disk " << n << " from rod " << from_rod <<

" to rod " << to_rod << endl;

towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);

// Driver code

int main()

int n = 4; // Number of disks

towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods

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>

using namespace std;

// Driver code

int main()

int sum,n;

cout<<"Entyer the no:";

cin>>n;

for(int i=0;i<n;i++)

sum=sum+i;

cout<<"Sum of "<<n <<" is :"<<sum;


return 0;

OUTPUT:
Entyer the no:5                                                                                                                
Sum of 5 is :10 

Entyer the no:25                                                                                                               
Sum of 25 is :300 

Practical 5:
#include <iostream>

using namespace std;

class NODE

public:

int data;

NODE *link;

};

void display(NODE *start)

NODE *temp;

if(start==NULL)

cout<<"\n---------LIST IS EMPTY----------\n";
return;

temp = start;

while(temp !=NULL)

cout<< temp->data << " ";

temp = temp->link;

NODE* getnode()

NODE *p;

p = new NODE;

p->link = NULL;

return p;

NODE* insertend(NODE *start)

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;

cout<<"\nNode at the End inserted successfully\n";

return start;

NODE* insertbefore (NODE *start,int key)

NODE *p,*cur, *prev;

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)

cout<<"Element"<<key<<"Not found in the list"<<endl;


return start;

prev->link=p;

p->link = cur;

cout<<"\n Node Inserted Before"<<key<<endl;

return start;

NODE* insertfront (NODE *start)

NODE *p;

p=getnode();

cout<<"Enter data:";

cin>>p->data;

p->link = start;

start=p;

cout<<"\n Node at the beginning Inserted successfully\n";

return start;

int main()

NODE *start= NULL;

int choice, key, pos;

while(1)

{
cout<<"\n****************LINKED LIST*****************"<<endl;

cout<<"1.Insert at Frot"<<endl;

cout<<"2.Insert at End"<<endl;

cout<<"3.Insert Before a given Element"<<endl;

// cout<<"4.Insert at the Position"<<endl;

cout<<"4.Display"<<endl;

cout<<"5.Exit"<<endl;

cout<<"\n\n Enter Your Choice";

cin>>choice;

switch(choice)

case 1: start = insertfront(start);

break;

case 2: start = insertend(start);

break;

case 3:

cout<<"Enter the key element:"<<endl;

cin>>key;

start = insertbefore(start,key);

break;

/*case 4:

cout<<"Enter the Position:"<<endl;

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>

using namespace std;

#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;

void Dequeue::insertFront(int itm)

if(rear==max-1)

cout<<"\ninsertion is not possible overflow!!1";

return;

if(front==-1)

front++;

else

for(int i=rear; i>=front;i--)

DQ[i+1]=DQ[i];

DQ[front]=itm;

rear++;

cout<<"\n*********************insertion is successfull**************";
}

void Dequeue::insertRear(int itm)

if(rear==max-1)

cout<<"\ninsertion is not possible overflow!!!";

return;

if(front==-1)

front++;

DQ[++rear]=itm;

cout<<endl<<"*********************insertion is successfull**************";

void Dequeue::display()

cout<<endl<<"*********************Queue Element are**************\n";

for(int temp=front;temp<=rear;temp++)

cout<<DQ[temp]<<" ";

void Dequeue::deleteFront()

if(front==-1)

cout<<"\nDeletion is not possible underflow!!!";

return;
}

int ele=DQ[front];

if(front==rear)

cout<<"\n\n.......Queue Empty.....!\n\n";

front=rear=-1;

else

front=front+1;

cout<<endl<<"Deleted Element = "<<ele<<endl;

cout<<endl<<"***********************************\n";

void Dequeue::deleteRear()

if(front==-1)

cout<<"\nDeletion is not possible underflow!!!";

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";

cout<<"\n1.insert at begining\n2.insert at end\n3.display\n4.Deletion from front\n5.deletion from


end\n5.exit";

cout<<"\nEnter your choice<1-6>:";

cin>>choic;

switch(choic)

case 1:

cout<<"Enter the element to be inserted:";

cin>>itm;

mydq.insertFront(itm);

break;

case 2:

cout<<"Enter the element to be inserted:";

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>

using namespace std;

class NODE

public:

NODE *left;

int data;

NODE *right;

};

NODE* getNode()

NODE *p=new NODE;

p->left=NULL;

p->right=NULL;

return p;

NODE* push(int itm,NODE *top)

NODE *p;

p=getNode();

if(p==NULL)

cout<<"\nOverflow...no memory in heap...";

return top;
}

p->data=itm;

p->left=NULL;

p->right=top;

if(top!=NULL)

top->left=p;

top=p;

return top;

NODE* pop(NODE* 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;

void Displaystack(NODE *top)


{

NODE *temp;

if(top==NULL)

cout<<"\nStack is empty\n";

return ;

temp=top;

cout<<"\n The contents of list:\n";

while(temp != NULL)

cout<<temp->data<<" ";

temp=temp->right;

int main()

int opt,itm;

NODE *top=NULL;

cout<<"\n Stack Using Doble LInked list******\n";

while(1)

cout<<"\nMenu....\n1.push\n2.pop\n3.display\n4.exit\n";

cout<<"Enter your option:";

cin>>opt;

switch(opt)

case 1:

cout<<"\nEnter item to be inserted :";


cin>>itm;

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>

using namespace std;

class NODE

public:

NODE *left;

int data;

NODE *right;

};
NODE *front=NULL,*tail=NULL;

NODE* getNode()

NODE *p=new NODE;

p->left=NULL;

p->right=NULL;

return p;

void enQueue()

int elem;

cout<<"Enter element to be inserted :";

cin>>elem;

NODE *p=getNode();

p->data=elem;

if(front==NULL)

front=p;

else

tail->right=p;

p->left=tail;

tail=p;

cout<<"Element has been inserted in the queue \n";

void deQueue()

if(front==NULL)

{
cout<<"Queue is empty !!\n";

return;

NODE* temp=front;

front=front->right;

if(front!=NULL)

front->left=NULL;

cout<<"Element removed= "<<temp->data<<endl;

delete temp;

void displayQueue()

NODE *p=front;

if(p==NULL)

cout<<"Queue is empty !!\n";

return ;

cout<<"Queue element are :\n";

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";

cout<<"\nEnter your choice<1-4>:";

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 binarysearch(int *a,int low,int high,int key)

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 linearsearch(int *a,int size,int 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;

cout<<"enter your choice";

cin>>choice;

cout<<"\n enter the size of array";

cin>>size;

cout<<"enter the element\n";

for(i=0;i<size;i++)

cin>>a[i];

cout<<"enter the search element";

cin>>key;

if(choice == 1)

index=linearsearch(a,size,key);

else

index=binarysearch(a,0,size-1,key);

if(index==-1)

cout<<"the key element not found in array"<<endl;

else

cout<<"the key is found at position:"<<index+1<<endl;


return 0;

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 

You might also like