I need help understand a Queue data structure.

I've previous learned the strut push/pop, last in first out data structure but I'm struggling to wrap my head around a queue data structure. All the videos I find confuse me.


I'm trying to answer this question.

what are the end results of a queue MyQ after the following sequence of operations?

MyQ.push_front(13);
MyQ.push_front(14);
MyQ.pop_back();
MyQ.push_front(12);
MyQ.push_front(8);
MyQ.pop_back();
MyQ.pop_back();



Work it out on paper.
13
14 13
14
12 14
8 12 14
8 12
8
I thought you always added new items to the back not the front?

what does push front mean
I thought you always added new items to the back not the front?

You can push items to the front or the back of the container. A stack will always pull from the same side of the container as the elements were placed. A queue will pull from the opposite side of the container where the elements were placed.

what does push front mean

It means just what it says, push the item to the front of the queue.

Remember a stack is a First In First Out (FIFO) data structure. A queue is a Last In First Out (LIFO) data structure.
Remember a stack is a First In First Out (FIFO) data structure. A queue is a Last In First Out (LIFO) data structure.
You have those backwards, stack is last in first out and queue is first in first out.
Think of it like this:

A stack is like a stack of plates that need washing. You start from the top, washing each dish you get, until you go to the bottom. Or stacked boxes, each on top of the other, and you need to move them. You start from the top.

A queue is like a queue in the supermarket waiting to pay for the products. If you get in the queue before the other person, you will pay before him and finish earlier.

Topic archived. No new replies allowed.