what is the basic difference between stack and queue?

后端 未结 11 760
死守一世寂寞
死守一世寂寞 2020-12-07 08:09

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 a

相关标签:
11条回答
  • 2020-12-07 08:27

    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.

    0 讨论(0)
  • 2020-12-07 08:29

    Imagine a stack of paper. The last piece put into the stack is on the top, so it is the first 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".

    0 讨论(0)
  • 2020-12-07 08:35

    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.

    Items are inserted & deleted at one end called ‘Top of the stack’.

    It is a dynamic & constantly changing object.

    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)

    0 讨论(0)
  • 2020-12-07 08:40

    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.
    0 讨论(0)
  • 2020-12-07 08:41

    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 from the front of the queue.

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

    0 讨论(0)
  • 2020-12-07 08:43

    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.

    0 讨论(0)
提交回复
热议问题