[Huawei] if the cloud allows you to share a handwriting stacks and queues, you will write it?

Yesterday CSDN with a friend on a chat, he said that now if he were his own handwriting a stack or queue, it is estimated have to write quite a long time, although usually in use, but others are packaged collection.

Indeed, the classic data structures, including sorting algorithm, although we do not usually handwritten, but these internal strength, as developers must be mastered. Inspired by this, I'm going to look at a series of articles more classic data structures and algorithms. Today, stacks and queues start talking.

These things, when crowded subway, eating the queue, when the bus, can be used to look, or, to put it as a tea bar ~

We know that in the array, if known index data items, you can immediately access the data item, or by order of the search items, access to individual data items in the array. But the different stacks and queues, their access is restricted, that is only one data item can be read at a particular time or deleted. As we all know, after the stack is advanced out of the top of the stack can only access data queues are FIFO, can only access the header data. Not repeat them here.

The main stack mechanism may be implemented using an array, linked list may be implemented, following an array to achieve the basic operation of the stack:

Pushing and popping of data items of both time complexity O (1). That is to say, the time consumed by stack operations does not depend on the number of data items in the stack, and therefore the operation time is short. Stack need to compare and move operations.

Queuing can also use arrays to implement, but here there is a problem, when a full array subscript after can not be added, but the head of the queue in front of the array because the data has been deleted, resulting in air. So we can use recycled queue array to achieve, see the following code:

And stack as the data item in the queue insert and delete data items are the time complexity of O (1).

There is a priority queue, the priority queue and the queue is more specialized than the stack data structure. Compared with ordinary priority queue of queues above, the main difference is in the queue elements are ordered, keyword minimum (or maximum) of the total data items in the first team. Insertion of the data item is inserted into an appropriate position in order to ensure that the order of the queue. Internal priority queue may be used to achieve a particular array or tree - the stack is achieved.

Priority queues implemented here, the insertion operation requires O (N) time, and delete operations will need to O (1) time.

Author: Huawei and so enjoy expert Wu Ni liter

 

欢迎添加华为云小助手微信(微信号:HWCloud002HWCloud003),验证通过后,输入关键字“加群”,加入华为云线上技术讨论群;输入关键字“最新活动”,获取华为云最新特惠促销。华为云诸多技术大咖、特惠活动等你来撩!

Guess you like

Origin www.cnblogs.com/huaweicloud/p/12384616.html