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 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.
Imagine a stack of paper. The first piece put into the stack is at the bottom, so it is the last one to come out. This is LIFO. Adding a piece of paper is called "pushing", and removing a piece of paper is called "popping".
Imagine a queue at the store. The first person in line is the first person to get out of line. This is FIFO. A person getting into line is "enqueued", and a person getting out of line is "dequeued".
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!
链接地址: http://www.djcxy.com/p/14016.html上一篇: 什么是类型跟着
下一篇: 堆栈和队列之间的基本区别是什么?