堆栈堆栈是一种线性数据结构,其中元素只能从列表的一侧插入和删除,称为顶部。堆栈遵循LIFO (后进先出)原则,即最后插入的元素是第一个出来的元素。将元素插入堆栈称为推入操作,从堆栈中删除元素称为弹出操作。在堆栈中,我们始终使用名为 top的指针跟踪列表中存在的最后一个元素。
堆栈的图解表示如下:
队列:队列是一种线性数据结构,其中元素只能从列表的一侧插入,称为back ,元素只能从称为front的另一侧删除。队列数据结构遵循FIFO (先进先出)原则,即最先插入列表的元素,是最先从列表中删除的元素。将元素插入队列称为入队操作,删除元素称为出队操作。在队列中,我们总是维护两个指针,一个指向在第一个插入并仍然存在于列表中的元素的前指针,第二个指针指向最后插入的元素和后指针。
队列的图解表示如下:
堆栈和队列数据结构之间的区别
Stacks | Queues |
---|---|
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. | 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. |
Insertion and deletion in stacks takes place only from one end of the list called the top. | Insertion and deletion in queues takes place from the opposite ends of the list. The insertion takes place at the rear of the list and the deletion takes place from the front of the list. |
Insert operation is called push operation. | Insert operation is called enqueue operation. |
Delete operation is called pop operation. | Delete operation is called dequeue operation. |
In stacks we maintain only one pointer to access the list, called the top, which always points to the last element present in the list. | In queues we maintain two pointers to access the list. The front pointer always points to the first element inserted in the list and is still present, and the rear pointer always points to the last inserted element. |
Stack is used in solving problems works on recursion. | Queue is used in solving problems having sequential processing. |
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。