您的位置:首页 > 理论基础 > 数据结构算法

前端学习总结(一)——常见数据结构的javascript实现

2017-03-27 19:06 706 查看

1.列表类

// 列表类
function List() {
this.listSize = 0;  // 列表的元素个数
this.pos = 0;  // 列表的当前位置
this.dataStore = []; // 初始化一个空数组来保存列表元素
this.clear = clear;  // 清空列表中所有元素
this.find = find;  // 查找列表中某一元素
this.toString = toString;  // 返回列表的字符串形式
this.insert = insert;  // 在现有元素后插入新元素
this.append = append;  //在列表的末尾添加新元素
this.remove = remove;  // 从列表中删除元素
this.front = front;  // 将列表的当前位置移动到第一个元素
this.end = end;  // 将列表的当前位置移动到最后一个元素
this.prev = prev;  // 将当前位置前移一位
this.next = next;  // 将当前位置后移一位
this.length = length;  // 返回列表中元素的个数
this.currPos = currPos;  // 返回列表的当前位置
this.moveTo = moveTo;  // 将当前位置移动到指定位置
this.getElement = getElement;  // 返回当前位置的元素
this.length = length;  // 返回列表中元素的个数
this.contains = contains;  // 判断给定元素是否在列表中
}

//在列表的末尾添加新元素
function append(element) {
this.dataStore[this.listSize++] = element;
}
// 查找列表中某一元素
function find(element) {
for (var i = 0; i < this.dataStore.length; ++i) {
if (this.dataStore[i] == element) {
return i;
}
}
return -1;
}
// 从列表中删除元素
function remove(element) {
var foundAt = this.find(element);
if (foundAt > -1) {
this.dataStore.splice(foundAt,1);
--this.listSize;
return true;
}
return false;
}
// 返回列表中元素的个数
function length() {
return this.listSize;
}
// 初始化一个空数组来保存列表元素
function toString() {
return this.dataStore;
}
// 在现有元素后插入新元素
function insert(element, after) {
var insertPos = this.find(after);
if (insertPos > -1) {
this.dataStore.splice(insertPos+1, 0, element);
++this.listSize;
return true;
}
return false;
}
// 清空列表中所有元素
function clear() {
delete this.dataStore;
this.dataStore = [];
this.listSize = this.pos = 0;
}

// 判断给定元素是否在列表中
function contains(element) {
for (var i = 0; i < this.dataStore.length; ++i) {
if (this.dataStore[i] == element) {
return true;
}
}
return false;
}

// 将列表的当前位置移动到第一个元素
function front() {
this.pos = 0;
}
// 将列表的当前位置移动到最后一个元素
function end() {
this.pos = this.listSize-1;
}
// 移到前一个位置
function prev() {
if (this.pos > 0) {
--this.pos;
}
}
// 移到后一个位置
function next() {
if (this.pos < this.listSize-1) {
++this.pos;
}
}
// 返回列表的当前位置
function currPos() {
return this.pos;
}
// 将当前位置移动到指定位置
function moveTo(position) {
this.pos = position;
}
// 返回当前位置的元素
function getElement() {
// return this.dataStore[this.pos];
}

2.栈

// 构造函数
function Stack() {
this.dataStore = []; //数据结构为数组
this.top = 0; // 指向栈顶元素
this.push = push;  // 向栈顶添加一个元素
this.pop = pop;  // 删除栈顶元素
this.peek = peek;  // 返回栈顶元素
this.length = length;  // 返回栈的长度
this.clear = clear;  // 清空栈
}

// 向栈顶添加元素
function push(element) {
this.dataStore[this.top++] = element;
}

// 删除栈顶元素
function pop() {
return this.dataStore[--this.top];
}

// 返回栈顶元素
function peek() {
return this.dataStore[this.top-1];
}

// 返回栈的长度
function length(){
return this.top;
}

// 清空栈
function clear() {
this.top = 0;
}

// 测试代码:
var s = new Stack();
s.push("David");
s.push("Raymond");
s.push("Bryan");
print("length: " + s.length());
print(s.peek());
var popped = s.pop();
print("The popped element is: " + popped);
print(s.peek());
s.push("Cynthia");
print(s.peek());
s.clear();
print("length: " + s.length());
print(s.peek());
s.push("Clayton");
print(s.peek());

// 输出为:
length: 3
Bryan
The popped element is: Bryan
Raymond
Cynthia
length: 0
undefined
Clayton

3.队列

function Queue() {
this.dataStore = []; // 利用数组实现队列
this.enqueue = enqueue;  // 入队
this.dequeue = dequeue;  // 出队
this.front = front;  // 取队首元素
this.back = back;  // 取队尾元素
this.toString = toString;  // 显示队列内的所有元素
this.empty = empty;  // 判断队空
}

// 入队
function enqueue(element) {
this.dataStore.push(element);
}

// 出队
function dequeue() {
return this.dataStore.shift();
}

// 取队首元素
function front() {
return this.dataStore[0];
}

// 取队尾元素
function back() {
return this.dataStore[this.dataStore.length-1];
}

// 显示队列内的所有元素
function queueToString() {
var retStr = "";
for (var i = 0; i < this.dataStore.length; ++i) {
retStr += this.dataStore[i] + "\n";
}
return retStr;
}

// 判断队空
function empty() {
if (this.dataStore.length == 0) {
return true;
}
else {
return false;
}
}

// 测试代码
var q = new Queue();
q.enqueue("Meredith");
q.enqueue("Cynthia");
q.enqueue("Jennifer");
print(q.queueToString());
q.dequeue();
print(q.queueToString());
print("Front of queue: " + q.front());
print("Back of queue: " + q.back());

输出为:
Meredith
Cynthia
Jenniefer
Cynyhia

Front of queue: Cynthia
Back of queue: Jennifer

4.链表

4.1单链表

function Node(element) {
this.element = element;
this.next = null;
}

function LList() {
this.head = new Node("head");
this.find = find;  // 查找给定结点
this.insert = insert;  // 在item后面插入新节点newElement
this.display = display;  // 显示链表中的所有节点
this.findPrevious = findPrevious;  // 查找当前节点的前一个结点
this.remove = remove;  // 删除一个节点
}

// 删除一个节点
function remove(item) {
var prevNode = this.findPrevious(item);
if (!(prevNode.next == null)) {
prevNode.next = prevNode.next.next;
}
}

// 查找当前节点的前一个结点
function findPrevious(item) {
var currNode = this.head;
while (!(currNode.next == null) && (currNode.next.element != item)) {
currNode = currNode.next;
}
return currNode;
}

// 显示链表中的所有节点
function display() {
var currNode = this.head;
while (!(currNode.next == null)) {
print(currNode.next.element);
currNode = currNode.next;
}
}

// 查找给定结点
function find(item) {
var currNode = this.head;
while (currNode.element != item) {
currNode = currNode.next;
}
return currNode;
}

// 在item后面插入新节点newElement
function insert(newElement, item) {
var newNode = new Node(newElement);
var current = this.find(item);
newNode.next = current.next;
current.next = newNode;
}

var cities = new LList();
cities.insert("Conway", "head");
cities.insert("Russellville", "Conway");
cities.insert("Carlisle", "Russellville");
cities.insert("Alma", "Carlisle");
cities.display();
console.log();
cities.remove("Carlisle");
cities.display();

4.2 双向 链表

function Node(element) {
this.element = element;
this.next = null;
this.previous = null;
}

function LList() {
this.head = new Node("head");
this.find = find;  // 查找指定结点
this.insert = insert;  // 在item后面插入一个结点
this.display = display;  // 从头到尾打印链表
this.remove = remove;  // 移除一个结点
this.findLast = findLast;  // 找到链表中的最后一个结点
this.dispReverse = dispReverse;  // 反转双向链表
}

// 反转双向链表
function dispReverse() {
var currNode = this.head;
currNode = this.findLast();
while (!(currNode.previous == null)) {
print(currNode.element);
currNode = currNode.previous;
}
}

// 找到链表中的最后一个结点
function findLast() {
var currNode = this.head;
while (!(currNode.next == null)) {
currNode = currNode.next;
}
return currNode;
}

// 移除一个结点
function remove(item) {
var currNode = this.find(item);
if (!(currNode.next == null)) {
currNode.previous.next = currNode.next;
currNode.next.previous = currNode.previous;
currNode.next = null;
currNode.previous = null;
}
}

//findPrevious 没用了,注释掉
/*function findPrevious(item) {
var currNode = this.head;
while (!(currNode.next == null) && (currNode.next.element != item)) {
currNode = currNode.next;
}
return currNode;
}*/

// 从头到尾打印链表
function display() {
var currNode = this.head;
while (!(currNode.next == null)) {
print(currNode.next.element);
currNode = currNode.next;
}
}

// 查找指定结点
function find(item) {
var currNode = this.head;
while (currNode.element != item) {
currNode = currNode.next;
}
return currNode;
}

// 在item后面插入一个结点
function insert(newElement, item) {
var newNode = new Node(newElement);
var current = this.find(item);
newNode.next = current.next;
newNode.previous = current;
current.next = newNode;
}

//
var cities = new LList();
cities.insert("Conway", "head");
cities.insert("Russellville", "Conway");
cities.insert("Carlisle", "Russellville");
cities.insert("Alma", "Carlisle");
cities.display(); //
print();
cities.remove("Carlisle");
cities.display();  //
print();
cities.dispReverse();  //

//打印结果:
Conway
Russellville
Carlisle
Alma

Conway
Russellville
Alma

Alma
Russellville
Conway
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: