Tuesday, July 10, 2018

Program 22


Program – Circular Queue Using Array

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

int q[5];
const int max=5;
int front=-1,rear=-1;
int item;
void insert();
void dele();
void displayq();
void main()
{
 clrscr();
 int choice;
 do
 {
  cout<<"\nCircular Queue Menu\n";
  cout<<"1. Insert\n";
  cout<<"2. Delete\n";
  cout<<"3. Display\n";
  cout<<"4. Exit\n";
  cout<<" Enter your choice: ";
  cin>>choice;
  switch(choice)
  {
   case 1: insert();
             break;
   case 2: dele();
             break;
   case 3: displayq();
            break;
   case 4: exit(0);  
   default: cout<<"\nPlease enter a valid choice! ";
 }
}while(choice!=4);
getch();
}
void insert()
{
 if((front==0)&&(rear==max-1)||(front==rear+1))
 {
  cout<<"\nOverflow!";
  getch();
  return;
 }
 cout<<"\nEnter the item to be inserted: ";
 cin>>item;
 if(front==-1)
 { front=0;
    rear=0;
 }
 else if(rear==max-1)
 rear=0;
 else
 rear=rear+1;
 q[rear]=item;
}
void dele()
{
 if(front==-1)
 {
  cout<<"\nUnderflow! Queue empty";
  getch();
  return;
 }
 cout<<"\nThe deleted value is "<<q[front]<<"\n";
 if(front==rear)
 {
   front=-1;
   rear=-1;
 }
 else if(front==max-1)
 front=0;
 else
 front=front+1;
}
void displayq()
{
 if(front==-1)
 {
  cout<<"\nEmpty Queue!";
  getch();
  return;
 }
 cout<<"\nThe queue is:\n";
 if(front<=rear)
 {
  for(int i=front;i<=rear;i++)
  cout<<q[i]<<"\t";
}
else
{
 for(int i=front;i<max;i++)
 cout<<q[i]<<"\t";
 for(i=0;i<=rear;i++)
 cout<<q[i]<<"\t";
 }
}

Program 21


         
LINKED LIST IMPLEMENTATION OF QUEUE

Program to perform insertion and deletion operations in a linear Queue that stores students’ information, using linked list implementation.

/* Program to create a Queue which stores student's information
   using Linked implementation and to perform insertion, deletion and
   display operations*/

#include<iostream.h>
#include<process.h>
#include<stdio.h>
#include<conio.h>
struct Node
 {
          int rollno ;
          char name [ 20 ] ;
          Node * next ;
 } ;
class LQueue

 {
     Node *front , * rear ;

     public :
          Queue ( )
           {
                   front = rear = NULL ;      //denotes Q is empty
           }
          void add_to_Q ( )
           {
                   Node *latestnode = new Node ;
                   if ( latestnode == NULL )
                             cout<<"No memory!! Cannot add!! " ;
                   else
                     {
                             cout << "\nEnter the rollno & name :: " ;
                             cin  >> latestnode -> rollno ;
                             gets ( latestnode -> name ) ;
                             latestnode -> next = NULL ;
                             if ( front == NULL )
                              front = rear = latestnode ;
                             else
                              {
                               rear -> next = latestnode ;
                               rear = latestnode ;
                              }
                             cout << "\nAdded to the Queue!! " ;
                     }
           }

          void del_from_Q ( )
           {
                   if ( front == NULL )
                    {
                             cout<< " Queue is empty !! Cannot delete !! " ;
                    }
                   else
                    {
                      Node  *temp  = front ;
                      cout<< "Element deleted :: " << front -> rollno << " " << front -> name ;

                      if ( front == rear )
                             front = rear = NULL ;
                      else
                             front = front -> next ;

                      delete temp ;

                      if(front == NULL )
                             rear = NULL;
                    }
           }
          void display_Q ( )
           {
                   if ( front == NULL )
                             cout << "Queue is empty !! ";
                   else
                    {
                             cout<< "\nQueue is ::\t";
                             Node * temp = front ;
                             while ( temp )
                              {
                                      cout << temp -> rollno << " " << temp -> name<< "\n" ;
                                      temp = temp ->next ;
                              }
                    }
           }
          void cleanup ( )
           {
                   Node * temp = front ;
                   while ( temp )
                    {
                             front= front -> next ;
                             delete temp ;
                             temp = front;
                    }
                   cout << " Queue is cleaned up !! ";
            }

 } Q ;

void main ( )
 {
          int ch ;
          clrscr( ) ;
          while ( 1 )
           {
                   cout << "\n1. Add to Queue ";
                   cout << "\n2. Delete from Queue" ;
                   cout << "\n3. Display Queue" ;
                   cout << "\n4. Exit" ;
                   cout << "\nEnter your choice :: " ;
                   cin  >> ch ;

                   if ( ch == 1 )
                             Q.add_to_Q ( ) ;
                   if ( ch == 2 )
                             Q.del_from_Q ( ) ;
                   if ( ch == 3 )
                             Q. display_Q ( ) ;
                   if ( ch == 4 )
                      {
                             Q . cleanup ( ) ;
                             getch ( ) ;
                             exit ( 0 ) ;
                      }
                   getch ( ) ;
           }
 }

Program 20


         
LINKED LIST IMPLEMENTATION OF STACK


Write a program to implement a LINKED STACK which stores Book’s information.

//Program to implement a Linked Stack
# include < iostream . h >
# include < process . h >
# include < stdio . h >
# include < conio . h >
struct Book
 {
   int bookid ;
   char bookname [ 30 ] ;
   Book *next ;
 } ;

class Stack
 {
           Book *top ;
    public  :
           Stack ( )
            {
                   top = NULL ;         //denotes stack is empty
            }

            void Push( )
            {

            Book *newnode = new Book  ;
            if ( newnode == NULL )
             {
              cout << "Not enough memory " ;
              getch ( ) ;
              return ;
             }
            cout << "\nEnter the Book id and Book name ::" ;
            cin >> newnode  -> bookid ;
            gets ( newnode  ->bookname )  ;
            newnode  -> next = NULL ;

            if ( top ==NULL )

               top = newnode  ;

            else
             {
              newnode  ->  next = top ;
              top = newnode ;
             }
            cout << "\n Book Added" ;
           }
           void Pop( )
            {
                if(top == NULL )
                     {
                             cout << "\nStack is empty !" ;
                             getch( );
                             return;
}

                    cout << "Popped element is ::\n";
cout <<  top  ->  bookid  <<  "  "  <<  top -> bookname;
 Book * temp = top;
                    top = top -> next  ;
                   delete temp ;
                  
             }

           void display ( )
            {

                      if(top == NULL )
                     {
                             cout << "\nStack is empty !" ;
                             getch( );
                             return;
}

                  
                   cout  << "\n Stack contents are ....\n" ;
                   Book *ptr = top ;
                   while ( ptr )
                    {
                              cout <<  ptr -> bookid << " " << ptr  ->  bookname  <<endl ;
                              ptr = ptr -> next ;
                    }
               
            }

 } ;

void main()
 {
          clrscr ( ) ;
          Stack S ;
          int ch ;
          while ( 1 )
           {
                   cout << "\n1.        Push an element" ;
                   cout << "\n2.        Pop an element" ;
                   cout << "\n3.        Display the stack" ;
                   cout << "\n4.        Exit" ;
                   cin  >> ch ;
                   switch ( ch )
                    {
                             case 1 :
                                      S.Push ( ) ;
                                      break ;
                             case 2 :
                                      S.Pop ( ) ;
                                      break ;
                             case 3 :
                                      S.display ( ) ;
                                      break ;
                             case 4 :
                                      exit ( 0 ) ;
                    }
                   getch( ) ;
           }
 }

Program 19


         
ARRAY IMPLEMENTATION OF STACK



Write a program to implement an ARRAY STACK.

#include < iostream . h >
#include < process . h >
#include < stdio . h >
#include < conio . h >
class Stack
 {
           int top ;
           int arr[ 10 ] ;
    public  :
           Stack ( )
            {
                    top = -1 ; //denotes stack is empty
            }
           int isFull ( )
            {
                   if( top == 9 )
                    {
                      cout << "\nStack is Full!" ;
                      getch ( ) ;
                      return 1 ;
                    }
                   else
                      return 0 ;
            }
           int isEmpty ( )
            {
                   if ( top == -1 )
                     {
                             cout << "\nStack is empty !" ;

                             getch ( ) ;
                             return 1 ;
                     }
                   else
                             return 0 ;
            }
           void Push ( int a )
            {
                   if  ( ! isFull  (  )  )
                     {
                             arr [ ++ top ] =  a ;
                             cout << "\nElement Pushed" ;
                     }
            }
           void Pop ( )
            {
                   if (  ! isEmpty  ( )  )
                             cout  << "Popped element is ::" << arr [ top -- ]  ;
            }
           void display ( )
            {

                if ( ! isEmpty ( ) )
                 {
                   cout << "\n Stack contents are ...." ;
                   for ( int i = top ;  i >= 0 ; -- i )
                    cout <<  arr [ i ] << "  " ;
                 }
            }
 } ;
void main ( )
 {
          clrscr ( ) ;
          Stack S ;
          int ch  , n ;
          while ( 1 )
           {
                   cout << "\n1.        Push an element" ;
                   cout << "\n2.        Pop an element" ;
                   cout << "\n3.        Display the stack" ;
                   cout << "\n4.        Exit" ;
                   cin   >>  ch ;
                   switch ( ch )
                    {
                             case  1  :
                                      cout << "\nEnter the element to be pushed::" ;
                                      cin  >>n ;
                                      S . Push ( n ) ;
                                      break ;
                             case  2  :      S . Pop ( ) ;           break ;
                             case  3  :       S . display ( ) ;     break ;
                             case  4  :      exit ( 0 ) ;
                    }
                   getch ( ) ;
           }
 }