简体   繁体   中英

I am trying to implement a stack using two Queues but my "push" operation is problematic although the code seems to be fine

Here's the code for class "StackUsingTwoQueues"

The Class "queueUsingLL" is working fine and following are the methods used for this class

  1. isEmpty() returns true or false depending upon whether queue is empty or NOT
  2. size() returns the integer value for size of the queue
  3. enqueue(int element) stores the element at the tail of the queue
  4. dequeue() removes the value stored at the front of queue and returns it

Using this queue class I tried to implement the stack

class StackUsingTwoQueues {
    QueueUsingLL<int> q;
    QueueUsingLL<int> tempQ;
    

    public:
        bool isEmpty(){
            return q.isEmpty();
        }
        int pop(){
            if(isEmpty()){
                return -1;
            }
            else{
                return q.dequeue();
            }
        }
        int size(){
            return q.size();
        }

// following push method is problematic : -

        void push(int element){
            if(q.isEmpty()){
                q.enqueue(element);
            }
            else{
                for(int i = 0; i < q.size(); i++){
                    tempQ.enqueue(q.dequeue());
                }
                q.enqueue(element);
                for(int i = 0; i < tempQ.size(); i++){
                    q.enqueue(tempQ.dequeue());
                }
            }
        }

        int top(){
            return q.front();
        }
};

PLEASE NOTE THAT if I write following code for push() function, it works fine: -

void push(int element){
            while(!q.isEmpty()){
                tempQ.enqueue(q.dequeue());
            }
            q.enqueue(element);
            while(!tempQ.isEmpty()){
                q.enqueue(tempQ.dequeue());
            }
        }

Here's the driver function:-

Output expected: 1 2 3 4 5

Actual Output: 1 2 1 2 1

int main(){
    int arr[] = {1,2,3,4,5};
    StackUsingTwoQueues s;

    for(int i = 0; i < 5; i++){
        s.push(arr[i]);
        cout<<s.top()<<" ";
    }cout<<endl;

}

The conditions for this tasks are given. 2 queues shall be used.

The problem is that you use a variable value as the end condition in your for loops. You use the size() function, with the initial correct value. But then you are popping elements from the queue and then size will be one less.

That can logically not work.

Instead of this: Please assign the size() to a different variable, just before the loop. And then use this loop invariant value as the end-condition for the loop. Do this for both loops.

The rest of your function is perfectly fine.

Please see below your corrected code:

#include <iostream>
#include <list>

template <class T>
class QueueUsingLL {
    std::list<T> data{};
public:
    bool isEmpty() const { return data.empty(); }
    std::size_t size() const { return data.size(); }
    void enqueue(const T& value) { data.push_back(value); }
    T dequeue() { T value{ data.front() }; data.pop_front(); return value; }
    T front() { return data.front(); };
};


class StackUsingTwoQueues {
    QueueUsingLL<int> q;
    QueueUsingLL<int> tempQ;

public:
    bool isEmpty() {
        return q.isEmpty();
    }
    int pop() {
        if (isEmpty()) {
            return -1;
        }
        else {
            return q.dequeue();
        }
    }
    int size() {
        return q.size();
    }

    // following push method is problematic : -

    void push(int element) {
        if (q.isEmpty()) {
            q.enqueue(element);
        }
        else {
            std::size_t numberOfElements{ q.size() };
            for (std::size_t i = 0; i < numberOfElements; i++) {
                tempQ.enqueue(q.dequeue());
            }
            q.enqueue(element);

            numberOfElements =  tempQ.size() ;
            for (int i = 0; i < numberOfElements; i++) {
                q.enqueue(tempQ.dequeue());
            }
        }
    }
    int top() {
        return q.front();
    }
};
int main() {
    int arr[] = { 1,2,3,4,5 };
    StackUsingTwoQueues s;

    for (int i = 0; i < 5; i++) {
        s.push(arr[i]);
        std::cout << s.top() << " ";
    }
    std::cout << '\n';
    while (s.size())
        std::cout << s.pop() << ' ';
}

I made a stub class for your queue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM