1

I've been getting mixed information from the web.

A quick Google search for what I ask will give many results saying they are separate and operate differently. Enqueue/dequeue for queues and push/pop for stack.

Here for example: Data structures: explanation of pop, push, dequeue, enqueue in this type of exercise

However in reality, push/pop works for both stacks and queues. In stack Push adds to the top of stack while Pop removes from the top of the stack. In queue Push adds to the back of the queue while Pop removes from the front of the queue.

As shown here: https://www.geeksforgeeks.org/queue-cpp-stl/ https://www.geeksforgeeks.org/stack-in-cpp-stl/

So what gives? Theres a difference yes, but why use enqueue/dequeue when push/pop do the same thing? Can push/pop be used interchangably when working with queues or not? I feel like I'm missing something.

Thanks

`

#include <stdio.h>
#include <iostream>
using namespace std;
#include <stack>
#include <queue>

stack<int> CreateStack(int Array[], int length)
{
    stack<int> stk;

    for (int i = 0; i < length; i++)
    {
        stk.push(Array[i]);
    }
    return stk;
}

queue<int> CreateQueue(int Array[], int length)
{
    queue<int> que;

    for(int i = 0; i < length; i++)
    {
        que.push(Array[i]);
    }
    return que;
}


void printStack(stack<int> inputStack)
{
    cout << "Top of the stack" << endl;
    while(inputStack.empty() == false)
    {
        cout << inputStack.top() << endl;
        inputStack.pop();
    }
    cout << "Bottom of the stack" << endl;
}

void printQueue(queue<int> inputQueue)
{
    cout << "Front of the queue" << endl;
    while(inputQueue.empty() == false)
    {
        cout << inputQueue.front() << endl;
        inputQueue.pop();
    }
    cout << "Back of the queue";
}



int main()
{
    int stackArray[] = {2,4,6,8,10};
    int arraySize = sizeof(stackArray)/sizeof(stackArray[0]);
    stack<int> stk = CreateStack(stackArray, arraySize);
    queue<int> que = CreateQueue(stackArray, arraySize);
    printStack(stk);
    printQueue(que);
}

`

Prints:

Top of the stack

10

8

6

4

2

Bottom of the stack

Front of the queue

2

4

6

8

10

Back of the queue

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Vultiverse
  • 25
  • 5
  • 1
    As you seem to have noticed yourself, it's just different words for the same thing. In C++ you push to and pop from both stacks and queues. It's like many things in spoken and written languages as well, where some words are different but mean the same thing. For example, you can *enter* a house, or you can *go into* a house. For most cases they both mean the same thing. – Some programmer dude Dec 31 '22 at 22:50
  • 1
    In practice not much difference. But queues could in theory have two ends, and you can queue or dequeue items somewhere in the middle (e.g. with a [priority queue](https://en.cppreference.com/w/cpp/container/priority_queue)). A stack only has one end and then its called push and pop. – Pepijn Kramer Dec 31 '22 at 22:52
  • For C++ std::queue has push and pop, not dequeue and enqueue. – john Dec 31 '22 at 22:54
  • 2
    Some further remarks, don't teach yourself C++ from geekforgeeks, already some bad practices are showing (like `using namespace std;` and not passing stacks/queues by constant reference for printing). Learn from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ first. – Pepijn Kramer Dec 31 '22 at 22:55
  • 2
    To add, many, if not most of the code samples at that mentioned website will *not* compile with a standard C++ compiler, and most notably, Visual C++. The reason is that many of their examples are riddled with `#include ` nonsense, and usages of variable-length arrays which are not standard C++. – PaulMcKenzie Dec 31 '22 at 22:57
  • These are not `C++` terms, they are general computing terms. – Galik Dec 31 '22 at 23:30

1 Answers1

1

The difference between the two containers means that different terminology works best to describe what is happening to the humans that read and write the code that uses them!

As you realise a stack is a LIFO (last-in, first-out) container whereas a queue is a FIFO (first-in, first-out) one and if you refer to those URLs (to https://en.cppreference.com) you can see that really they have a range of operators that are subsets of those that a third container, the double-ended queue or deque has.

SlySven
  • 326
  • 3
  • 15