You are on page 1of 5

what is the basic difference between stack and queue?

What is the basic difference between stack and queue??

Please help me i am unable to find the difference.

How do you differentiate a stack and a queue?

I searched for the answer in various links and found this answer..

In high level programming,

a stack is defined as a list or sequence of elements that is lengthened by placing new elements "on top" of existing elements and shortened by
removing elements from the top of existing elements. It is an ADT[Abstract Data Type] with math operations of "push" and "pop".

A queue is a sequence of elements that is added to by placing the new element at the rear of existing and shortened by removing elements in
front of queue. It is an ADT[Abstract Data Type]. There is more to these terms understood in programming of Java, C++, Python and so on.

Can i have an answer which is more detailed? Please help me.

stack queue

edited Oct 27 '15 at 10:50 asked Jun 11 '12 at 5:39


Bikku Harish Rachakonda
5,072 9 31 49 458 2 6 7

11 You seem to have answered your own question - a stack is a Last-In First-Out (LIFO) container, and a
queue is a First-In First-Out (FIFO) container. – Iridium Jun 11 '12 at 5:47

11 Answers

Stack is a LIFO (last in first out) data structure. The associated link to wikipedia contains
detailed description and examples.

Queue is a FIFO (first in first out) data structure. The associated link to wikipedia contains
detailed description and examples.

answered Jun 11 '12 at 5:46


jhonkola
2,629 11 27

You can think of both as an ordered list of things (ordered by the time at which they were
added to the list). The main difference between the two is how new elements enter the list and
old elements leave the list.

For a stack, if I have a list a, b, c , and I add d , it gets tacked on the end, so I end up with
a,b,c,d . If I want to pop an element of the list, I remove the last element I added, which is d .
After a pop, my list is now a,b,c again

For a queue, I add new elements in the same way. a,b,c becomes a,b,c,d after adding d .
But, now when I pop, I have to take an element from the front of the list, so it becomes b,c,d .

It's very simple!

answered Jun 11 '12 at 5:47


cdesrosiers
7,692 2 18 30

Join Stack Overflow to learn, share knowledge, and build your career. Email Sign Up OR SIGN IN WITH Google Facebook

Imagine a stack of paper. The first piece put into the stack is at the bottom, so it is the last
Imagine a queue at the store. The first person in line is the first person to get out of the line.
This is FIFO.

edited Nov 12 '16 at 3:15 answered Jan 27 '16 at 7:28


123
4,271 5 27 47

A visual model

Pancake Stack (LIFO)

The only way to add one and/or remove one is from the top.

Line Queue (FIFO)

When one arrives they arrive at the end of the queue and when one leaves they leave at the
front of the queue.

Fun fact: the British refer to lines of people as a Queue

edited Nov 8 '17 at 17:42 answered Aug 16 '17 at 15:48


Jacksonkr
16.1k 30 127 227

1 Jee, this must be the answer imo. Thanks @Jacksonkr – Kulasangar Oct 24 '17 at 7:11

Queue

Queue is a ordered collection of items.

Items are deleted at one end called ‘front’ end of the queue.

Items are inserted at other end called ‘rear’ of the queue.

The first item inserted is the first to be removed (FIFO).

Stack

Stack is a collection of items.

It allows access to only one data item: the last item inserted.
Join Stack Overflow to learn, share knowledge, and build your career. Email Sign Up OR SIGN IN WITH Google Facebook
Items are inserted & deleted at one end called ‘Top of the stack’.
All the data items are put on top of the stack and taken off the top

This structure of accessing is known as Last in First out structure (LIFO)

edited Feb 20 '17 at 0:51 answered Oct 8 '13 at 7:43


123 Dissanayake
4,271 5 27 47 177 1 15

So basically a 'queue' is a "FIFO" - first in first out queue. While a 'stack' is a "LIFO" - last in first out queue.
Am I correct? – Sebastian Nielsen May 28 '17 at 14:47

@SebastianNielsen Yes correct as mention in the answer. – Dissanayake May 30 '17 at 5:37

