[Data Structure and Algorithm] Learning Record Collection (03) Stack and Queue

  • This collection is the study record of the "C++ Data Structure and Algorithm" course that the author self-study on the b station. It aims to record the important learning points, thinking content and part of the code, so that it can be read by yourself later, and it can also bring some information to other readers. refer to
  • The content is based on the author’s own understanding or perception, and may contain inappropriate or incorrect contents.
  • System environment: Win10, Visual Studio 2019
  • The picture refers to the online course lecture notes of Northeastern University's "Data Structure and Algorithm Design" (2020)

Table of contents

1 stack

Array implementation

two-way stack

Linked list implementation

Stack in STL

Applications

2 queue 

Array implementation

Linked list implementation

Queues in STL


1 stack

A "restricted" linear data structure that follows the "first in, last out" rule. Commonly used instructions include IsEmpty, IsFull, top, Push, Pop, displaystack and other instructions.

Array implementation

 The linked list implementation below is more commonly used and has balanced overall performance.

two-way stack

 The structure is relatively complex. It was designed to save memory in the early days of computers and is rarely used now.

Linked list implementation

 Also called a chain stack, the tail of the chain is usually designed to be at the bottom of the stack, otherwise it will cost O(n) during the deletion operation, and the operation of moving a large number of elements will consume performance. Generally, a single linked list without a head node is used, which has a certain impact on dynamic memory. The disadvantage of scheduling is higher consumption, so it is less commonly used than array stacks.

Stack in STL

Remember to add the header file:

#include <stack>

It is an adapter class based on deque. It changes the calling interface of its base class. Please note that the pop operation has no return value and the content of the deleted element cannot be known. If you want to know, you need to query before deleting. 

Applications

Can be applied to symbol matching (checking compilation syntax), expression operations (Postfix/Infix Expressions)


2 queue 

Similar to the fate of a stack, it is also a "restricted" linear list whose elements follow the "first in, first out" rule.

Insert ( Insert ) is performed at the rear of the queue ( rear ) , and its operation behavior is called (enqueue) Enqueue

Delete ( delete ) at the head of the queue ( front ) , and its operation behavior is called (dequeue) Dequeue

Array implementation

 A better method is to use circular array, which can avoid the performance overhead of frequent addition and deletion operations in Array without wasting space. The following figure is a poor solution.

It takes about 0(n) to dequeue, which is very inefficient.

 If a better method is used to perform the dequeue operation by moving the head of the queue, although dequeueing only costs O(1), it may cause a waste of the first part of the space.

 Finally, the circular array method is adopted and a storage space is sacrificed to realize the ability to distinguish between empty and full queues (if not sacrificed, it is impossible to determine whether the queue is empty or full by judging whether front is equal to rear, because when empty and full, the two pointers overlap) , this method not only avoids the performance overhead of frequently deleted mobile elements, but also saves space.

Linked list implementation

 To record the head and tail pointers to complete insertion and deletion operations, you can set up a conter to continuously track the size of the list, which may be better than the Array implementation.

Queues in STL

It is an adapter class based on a double-ended queue. Remember to:

#include <queue>

The End 

Guess you like

Origin blog.csdn.net/Norths_/article/details/125530441