Home  >  Article  >  Backend Development  >  Data Structures: Difference Between Stack and Queue

Data Structures: Difference Between Stack and Queue

藏色散人
藏色散人Original
2019-03-02 14:49:564572browse

Data Structures: Difference Between Stack and Queue

Stack:

A stack is a linear data structure in which elements can only be inserted from the top of the list and delete. The stack follows the last-in-first-out principle, that is, the last element inserted is the first element out. Inserting an element into the stack is called a push operation, and removing an element from the stack is called a pop operation. In a stack, we always keep track of the last element present in the list using a pointer called top.

The diagram of the stack is as follows:

Data Structures: Difference Between Stack and Queue

Queue:

The queue is a linear data structure. In this structure, elements can only be inserted from one side of the list called "after" and elements can only be deleted from the other side of the list called "before". The queue data structure follows the FIFO (First In First Out) principle, that is, the first element inserted into the list is the first element deleted from the list. Inserting an element into the queue is called an enqueuing operation, and deleting an element is called a dequeuing operation.

In the queue, we always maintain two pointers, one pointer points to the element inserted on the first pointer and is still represented by the previous pointer in the list, and the other pointer points to the element inserted on the last pointer The element is represented by a pointer later.

The diagram of the queue is as follows:

Data Structures: Difference Between Stack and Queue

The difference between stack and queue

Stack Queue
Stack is based on the LIFO principle, that is, the last inserted element is the first element in the list. The queue is based on the FIFO principle, that is, the first element inserted is the first element coming out of the list.
Insertions and deletions in the stack occur only at one end of the list named top. Insertions and deletions in the queue are made from opposite ends of the list. Insertions occur at the back of the list, and deletions occur at the front of the list.
The insertion operation is called a push operation. The insertion operation is called the enqueuing operation.
The deletion operation is called a pop operation. The deletion operation is called a dequeue operation.
In the stack, we only maintain a pointer that accesses the list, called top, which always points to the last element in the list. In the queue, we maintain two pointers to access the list. The front pointer always points to the first element inserted into the list and still exists, and the back pointer always points to the last inserted element.


The above is the detailed content of Data Structures: Difference Between Stack and Queue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn