堆栈堆栈是一种线性数据结构,其中只能从列表的一侧(称为top)插入和删除元素。堆栈遵循LIFO (后进先出)原则,即,最后插入的元素是第一个出现的元素。将元素插入堆栈的操作称为推入操作,将元素从堆栈中删除的操作称为弹出操作。在堆栈中,我们始终使用名为top的指针来跟踪列表中存在的最后一个元素。
堆栈的图解表示如下:
队列:队列是一种线性数据结构,其中元素只能从列表的一侧(称为后方)插入,而元素只能从列表的另一侧(即前方)删除。队列数据结构遵循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. |
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。