But what is the difference then between a linked list and a stack? Isn't it the same? – Sebastian Nielsen May
30 '17 at 8:05

STACK:

1. Stack is defined as a list of element in which we can insert or delete elements only at the
top of the stack.
2. The behaviour of a stack is like a Last-In First-Out(LIFO) system.
3. Stack is used to pass parameters between function. On a call to a function, the
parameters and local variables are stored on a stack.
4. High-level programming languages such as Pascal, c, etc. that provide support for
recursion use the stack for bookkeeping. Remember in each recursive call, there is a need
to save the current value of parameters, local variables, and the return address (the
address to which the control has to return after the call).

QUEUE:

1. Queue is a collection of the same type of element. It is a linear list in which insertions can
take place at one end of the list,called rear of the list, and deletions can take place only at
other end, called the front of the list
2. The behaviour of a queue is like a First-In-First-Out (FIFO) system.

edited Apr 9 '13 at 4:46 answered Apr 9 '13 at 4:22


Simon MᶜKenzie ASHUTOSH KUMAR
5,633 13 29 52 101 1 2

I'm pretty sure you can insert at the end or start of a stack too, I think the important thing to note here is the
FIFO vs. LIFO – Mike Jul 15 '15 at 19:39

A stack is a collection of elements, which can be stored and retrieved one at a time. Elements
are retrieved in reverse order of their time of storage, i.e. the latest element stored is the next
element to be retrieved. A stack is sometimes referred to as a Last-In-First-Out (LIFO) or First-
In-Last-Out (FILO) structure. Elements previously stored cannot be retrieved until the latest
element (usually referred to as the 'top' element) has been retrieved.

A queue is a collection of elements, which can be stored and retrieved one at a time. Elements
are retrieved in order of their time of storage, i.e. the first element stored is the next element to
be retrieved. A queue is sometimes referred to as a First-In-First-Out (FIFO) or Last-In-Last-
Out (LILO) structure. Elements subsequently stored cannot be retrieved until the first element
(usually referred to as the 'front' element) has been retrieved.

answered Jun 11 '12 at 5:46


Luftwaffe
2,025 1 17 31

STACK: Stack is defined as a list of element in which we can insert or delete elements only at
the top of the stack

Stack is used to pass parameters between function. On a call to a function, the parameters
and local variables are stored on a stack.

A stack is a collection of elements, which can be stored and retrieved one at a time. Elements
are retrieved in reverse order of their time of storage, i.e. the latest element stored is the next
element to be retrieved. A stack is sometimes referred to as a Last-In-First-Out (LIFO) or First-
In-Last-Out (FILO) structure. Elements previously stored cannot be retrieved until the latest
Email Sign Up
Join Stack Overflow to learn, share knowledge, and build your career. OR SIGN IN WITH Google Facebook
element (usually referred to as the 'top' element) has been retrieved.
Queue is a collection of the same type of element. It is a linear list in which insertions can take
place at one end of the list,called rear of the list, and deletions can take place only at other
end, called the front of the list

A queue is a collection of elements, which can be stored and retrieved one at a time. Elements
are retrieved in order of their time of storage, i.e. the first element stored is the next element to
be retrieved. A queue is sometimes referred to as a First-In-First-Out (FIFO) or Last-In-Last-
Out (LILO) structure. Elements subsequently stored cannot be retrieved until the first element
(usually referred to as the 'front' element) has been retrieved.

answered Apr 17 '13 at 15:09


shashimani
1

STACK is a LIFO (last in, first out) list. means suppose 3 elements are inserted in stack i.e
10,20,30. 10 is inserted first & 30 is inserted last so 30 is first deleted from stack & 10 is last
deleted from stack.this is an LIFO list(Last In First Out).

QUEUE is FIFO list(First In First Out).means one element is inserted first which is to be
deleted first.e.g queue of peoples.

edited Mar 9 '14 at 19:17 answered Mar 9 '14 at 15:03


mah apurva sanjay borse
31.2k 8 53 83 11 1

