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

C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H...
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H #define STACK_H #include "Node.h" template class Stack { private: Node* top; public: // Constructor Stack() { top = nullptr; } void push(Object ab) { if (top != nullptr) { Node* newNode = new Node(ab, *top); top = newNode; } else { Node* newNode = new Node(ab); top = newNode; } } Object pop() { if (top != nullptr) { Node *returnVal = top; top...
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...
Using Python list tools, create the standard stack (push, pop) and queue (enqueue, dequeue) operations Counting...
Using Python list tools, create the standard stack (push, pop) and queue (enqueue, dequeue) operations Counting Letter Challenge: Create a function that... Takes in a string as parameter Counts how often each letter appears in the string Returns a dictionary with the counts BONUS: make it so lowercase and uppercase letter count for the same letter
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...
True False The enqueue and dequeue operations in a priority queue take O(lg n) time, while...
True False The enqueue and dequeue operations in a priority queue take O(lg n) time, while linked list and array implementations take O(1) time. A binary heap is a (nearly) complete binary tree. Heaps usually use a linked node structure for implementation. When implementing heaps as arrays, you can leave a blank space at the front. If you do, the parent of a node at index i can be found at index floor(i/2). When implementing heaps as arrays, if you...
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...
(based on 8.38) A stack is a sequence container type that, like a queue, supports very...
(based on 8.38) A stack is a sequence container type that, like a queue, supports very restrictive access methods: all insertions and removals are from one end of the stack, typically referred to as the top of the stack. A stack is often referred to as a list-in first-out (LIFO) container because the last item inserted is the first removed. Implement a Stack class. It should support the following methods/functions: _init__ - Can construct either an empty stack, or initialized...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT