Question

IN C++ - [(1)] Design a Stack that is composed ONLY of one or two Queue...

IN C++

- [(1)] Design a Stack that is composed ONLY of one or two Queue objects ergo the ONLY instance variables that exist in this stack are queues. Stack class should contain the following methods: Print, Pop, Push, Top, Size, isEmpty, copy [(2)] Design a Queue that is composed ONLY of two Stacks objects ergo the ONLY instance variables that exist in this queue are stacks. Queue class should contain the following methods: Print, Enqueue, Dequeue, Front, Rear, Size, isEmpty, Copy

Homework Answers

Answer #1

ANSWER :-

1) Program to implement a stack using two queue -

#include <iostream>

using namespace std;

class Stack {

    queue<int> q1, q2;

  int curr_size;

public:

    Stack()

    {

        curr_size = 0;

    }

void push(int x)

    {

       curr_size++;

// Push x first in empty q2

        q2.push(x);

// Push all the remaining

        // elements in q1 to q2.

        while (!q1.empty()) {

            q2.push(q1.front());

            q1.pop();

        }

        queue<int> q = q1;

        q1 = q2;

        q2 = q;

    }

void pop()

    {

// if no elements are there in q1

        if (q1.empty())

            return;

        q1.pop();

        curr_size--;

    }

int top()

    {

        if (q1.empty())

            return -1;

        return q1.front();

    }

   int size()

    {

        return curr_size;

    }

};

// Driver code

int main()

{

    Stack s;

    s.push(1);

    s.push(2);

    s.push(3);

    cout << "current size: " << s.size()

         << endl;

    cout << s.top() << endl;

    s.pop();

    cout << s.top() << endl;

    s.pop();

    cout << s.top() << endl;

cout << "current size: " << s.size()

         << endl;

    return 0;

}

2) program implement queue using two stack-

#include <iostream>

using namespace std;

class Queue {
public:
Stack S1, S2;
  
//declaring enqueue method
void enqueue(int x);
  
//declaring dequeue method
int dequeue();
};

// enqueue function
void Queue :: enqueue(int x)
{
S1.push(x);
cout << "Element Inserted into Queue\n";
}

// dequeue function
int Queue :: dequeue()
{
int x, y;
while(!S1.isEmpty())
{
// take an element out of first stack
x = S1.pop();
// insert it into the second stack
S2.push(x);
}
    
y = S2.pop();

while(!S2.isEmpty())
{
x = S2.pop();
S1.push(x);
}
  
return y;
}

// main function
int main()
{
Queue q;
q.enqueue(10);
q.enqueue(100);
q.enqueue(1000);
cout << "Removing element from queue" << q.dequeue();
  
return 0;
}

Know the answer?
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
Similar Questions
Generate 100 random numbers and add them to a stack and a queue, after print out...
Generate 100 random numbers and add them to a stack and a queue, after print out the content from both stack and queue, sort the data from both in ascending order, then print out again, after being sorted, and go through both the stack a queue and remove an element one at a time till empty then print out a message saying its empty. print out how much time it took to do this process, basically compare the stack and...
TestQueue.java Design a class named Queue for storing integers. Like a stack, a queue holds elements....
TestQueue.java Design a class named Queue for storing integers. Like a stack, a queue holds elements. But in a queue, the elements are retrieved in a first-in first-out fashion. The class contains: An int[] data field named elements that stores the int values in the queue. A data field named size that stores the number of elements in the queue. A constructor that creates a Queue object with default capacity 8. The method enqueue(int v) that adds v into the...
Implement the selection sort algorithm on a Queue of long-type items. Specifically, you are given the...
Implement the selection sort algorithm on a Queue of long-type items. Specifically, you are given the Queue class implementation and you need to write a method that takes a Queue and sorts it using the selection sort idea. The implementation of the Queue class for long data type is given in the lecture slides (the code skeleton including the implementation of the Queue class is provided below for your convenience). You should go over the Queue and use enqueue(), dequeue(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your...
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your Stack class by determining if the following strings have matching open and closing ( ) and/or [ ] and/or { }. To test matches, push open (, [, { onto the stack. Input a close char, pop off the stack and see if input matches symbol form stack.   Use the following data to test your code:                               ( )                               [ ] ( )...
Consider the following pseudo-code: /* Global memory area accessible by threads */ #define N 100 struct...
Consider the following pseudo-code: /* Global memory area accessible by threads */ #define N 100 struct frame *emptyStack[N]; struct frame *displayQueue[N]; int main() { /* ** Initialise by allocating the memory for N frames ** And place the N frame addresses into the ** empty Stack array */ Initialise(); thread_t tid1, tid2; threadCreate(&tid1, GetFrame); threadCreate(&tid2, ShowFrame); sleep(300); } GetFrame() { struct frame *frame; struct frame local; while (1) { CameraGrab(&local); /* get a frame from the camera store it in...
Please answer the following as soon as possible. Thank you. Add the top method in class...
Please answer the following as soon as possible. Thank you. Add the top method in class Stack to the following python code which returns the top item of the stack. Test it. Design top() method using a single queue as an instance variable, and only constant additional local memory within the method bodies. python code: class Stack: def __init__(self): self.q = Queue() def is_empty(self): return self.q.is_empty() def push(self, data): self.q.enqueue(data) def pop(self): for _ in range(self.q.get_size() - 1): dequeued =...
1. Which of the following statements is FALSE? a. A transformer operation changes the object (e.g....
1. Which of the following statements is FALSE? a. A transformer operation changes the object (e.g. list.add(element) changes the list by adding an element) b. An observer operation may also modify the object (e.g. list.isFull( ) may increase the size of the list if no more elements can be added) c. An observer operation returns information about an object (e.g. list.size() returns the number of elements in the list) d. A transformer operation may or may not return a value....
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
A stack can be used to print numbers in other bases (multi-base output). Examples: (Base8) 2810...
A stack can be used to print numbers in other bases (multi-base output). Examples: (Base8) 2810 = 3 * 81 + 4 * 80 = 348 (Base4) 7210 = 1 * 43 + 0 * 42 + 2 * 41 + 0 * 4 = 10204 (Base 2) 5310 = 1 * 25 + 1*24 + 0 * 23 + 1 * 22 + 0 * 21 + 1 * 20 = 1101012 Write a java program using stacks that...