堆栈和队列之间的区别

In this article you will learn about difference between stack and queue.

在本文中,您将了解堆栈和队列之间的区别。

Stack

叠放

A Stack data structure works on Last In First Out principle, also abbreviated as LIFO. A Stack requires only one reference variable, known as a top pointer. Stack data structures are normally represented as a vertical representation of data items.

堆栈数据结构基于后进先出原理(也称为LIFO)工作。 堆栈仅需要一个参考变量,即顶部指针。 堆栈数据结构通常表示为数据项的垂直表示。

A Stack Data Structure can be represented in static arrays as well as linked lists. In a stack, the data items can be inserted and deleted from the same end. The element to be inserted first is the last element to get deleted. Both the operations, insertion and deletion occur at the TOP reference pointer. A Stack doesn’t necessarily contain ordered collection of data items.

堆栈数据结构可以用静态数组和链表表示。 在堆栈中,可以从同一端插入和删除数据项。 首先插入的元素是要删除的最后一个元素。 插入和删除操作都发生在TOP参考指针处。 堆栈不一定包含数据项的有序集合。

Stack Implementations:

堆栈实现:

Stack data structure is used in infix to postfix conversion, scheduling algorithms, evaluation of an expression and depth first search.

堆栈数据结构用于中缀到后缀的转换, 调度算法,表达式的评估和 深度优先搜索。

The condition to check if a stack is empty:

检查堆栈是否为空的条件:

1
2
3
4
5
6
7
int isEmpty()
{
if(top==-1)
return 1;
else
return 0;
}
1
2
3
4
5
6
7
int isEmpty ( )
{
if ( top == - 1 )
return 1 ;
else
return 0 ;
}

The condition to check if a stack is full:

检查堆栈是否已满的条件:

1
2
3
4
5
6
7
int isFull()
{
if(top==MAX-1)
return 1;
else
return 0;
}
1
2
3
4
5
6
7
int isFull ( )
{
if ( top == MAX - 1 )
return 1 ;
else
return 0 ;
}

Queue

队列

A Queue data structure works on First In First Out principle, also abbreviated as FIFO. A Queue requires two reference variables. Queue is normally represented as a horizontal representation of data items. The Queue data structure contains FRONT and REAR as its reference points for data processing.

队列数据结构基于先进先出原理工作,也简称为FIFO。 队列需要两个参考变量。 队列通常表示为数据项的水平表示。 队列数据结构包含FRONT和REAR作为其数据处理的参考点。

A Queue can also be represented in by static arrays as well as linked lists. In a Queue, the data items can be inserted and deleted from different ends. The element to be inserted first is the first element to get deleted. The insertion operation is done at rear end whereas the deletion occurs at the front end. A Queue contains ordered collection of data items. A Queue can be divided into sub sections and it has the following extensions: Double Ended Queue, Simple Queue, Priority Queue and Circle Queue.

队列也可以通过静态数组和链接列表来表示。 在队列中,可以从不同端插入和删除数据项。 首先插入的元素是要删除的第一个元素。 插入操作在后端进行,而删除发生在前端。 队列包含数据项的有序集合。 队列可以分为几个子部分,并具有以下扩展:双端队列,简单队列,优先级队列和循环队列。

Queue Implementations:

队列实现:

A Queue offers services in operations research, transportation and computer science that involves persons, data, events and objects to be stored for later processing.

队列提供运筹学,运输和计算机科学方面的服务,涉及人员,数据,事件和要存储的对象,以供以后处理。

The condition to check if a queue is empty:

检查队列是否为空的条件:

1
2
3
4
5
6
7
int isEmpty()
{
    if(front==-1 || front==rear+1)
        return 1;
    else
        return 0;
}
1
2
3
4
5
6
7
int isEmpty ( )
{
     if ( front == - 1 || front == rear + 1 )
         return 1 ;
     else
         return 0 ;
}

The condition to check if a queue is full:

检查队列是否已满的条件:

1
2
3
4
5
6
7
int isFull()
{
    if(rear==MAX-1)
        return 1;
    else
        return 0;
}
1
2
3
4
5
6
7
int isFull ( )
{
     if ( rear == MAX - 1 )
         return 1 ;
     else
         return 0 ;
}
堆栈和队列之间的区别

Image Source

图片来源

堆栈和队列之间的区别 (Difference Between Stack and Queue)

Stack Queue
A Stack Data Structure works on Last In First Out (LIFO) principle. A Queue Data Structure works on First In First Out (FIFO) principle.
A Stack requires only one reference pointer. A Queue requires two reference pointers.
A Stack is primarily a vertical representation of data items. A Queue is a horizontal representation of data items.
A Stack contains TOP as its reference for data processing. A Queue contains REAR and FRONT as its reference for data processing.
The data items are inserted and deleted from the same end. The data items in a queue are inserted and deleted from different ends.
The element to be inserted first is removed last. The element to be inserted first is removed first.
To check if a stack is empty, following condition is used:

TOP == -1

To check if a queue is empty, following condition is used:

FRONT == -1 || FRONT == REAR + 1

The insertion and deletion operations occur at the TOP end of a Stack. The insertion operation occurs at REAR end and the deletion operation occurs at FRONT end.
A Stack data structure is not necessarily an ordered collection of data items. A Queue data structure is an ordered collection of data.
To check if a stack is full, following condition is used:

TOP == MAX – 1

To check if a queue is full, following condition is used:

REAR == MAX – 1

A Stack cannot be divided into sub sections and it doesn’t have extensions. A Queue can be divided into sub sections and it has the following extensions: Double Ended Queue, Simple Queue, Priority Queue and Circle Queue.
Used in infix to postfix conversion, scheduling algorithms, depth first search and evaluation of an expression. A Queue offers services in operations research, transportation and computer science that involves persons, data, events and objects to be stored for later processing.
叠放 队列
堆栈数据结构基于后进先出(LIFO)原理工作。 队列数据结构基于先进先出(FIFO)原理工作。
堆栈仅需要一个引用指针。 队列需要两个引用指针。
堆栈主要是数据项的垂直表示。 队列是数据项的水平表示。
堆栈包含TOP作为数据处理的参考。 队列包含REARFRONT作为数据处理的参考。
数据项从同一端插入和删除。 队列中的数据项将从不同端插入和删除。
首先删除要插入的元素。 首先删除要插入的元素。
要检查堆栈是否为空,请使用以下条件:

TOP == -1

要检查队列是否为空,请使用以下条件:

前== -1 || 前==后+ 1

插入和删除操作发生在堆栈的TOP端。 插入操作发生在REAR端,删除操作发生在FRONT端。
堆栈数据结构不一定是数据项的有序集合。 队列数据结构是数据的有序集合。
要检查堆栈是否已满,请使用以下条件:

TOP == MAX – 1

要检查队列是否已满,请使用以下条件:

后方== MAX – 1

堆栈不能分为子部分,也没有扩展名。 队列可以分为几个子部分,并具有以下扩展:双端队列,简单队列,优先级队列和循环队列。
用于中缀到后缀的转换,调度算法,深度优先搜索和表达式评估。 队列提供运筹学,运输和计算机科学方面的服务,涉及人员,数据,事件和要存储的对象,以供以后处理。

Comment below if you found anything incorrect or have doubts related to above difference between stack and queue tutorial.

如果发现任何不正确的内容或对堆栈和队列教程之间的上述差异有疑问,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2016/05/difference-between-stack-and-queue.html