Question

Write C++ program that implements the queue data structure using an array and perform the following operations: i) Enqueue ii

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;
const int capacity=100;
int queue[capacity];
int front = - 1;
int rear = - 1;
void Enqueue() {
int val;
if (rear == capacity - 1)
cout<<"Overflow Occured in Queue\n";
else {
if (front == - 1)
{front = 0;}
cout<<"Enter an integer to insert in the Queue:";
cin>>val;
rear=rear+1;
queue[rear] = val;
}
}
void Dequeue() {
if (front == - 1 || front > rear)
cout<<"Underflow has occured in the Queue\n";
else {
cout<<queue[front]<<"is removed from Queue\n";
front++;;
}
}
void Display() {
if (front == - 1)
cout<<"Empty Queue."<<endl;
else {
cout<<"Elements in the Queue are:\n";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
int main() {
int ch;
while(1) {
cout<<"1. Enqueue\n";
cout<<"2. Dequeue\n";
cout<<"3. Display\n";
cout<<"4. Exit\n";
cout<<"Enter your choice : ";
cin>>ch;
if(ch==1)
Enqueue();
else if(ch==2)
Dequeue();
else if(ch==3)
Display();
else if(ch==4)
break;
else
{cout<<"Invalid choice\n";break;}
}

return 0;
}

Debug Save {}Beautify $ Language C++ 1 # VE RE input Run Stop Share OnlineGDB beta main.cpp online compiler and debugger for

Add a comment
Know the answer?
Add Answer to:
Write C++ program that implements the queue data structure using an array and perform the following...
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
  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

  • 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...

  • C++ queue data sructure ising linked list. Design and implement a Queue data structure using linked...

    C++ queue data sructure ising linked list. 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.

  • Suppose we have an array-based queue (circular buffer) of size 6: int data[6]; int front =...

    Suppose we have an array-based queue (circular buffer) of size 6: int data[6]; int front = 0, back = 0; void enqueue(int x) { data[back] = x; back = (back + 1) % 6; } void dequeue() { front = (front + 1) % 6; } and we perform the following series of queue operations: enqueue(1); dequeue(); enqueue(2); dequeue(); enqueue(7); enqueue(3); enqueue(5); dequeue(); dequeue(); enqueue(4); enqueue(6); Write the state of the queue array after each operation, and at the end,...

  • 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.

  • C++ I need help to create a program that executes a simple queue data type. You...

    C++ I need help to create a program that executes a simple queue data type. You must define the queue data type and use the enqueue(), dequeue(), and displayQueue() functions. (Dont use C++ STL to execute queue logic) You may use a linked list , or just use a regular C array of integers. The input file (testfile.txt) will be the list of operations to carry out on a queue. Example input file: enqueue 6 enqueue 8 dequeue enqueue 4...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...

    (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert the next element even if there is a space in front of queue. Efficiently implement a queue class using a circular...

  • (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...

  • 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...

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