Design and implement a Queue data structure using linked lis

Design and implement a Queue data structure using linked list. Support the following usual operations: (a) default constructor (b) parameterized constructor to create a queue of user-specified capacity (c) enqueue (d) dequeue (e) is_full (f) is_empty display (h) destructor that deallocates all nodes (i) copy constructor (j) overloaded assignment operator Demonstrate using a main function.

Solution

PROGRAM CODE:

/*
* Queue.cpp
*
* Created on: 17-Nov-2016
* Author: kasturi
*/


#include<iostream>
#include<cstdlib>
#define MAX_SIZE 10
using namespace std;

class Queue{
   private:
       int item[MAX_SIZE];
       int rear;
       int front;
   public:
       Queue();
Queue( Queue &q);
~Queue();
       void enqueue(int);
       int dequeue();
       void display();
       bool is_empty();
       bool is_full();
int size();
Queue operator+(Queue &q1);
};

       Queue::Queue(){
           rear = -1;
           front = 0;
       }
       int Queue::size(){
           return (rear - front + 1);
       }
       Queue::Queue( Queue &q){
           rear = q.rear;
           front = q.front;
           for(int i=0; i<q.size(); i++)
           {
               item[i] = q.item[i];
           }
       }

       Queue Queue::operator+( Queue &q1)
       {
           Queue q3;
           for(int i=this->front; i<=this->rear; i++)
           {
               q3.item[++rear] = this->item[i];
           }
           for(int i=q1.front; i<=q1.rear; i++)
           {
               q3.item[++rear] = q1.item[i];
           }
           return q3;
       }
       Queue::~Queue(){
           delete this;
   }
   void Queue::enqueue(int data){
       item[++rear] = data;
   }
   int Queue::dequeue(){
       return item[front++];
   }
   void Queue::display(){
       if(!this->is_empty()){
       for(int i=front; i<=rear; i++)
       cout<<item[i]<<\" \";
       }else{
       cout<<\"Queue is Underflow\"<<\" \";
       }
       cout<<endl;
   }
   bool Queue::is_empty(){
   if(front>rear){
       return true;
       }else{
       return false;
       }
   }
   bool Queue::is_full(){
       if(this->size()>=MAX_SIZE){
       return true;
       }else{
       return false;
       }
   }
   int main(){
       Queue queue, q3, q2;
       int choice, data;
       char contChoice = \'Y\';
       while(contChoice == \'Y\' || contChoice == \'y\'){
           cout<<\"\ 1. Enqueue\ 2. Dequeue\ 3. Is Full?\ 4. Is empty?\ 5. Display all elements\ 6. Add two Queues\ 7. Quit\";
           cout<<\"\ Enter your choice: \";
           cin>>choice;
           switch(choice){
           case 1:
               if(!queue.is_full()){
               cout<<\"\ Enter data: \";
               cin>>data;
               queue.enqueue(data);
               }else{
               cout<<\"Queue is Full\"<<endl;
               }
               break;
           case 2:
               if(!queue.is_empty()){
                   cout<<\"The data dequeued is :\"<<queue.dequeue();
               }else{
                   cout<<\"Queue is Empty\"<<endl;
               }
               break;
           case 3:
               queue.is_full() == 0? cout<<\"false\" : cout<<\"true\";
               break;
       case 4:
           queue.is_empty()== 0? cout<<\"false\" : cout<<\"true\";
           break;
           case 5:
               queue.display();
               break;
           case 6:
                   q2.enqueue(1);
                   q2.enqueue(2);
                   cout<<\"Queue1: \";
                   queue.display();
                   cout<<\"Queue2: \";
                   q2.display();
                   cout<<\"New Queue: \";
                   q3 = queue + q2;
                   q3.display();
                   break;
           case 7:
               exit(0);
               break;
           }
           cout<<endl<<\"Do you want to continue? Y or N \ \";
           cin>>contChoice;
       }
       return 0;
   }

OUTPUT:

1. Enqueue

2. Dequeue

3. Is Full?

4. Is empty?

5. Display all elements

6. Add two Queues

7. Quit

Enter your choice: 1

Enter data: 34

Do you want to continue? Y or N

y

1. Enqueue

2. Dequeue

3. Is Full?

4. Is empty?

5. Display all elements

6. Add two Queues

7. Quit

Enter your choice: 1

Enter data: 35

Do you want to continue? Y or N

y

1. Enqueue

2. Dequeue

3. Is Full?

4. Is empty?

5. Display all elements

6. Add two Queues

7. Quit

Enter your choice: 5

34 35

Do you want to continue? Y or N

n

 Design and implement a Queue data structure using linked list. Support the following usual operations: (a) default constructor (b) parameterized constructor to
 Design and implement a Queue data structure using linked list. Support the following usual operations: (a) default constructor (b) parameterized constructor to
 Design and implement a Queue data structure using linked list. Support the following usual operations: (a) default constructor (b) parameterized constructor to
 Design and implement a Queue data structure using linked list. Support the following usual operations: (a) default constructor (b) parameterized constructor to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site