1. Stack
The stack is a last in, first out (LastInFirstOut) data structure. It can be achieved by the push and pop operation of the analog array.
Wherein the push and pop algorithm complexity is O (1)
2. Queue
A queue is a first-in first-out data structure (FirstInFIrstOut) data structure. Push and shift operation is essentially an array.
But the complexity of the algorithm is to shift method O (n).
1. Construction of the queue
You can implement your own queue data structure
{Queue class constructor (max = 100 ) { the this .data = the Array (max); the this .p = 0; // enqueue pointer; points to the next data will be inserted location the this .q = 0; // dequeue pointer; current queue a first data location of the this .max = max; // max refers to the maximum capacity of the queue of the this .size = 0; // initial queue is empty } // reading operation of the head PEEK () { const Item = the this .data [ the this .q]; return Item; } // enqueue operation the enqueue (value) { IF ( the this=== .size the this .max) { // queue overflow; can throw an exception or perform other operations the throw new new Error ( "Queue overflow" ) } the this .data [ the this .p ++] = value; the this .size ++ ; IF ( the this .p === the this .max) { the this .p = 0 ; } } // dequeue - dequeued return data dequeue () { IF ( the this .size === 0 ) { // queue reverse overflow: can throw an exception or perform other operations the throw new new Error ( "queue underflow" ) } Const Item = the this .data [ the this .q]; the this .data [ the this .q ++] = undefined; // the value is set to a position corresponding to undefined the this .size-- ; IF ( the this .q === the this . max) { the this .q = 0; // out of range, the start position } return Item; } }
Example:
/ * * Railcar a circular orbit by rearrangement, the order is determined whether the target can be obtained; [1,2,3,4,5] => [1,5,2,3,4] | --4 -3-2-1 --- | temporary track (queue) | | [. 5] [. 4] [. 3] [2] [. 1] ---- |. 5 ---- ------- | --------- [4] [3] [2] [5] [1] corresponding to the array [1,5,2,3,4] * /
Analysis of the actual situation, the arrangement can be FIFO, the queue data structure should be.
function Resort (Original, target) { // value traversed the target queue, if the queue corresponding to the original position, and matching the original data from the shift () This value // If not then the original series data prior to the global value corresponding to stored in the temporary queue. // continue to traverse, the temporary queue, or if the values match the original residual queue and the queue value corresponding to a first position of the target, the Shift () // final result if the temporary queue is empty, it can be proved that the let tempQueue = new new Queue ( original.size); for (target.data of the let X) { IF (X === tempQueue.peek ()) { tempQueue.dequeue (); } the else IF (original.size === 0 ) { return tempQueue. === 0 size } the let Y = null ; the while(original.size > 0 && ((y=original.dequeue()) !== x)) { tempQueue.enqueue(y); } } return tempQueue.size === 0; //如果取值tempQueue.data }
Test Case:
// 测试用例 const arr1 = [1,2,3,4,5]; const arr2 = [1,3,2,4,5]; //true // const arr2 = [5,4,3,2,1]; // false // const arr2 = [1,5,2,3,4]; // true // const arr2 = [1,5,2,4,3]; // false const queue1 = new Queue(arr1.length); const queue2 = new Queue(arr2.length); for(let i=0; i<arr1.length; i++) { queue1.enqueue(arr1[i]) queue2.enqueue(arr2[i]) } console.log(resort(queue1,queue2));
2. Application of the queue
1. Implement the breadth-first search
2. The producer - consumer model
3. The buffer area (such as a keyboard buffer)
4. The priority queue