Stacks a considered a vertical collection. First understand that a collection is an OBJECT that
gathers and organizes other smaller OBJECTS. These smaller OBJECTS are commonly
referred to as Elements. These elements are "Pushed" on the stack in an A B C order where A
is first and C is last. vertically it would look like this: 3rd element added) C 2nd element added)
B 1st element added) A

Notice that the "A" which was first added to the stack is on the bottom. If you want to remove
the "A" from the stack you first have to remove "C", then "B", and then finally your target
element "A". The stack requires a LIFO approach while dealing with the complexities of a
stack.(Last In First Out) When removing an element from a stack, the correct syntax is pop. we
don't remove an element off a stack we "pop" it off.

Recall that "A" was the first element pushed on to the stack and "C" was the last item Pushed
on the stack. Should you decide that you would like to see what is on bottom the stack, being
the 3 elements are on the stack ordered A being the first B being the second and C being the
third element, the top would have to be popped off then the second element added in order to
view the bottom of the stack.

answered Apr 8 '14 at 16:42


Zimmermen7
1

Please format your question to make it look better and more readable. – Neeku Apr 8 '14 at 17:01

To try and over-simplify the description of a stack and a queue, They are both dynamic chains
of information elements that can be accessed from one end of the chain and the only real
difference between them is the fact that:

when working with a stack

• you insert elements at one end of the chain and


• you retrieve and/or remove elements from the same end of the chain

while with a queue

• you insert elements at one end of the chain and


• you retrieve/remove them from the other end

NOTE: I am using the abstract wording of retrieve/remove in this context because there are
instances when you just retrieve the element from the chain or in a sense just read it or access
its value, but there also instances when you remove the element from the chain and finally
there are instances when you do both actions with the same call.

Also the
Join word
Stack elementtois learn,
Overflow purposely
shareused in order and
knowledge, to abstract the career.
build your imaginary chain asEmail
muchSign
as Up OR SIGN IN WITH Google Facebook
possible and decouple it from specific programming language terms. This abstract information
entity called element could be anything, from a pointer, a value, a string or characters, an
In most cases, though it is actually either a value or a memory location (i.e. a pointer). And the
rest are just hiding this fact behind the language jargon<

A queue can be helpful when the order of the elements is important and needs to be exactly
the same as when the elements first came into your program. For instance when you process
an audio stream or when you buffer network data. Or when you do any type of store and
forward processing. In all of these cases you need the sequence of the elements to be output
in the same order as they came into your program, otherwise the information may stop making
sense. So, you could break your program in a part that reads data from some input, does
some processing and writes them in a queue and a part that retrieves data from the queue
processes them and stores them in another queue for further processing or transmitting the
data.

A stack can be helpful when you need to temporarily store an element that is going to be used
in the immediate step(s) of your program. For instance, programming languages usually use a
stack structure to pass variables to functions. What they actually do is store (or push) the
function arguments in the stack and then jump to the function where they remove and retrieve
(or pop) the same number of elements from the stack. That way the size of the stack is
dependent of the number of nested calls of functions. Additionally, after a function has been
called and finished what it was doing, it leaves the stack in the exact same condition as before
it has being called! That way any function can operate with the stack ignoring how other
functions operate with it.

Lastly, you should know that there are other terms used out-there for the same of similar
concepts. For instance a stack could be called a heap. There are also hybrid versions of these
concepts, for instance a double-ended queue can behave at the same time as a stack and as
a queue, because it can be accessed by both ends simultaneously. Additionally, the fact that a
data structure is provided to you as a stack or as a queue it does not necessarily mean that it
is implemented as such, there are instances in which a data structure can be implemented as
anything and be provided as a specific data structure simply because it can be made to
behave like such. In other words, if you provide a push and pop method to any data structure,
they magically become stacks!

edited Dec 30 '16 at 22:58 answered Oct 13 '16 at 12:43


EJP Agelos Assonitis
237k 22 186 306 54 4

Don't use code formatting for text that isn't code. – EJP Dec 30 '16 at 22:59

protected by Community ♦ Jun 28 '15 at 15:18


Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on
this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Join Stack Overflow to learn, share knowledge, and build your career. Email Sign Up OR SIGN IN WITH Google Facebook

You might also like