var stack = [];stack.push(2); // stack is now [2]stack.push(5); // stack is now [2, 5]var i = stack.pop(); // stack is now [2]alert(i); // displays 5
var queue = [];queue.push(2); // queue is now [2]queue.push(5); // queue is now [2, 5]var i = queue.shift(); // queue is now [5]alert(i); // displays 2
var temp_stack = new Array();var stack = new Array();
temp_stack.push(1);temp_stack.push(2);temp_stack.push(3);
如果我现在弹出元素,那么输出将是3,2,1。但是我们想要FIFO结构,所以你可以做以下事情。
stack.push(temp_stack.pop());stack.push(temp_stack.pop());stack.push(temp_stack.pop());
stack.pop(); //Pop out 1stack.pop(); //Pop out 2stack.pop(); //Pop out 3
//classic stackvar stack = [];stack.push("first"); // push inserts at the endstack.push("second");stack.push("last");stack.pop(); //pop takes the "last" element
//One way to implement queue is to insert elements in the oposite sense than a stackvar queue = [];queue.unshift("first"); //unshift inserts at the beginningqueue.unshift("second");queue.unshift("last");queue.pop(); //"first"
//other way to do queues is to take the elements in the oposite sense than stackvar queue = [];queue.push("first"); //push, as in the stack inserts at the endqueue.push("second");queue.push("last");queue.shift(); //but shift takes the "first" element
// QUEUE Object Definition
var Queue = function() {this.first = null;this.last = null;this.size = 0;};
var Node = function(data) {this.data = data;this.next = null;};
Queue.prototype.enqueue = function(data) {var node = new Node(data);
if (!this.first){ // for empty list first and last are the samethis.first = node;this.last = node;} else { // otherwise we stick it on the endthis.last.next=node;this.last=node;}
this.size += 1;return node;};
Queue.prototype.dequeue = function() {if (!this.first) //check for empty listreturn null;
temp = this.first; // grab top of listif (this.first==this.last) {this.last=null; // when we need to pop the last one}this.first = this.first.next; // move top of list downthis.size -= 1;return temp;};
var x = 10;var y = 11;var Queue = new Array();Queue.unshift(x);Queue.unshift(y);
console.log(Queue)// Output [11, 10]
Queue.pop()console.log(Queue)// Output [11]
class Stack {constructor(...items){this._items = []
if(items.length>0)items.forEach(item => this._items.push(item) )
}
push(...items){//push item to the stackitems.forEach(item => this._items.push(item) )return this._items;
}
pop(count=0){//pull out the topmost item (last item) from stackif(count===0)return this._items.pop()elsereturn this._items.splice( -count, count )}
peek(){// see what's the last item in stackreturn this._items[this._items.length-1]}
size(){//no. of items in stackreturn this._items.length}
isEmpty(){// return whether the stack is empty or notreturn this._items.length==0}
toArray(){return this._items;}}
class Queue{constructor(...items){//initialize the items in queuethis._items = []// enqueuing the items passed to the constructorthis.enqueue(...items)}
enqueue(...items){//push items into the queueitems.forEach( item => this._items.push(item) )return this._items;}
dequeue(count=1){//pull out the first item from the queuethis._items.splice(0,count);return this._items;}
peek(){//peek at the first item from the queuereturn this._items[0]}
size(){//get the length of queuereturn this._items.length}
isEmpty(){//find whether the queue is empty or noreturn this._items.length===0}}
/*** A Typescript implementation of a queue.*/export default class Queue {
private queue = [];private offset = 0;
constructor(array = []) {// Init the queue using the contents of the arrayfor (const item of array) {this.enqueue(item);}}
/*** @returns {number} the length of the queue.*/public getLength(): number {return (this.queue.length - this.offset);}
/*** @returns {boolean} true if the queue is empty, and false otherwise.*/public isEmpty(): boolean {return (this.queue.length === 0);}
/*** Enqueues the specified item.** @param item - the item to enqueue*/public enqueue(item) {this.queue.push(item);}
/*** Dequeues an item and returns it. If the queue is empty, the value* {@code null} is returned.** @returns {any}*/public dequeue(): any {// if the queue is empty, return immediatelyif (this.queue.length === 0) {return null;}
// store the item at the front of the queueconst item = this.queue[this.offset];
// increment the offset and remove the free space if necessaryif (++this.offset * 2 >= this.queue.length) {this.queue = this.queue.slice(this.offset);this.offset = 0;}
// return the dequeued itemreturn item;};
/*** Returns the item at the front of the queue (without dequeuing it).* If the queue is empty then {@code null} is returned.** @returns {any}*/public peek(): any {return (this.queue.length > 0 ? this.queue[this.offset] : null);}
}
/** Queue contains a linked list of Subqueue */class Subqueue <T> {public full() {return this.array.length >= 1000;}
public get size() {return this.array.length - this.index;}
public peek(): T {return this.array[this.index];}
public last(): T {return this.array[this.array.length-1];}
public dequeue(): T {return this.array[this.index++];}
public enqueue(elem: T) {this.array.push(elem);}
private index: number = 0;private array: T [] = [];
public next: Subqueue<T> = null;}
这里是主Queue类:
class Queue<T> {get length() {return this._size;}
public push(...elems: T[]) {for (let elem of elems) {if (this.bottom.full()) {this.bottom = this.bottom.next = new Subqueue<T>();}this.bottom.enqueue(elem);}
this._size += elems.length;}
public shift(): T {if (this._size === 0) {return undefined;}
const val = this.top.dequeue();this._size--;if (this._size > 0 && this.top.size === 0 && this.top.full()) {// Discard current subqueue and point top to the one afterthis.top = this.top.next;}return val;}
public peek(): T {return this.top.peek();}
public last(): T {return this.bottom.last();}
public clear() {this.bottom = this.top = new Subqueue();this._size = 0;}
private top: Subqueue<T> = new Subqueue();private bottom: Subqueue<T> = this.top;private _size: number = 0;}
let stack = [];stack.push(1);//[1]stack.push(2);//[1,2]stack.push(3);//[1,2,3]
console.log('It was inserted 1,2,3 in stack:', ...stack);
stack.pop(); //[1,2]console.log('Item 3 was removed:', ...stack);
stack.pop(); //[1]console.log('Item 2 was removed:', ...stack);
let queue = [];queue.push(1);//[1]queue.push(2);//[1,2]queue.push(3);//[1,2,3]
console.log('It was inserted 1,2,3 in queue:', ...queue);
queue.shift();// [2,3]console.log('Item 1 was removed:', ...queue);
queue.shift();// [3]console.log('Item 2 was removed:', ...queue);
class Queue extends Array {constructor(){super()Object.defineProperty(this,"head",{ value : 0, writable: true});}enqueue(x) {this.push(x);return this;}dequeue() {var first;return this.head < this.length ? ( first = this[this.head], delete this[this.head++], first): void 0; // perfect undefined}peek() {return this[this.head];}}
var q = new Queue();console.log(q.dequeue()); // doesn't breakconsole.log(q.enqueue(10)); // add 10console.log(q.enqueue("DIO")); // add "DIO" (Last In Line cCc R.J.DIO reis cCc)console.log(q); // display qconsole.log(q.dequeue()); // lets get the first one in the lineconsole.log(q.dequeue()); // lets get DIO out from the line