Question

In: Computer Science

C++ language We are given a Queue data structure that supports standard operations like Enqueue() and...

C++ language

We are given a Queue data structure that supports standard operations like Enqueue() and Dequeue(): Enqueue(element): add a new element at the tail of the queue; Dequeue(): delete the element at the head of the queue.

Show how to implement a stack using two queues. Analyze the running time of the stack operations: Push and Pop.

Solutions

Expert Solution

Code for queue implementation:

#include <iostream>
using namespace std;
int queue[100], n = 100, front = - 1, rear = - 1;   //initially both pointer is poinred to -1 in queue
void Insert() {
   int val;
   if (front == n - 1)          //check an queue is full then we can not insert element
                cout<<"Queue Overflow"<<endl;
   else {
      if (rear == - 1)                  //check the element to be inserted is first 
                rear = 0;         // then fornt is pointed 0
      cout<<"Insert the element in queue : "<<endl;
      cin>>val;
      front++;                   //for inserting element increment front
      queue[front] = val;        //and inserted element where front is present 
   }
}
void Delete() {
   if (rear == - 1 || rear > front) {                //check an queue is empty and rear is greater than front
      cout<<"Queue Underflow ";              // then we cant delete any element from queue because its empty
      return ;
   } else {
      cout<<"Element deleted from queue is : "<< queue[rear] <<endl;   //return deleted element
      rear++;    //then increment rear
   }
}
void Display() {
   if (rear == - 1)
                cout<<"Queue is empty"<<endl;   //if rear is pointed -a then queue is empty
   else {
      cout<<"Queue elements are : ";
      for (int i = rear; i <= front; i++)            //traverse all element from that are in queue from front till front 
        cout<<queue[i]<<" ";                   //print element
      cout<<endl;  
   }
}
int main() {
   int ch;
   cout<<"1) Insert element to queue"<<endl;
   cout<<"2) Delete element from queue"<<endl;
   cout<<"3) Display all the elements of queue"<<endl;
   cout<<"4) Exit"<<endl;
   do {
      cout<<"Enter your choice : "<<endl;     //let user to choice what user want to do
      cin>>ch;
      switch (ch) {
         case 1: Insert();
         break;
         case 2: Delete();
         break;
         case 3: Display();
         break;
         case 4: cout<<"Exit"<<endl;
         break;
         default: cout<<"Invalid choice"<<endl;
      }
   } while(ch!=4);
   return 0;
}

OUTPUT

Implementation of stack using 2 queue:

For all stack operations (push, pop, isEmpty, size) time complexity is  O(1).

Stack follow the technic LIFO(Last-in-First-Out) any element insert or deleted from the top of stack. So For pish operation it directly insert element at top of stack. For pop any element we can choose element in one time so the time complexity of the stack is O(1)


Related Solutions

Suppose you start with an empty queue and perform the following operations: enqueue 1, enqueue 2,...
Suppose you start with an empty queue and perform the following operations: enqueue 1, enqueue 2, dequeue, enqueue 3, enqueue 4, dequeue, enqueue 5. What are the resultant contents of the queue, from front to back? Group of answer choices 1, 2, 3, 4, 5 1, 3, 5 1, 2, 3 3, 4, 5 Assume you are using the text's array-based queue and have just instantiated a queue of capacity 10. You enqueue 5 elements, dequeue 4 elements, and then...
Discuss the relative efficiency of the enqueue and dequeue operations for an array-based queue implemented with...
Discuss the relative efficiency of the enqueue and dequeue operations for an array-based queue implemented with a fixed-front approach as opposed to a floating-front approach.
C language Write a program in C to implement Queue and its operation (like create, insert,...
C language Write a program in C to implement Queue and its operation (like create, insert, delete, search) using array data structure.
The following sequence of operations essentially leaves a queue unchanged. Group of answer choices enqueue followed...
The following sequence of operations essentially leaves a queue unchanged. Group of answer choices enqueue followed by dequeue dequeue followed by enqueue two enqueues followed by two dequeues two isEmptys followed by two isFulls A standard linked list provides a good implementation of a "Deque". Group of answer choices True False The main thread of a Java program cannot generate additional threads. Group of answer choices True False The text's link-based queue is being used and holds an empty queue....
Given a Queue of Integers with the interface: public void enqueue(Integer i) // add to end...
Given a Queue of Integers with the interface: public void enqueue(Integer i) // add to end public Integer dequeue() // remove from front public boolean isEmpty() // return true if empty Write a method rearrange(Queue q) that takes a queue of integers as a parameter and rearranges the order of the values so that all of the even values appear before the odd values and that otherwise preserves the original order of the list. For example, if a Queue contained...
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and...
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and create a test to validate its functionality. The data consists of persons with the attributes of name, last name, age, height and weight. - Remembrer that, Their structure consists of: Head: Pointer to the first element of the queue Tail: Pointer to the last element of the queue And the following operations: Pop: Removes the element at the head Top: Returns the current element...
Solve in C++ program. Modify the use of queue data structure such that the array used...
Solve in C++ program. Modify the use of queue data structure such that the array used to implement the queue is dynamically allocated for a fast food autoservice
Create a dynamic array-based Queue ADT class in C++ that contains enqueue(Inserts newElement at the back...
Create a dynamic array-based Queue ADT class in C++ that contains enqueue(Inserts newElement at the back ) and dequeue(Removes the frontmost element ). If the size of the array is equal to the capacity a new array of twice the capacity must be made. The interface is shown: class Queue { private: int* elements; unsigned elementCount; // number of elements in the queue unsigned capacity; // number of cells in the array unsigned frontindex; // index the topmost element unsigned...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may not use any standard Java or C++ libraries. Assume your data structure only allows Strings. Implement the following operations for the data structure: Queue: enqueue, dequeue, create, isEmpty (10 points) Stack: push, pop, create, isEmpty (10 points) Here is a link to get started on transferring from Java to C++ http://www.horstmann.com/ccj2/ccjapp3.html (Links to an external site.) Upload a zip file with one implementation for...
Study and implement data structure deque (double ended queue, pronounced as "deck"). IN C++ PLEASE
Study and implement data structure deque (double ended queue, pronounced as "deck"). IN C++ PLEASE
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT