📜  数组,队列和堆栈之间的区别

📅  最后修改于: 2021-05-19 18:33:01             🧑  作者: Mango

大批:

数组是存储在连续内存位置的项目的集合。这个想法是将相同类型的多个项目存储在一起。通过简单地将偏移量添加到基本值(即数组的第一个元素的存储位置(通常由数组的名称表示)),这使得计算每个元素的位置更加容易。
数组的图示如下:

队列:

队列是遵循特定顺序执行操作的线性结构。顺序为先进先出(FIFO) 。队列的一个很好的例子是针对资源的任何使用者队列,其中首先服务于第一位的使用者。堆栈和队列之间的区别在于删除。在堆栈中,我们删除最近添加的项目;在队列中,我们删除了最近最少添加的项目。
队列的图示如下:

堆:

堆栈是一种线性数据结构,其中只能从列表的一侧(称为top)插入和删除元素。堆栈遵循LIFO (后进先出)原则,即,最后插入的元素是第一个出现的元素。将元素插入堆栈的操作称为推入操作,将元素从堆栈中删除的操作称为弹出操作。在堆栈中,我们始终使用名为top的指针来跟踪列表中存在的最后一个元素。
堆栈的图解表示如下:

以下是Array,Stack和Queue之间差异的表格表示形式:

Queues Array Stack
Queues are based on the FIFO principle, i.e., the element inserted at the first, is the first element to come out of the list. In the array the elements belong to indexes, i.e., if you want to get into the fourth element you have to write the variable name with its index or location within the square bracket eg arr[4] Stacks are based on the LIFO principle, i.e., the element inserted at the last, is the first element to come out of the list.
Insertion and deletion in Queues takes place only from rear and front respectively. Insertion and deletion in array can be done at any index in the array. Insertion and deletion in stacks take place only from one end of the list called the top.
Queue has a dynamic and fixed size. Array has a fixed size. Stack has a dynamic and fixed size.
Queue can contain elements of different data type. Array contains elements of same data type. The stack can contain elements of the different data types.
Different types of Queues are circular queue, priority queue, doubly ended queue Different types of Arrays are 1D, 2D, etc Stack has only one type.