Question
C++ queue data sructure ising linked list.
Design and implement a Queue data structure using
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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<<"\n1. Enqueue\n2. Dequeue\n3. Is Full?\n4. Is empty?\n5. Display all elements\n6. Add two Queues\n7. Quit";
           cout<<"\nEnter your choice: ";
           cin>>choice;
           switch(choice){
           case 1:
               if(!queue.is_full()){
               cout<<"\nEnter 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 \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

Add a comment
Know the answer?
Add Answer to:
C++ queue data sructure ising linked list. Design and implement a Queue data structure using linked...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In c++ language Design and implement a Queue data structure using linked list. Support the following...

    In c++ language Design and implement a Queue data structure using linked list. Support the following usual operations: default constructor parameterized constructor to create a queue of user-specified capacity enqueue dequeue is_full is_empty display destructor that deallocates all the nodes copy constructor overloaded assignment operator Demonstrate using a main function.

  • You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow...

    You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow the format** **Don't worry about the readme.txt** THANK YOU SO SO MUCH! You have to implement the doubly linked list in DList.h and DList.cpp and the queue in the LinkedQueue.h and LinkedQueue.cpp; all as template classes. You have to provide the main.cpp file as we discussed in class to allow the user to interact with your queue using enqueue, dequeue, front, back, size, empty,...

  • Write a C++ program to implement a queue using linked lists. You can use the queue...

    Write a C++ program to implement a queue using linked lists. You can use the queue data structure from the Standard Template Library (STL). The program should provide the following functionality: Enqueue data into queue Dequeue data from queue Print data at the front Print data at the back Print the entire queue Check if the queue is empty Print the number of elements in the queue Test your program using at least the following test cases (considering the queue...

  • Using C++ language, Design and implement a class representing a doubly linked list. The class must...

    Using C++ language, Design and implement a class representing a doubly linked list. The class must have the following requirements: The linked list and the nodes must be implemented as a C++ templates The list must be generic – it should not implement arithmetic/logic functions. (template class) It must include a destructor and a copy constructor It must include methods to insert at the front and at the back of the list It must include a method to return the...

  • QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities....

    QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...

  • solving using C. Use a singly linked list to implement a priority queue with two operations:...

    solving using C. Use a singly linked list to implement a priority queue with two operations: enqueue and dequeue. Each node contains an integer value, a priority, and a pointer to the next node. The priority is a value between 1- 10 (where 10 is the highest priority). When a value is added to the queue, it is added with a value and priority. When a value is removed from the priority queue, the first element with the highest priority...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • (Data Strcture) Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a...

    (Data Strcture) Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a Singly Linked-List and Implementing a Priority Queue Tasks/Assignments(s) ■ Write your own program to implement priority queue using the Linked-List and implement the Enqueue, Dequeue and Display methods. implement the Merge methods in your main program that receive Q1 and Q2 and merge the two queues into one queue. Public static PriorityQueue MergeQueue(PriorityQueue Q1,PriorityQueue Q2) Write main program to test priority queue class and...

  • How do I implement overload operator == C++ No late submission Submission instructions: Name your file...

    How do I implement overload operator == C++ No late submission Submission instructions: Name your file as (Last NameFirst Name.cpp). Up- load to Blackboard. 1. (Total 100 Points) Write a C++ program that implements a binary search tree (BST) to manage a number of integer items with different priorities. In order to do so, you will need to maintain a queue as an item in the tree. Each queue item is an integer. All items in a given queue will...

  • Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a...

    Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure. Write the required Java code to implement either a Stack or a Queue data structure based on a linked list. The code should include the class constructors, the necessary properties, and methods to add and remove elements from the data...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT