JavaScript 數據結構與算法 (一)

先進後出的數據結構:先進去的數據在底部,最後取出,後進去的數據在頂部,最先被取出。

如下圖所示:

在這裏插入圖片描述

function Stack() {
    this.items = [];
}
Stack.prototype.push = function(element) {	// 入棧
    this.items.push(element);
}
Stack.prototype.pop = function() {			// 出棧
    return this.items.pop();
}
Stack.prototype.peek = function() {			// 查看棧頂
    return this.items[this.items.length - 1];
}
Stack.prototype.isEmpty = function() {		// 判斷棧是否爲空
    return this.items.length === 0;
}
Stack.prototype.size = function () {		// 棧中的個數
    return this.items.length;
}
Stack.prototype.toString = function() {		// 返回棧中的具體數據
    var resultString = '';
    for(var i = 0; i < this.items.length; i++) {
        resultString += this.items[i] + ' ';
    }
    return resultString
}

// 以下代碼測試:
var s = new Stack();
s.push('sdfsdf');
s.push(12)
s.push(100);
s.push(123);
alert(s);
s.pop();
s.pop();
alert(s.peek());

隊列

先進先出的數據結構:特殊之處在於它只允許在隊列的對頭進行刪除操作,而在隊列的隊尾進行插入操作。

在這裏插入圖片描述

基於數組的封裝:

function Queue() {
    this.items = [];
}
Queue.prototype.enqueue = function(element) {	// 入隊
    this.items.push(element);
}
Queue.prototype.dequeue = function() {			// 出隊
    return this.items.shift();
}
Queue.prototype.front = function() {			// 隊頭
    return this.items[0];
}
Queue.prototype.isEmpty = function() {			// 空
    return this.items.length == 0;
}
Queue.prototype.size = function() {				// 長度
    return this.items.length;
}
Queue.prototype.toString = function() {
    var resultString = '';
    for(var i = 0; i < this.items.length; i++) {
        resultString += this.items[i] + ' ';
    }   
    return resultString;
}

// 以下爲測試代碼
var queue = new Queue();
queue.enqueue(12)
queue.enqueue(100)
queue.enqueue(15)
queue.enqueue(20)
queue.enqueue(50)
alert(queue);
queue.dequeue();
alert(queue);
console.log(queue.front());

單向鏈表

單向鏈表每個節點包含當前節點的數據和一個指向下一個節點的引用,末尾的元素的指向null ,但不同於數組,鏈表中的元素在內存中不必是連續的空間

在這裏插入圖片描述

// 輔助類:創建LinkedList中的每一個元素的
var Node = function(data) {
    this.data = data;
    this.next = null;
}

// 單項鍊表
function LinkedList() {
    this.header = null;
    this.length = 0;
}
LinkedList.prototype.append = function(data) {
    var newNode = new Node(data);

    if (this.length === 0 ) {
        this.header = newNode;
    } else {
        var current = this.header;
        // debugger
        while (current.next) {
            current = current.next;
        }
        current.next = newNode;
    }
    this.length += 1;
}
LinkedList.prototype.toString = function() {
    var current = this.header;
    var listString = '';
    while(current) {
        listString += current.data + " ";
        current = current.next;
    }
    return listString.trim();
}
LinkedList.prototype.insert = function(position, data) {
    if(position < 0 && position > this.length) {
        console.error('下標越界');
        current;
    }
    var newNode = new Node(data);
    if (position === 0) {
        newNode.next = this.header;
        this.header = newNode;
    } else {
        var index = 0;
        var current = this.header;
        var previous = null;
        while(index < position) {
            previous = current;
            current = current.next;
            index ++;
        }
        previous.next = newNode;
        newNode.next = current;
    }

    this.length ++;
}
LinkedList.prototype.get = function(position) {
    if (position < 0 || position >= this.length) {
        console.error('下標越界');
        return ;
    }
    var current = this.header;
    var index = 0;
    while (index++ < position) {
        current = current.next;
    }
    return current.data;
}
LinkedList.prototype.indexOf = function(data) {
    var current = this.header;
    var index = 0;
    while(current) {
        if(current.data === data) {
            return index;
        }
        current = current.next;
        index ++;
    }

    console.error('沒有數據');
    return;
}
LinkedList.prototype.update = function(position, element) {
    if (position < 0 || position >= this.length) {
        console.error('下標越界');
        return ;
    }
    var current = this.header;
    var index = 0;
    while (index++ < position) {
        current = current.next;
    }
    current.data = element;
    return element;
}
LinkedList.prototype.removeAt = function(position) {
    if (position < 0 || position >= this.length) {
        console.error('下標越界');
        return;
    }
    var current = this.header;
    if(position === 0 ) {
        this.header = this.header.next;
    } else {
        var index = 0;
        var previous = null;
        while(index ++ < position){
            previous = current;
            current = current.next;
        }
        previous.next = current.next;
    }
    this.length --;
    return current.data;
}
LinkedList.prototype.remove = function(data) {
    var position = this.indexOf(data);
    return this.removeAt(position);
}

// 以下爲測試代碼
var x = new LinkedList();
x.append(12);
x.append(1);
x.append('1');
console.log(x);

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章