Tuesday, July 10, 2018

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 ( ) ;
           }
 }

No comments:

Post a Comment