📜  堆栈和数组的区别

📅  最后修改于: 2021-09-11 04:19:41             🧑  作者: Mango

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

堆栈的图解表示如下:

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

Array 的图示如下:

堆栈和数组数据结构的区别:

Stacks Array
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. 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]
Insertion and deletion in stacks takes place only from one end of the list called the top. Insertion and deletion in array can be done at any index in the array.
Stack has a dynamic size. Array has a fixed size.
Stack can contain elements of different data type. Array contains elements of same data type.
We can do only linear search We can do both linear and Binary search