后续我会把这块补上
栈:https://www.jianshu.com/p/90808ed34b86
队列:https://www.jianshu.com/p/1157aaccad36
列表:https://www.jianshu.com/p/cea9f3be42f5
参考:https://www.cnblogs.com/xingguozhiming/p/9906752.html
队列
队列只能在队尾插入元素,在队首删除元素,就像我们平时排队买票一样~
- enqueue: 队尾插入新元素
- delqueue: 删除队头的元素
- font: 读取队头的元素
- back: 读取队尾的元素
- empty: 判断队列是否为空
- clear: 清空当前队列
- toString: 显示队列所有元素
function Queue() {
this.dataSource = [];
this.enqueue = enqueue;
this.dequeue = dequeue;
this.front = font;
this.back = back;
this.toString = toString;
this.empty = empty;
this.clear = clear;
}
function enqueue(item) {
this.dataSource.push(item)
}
function dequeue() {
return this.dataSource.shift()
}
function front() {
return this.dataSource[0];
}
function back() {
return this.dataSource[this.dataSource.length-1];
}
function clear() {
this.dataSource = [];
}
function empty() {
if (this.dataSource.length == 0) {
return true;
} else {
return false;
}
}
function toString() {
var str = '';
for(var i = 0; i < this.dataSource.length; i++) {
str += this.dataSource[i];
}
return str;
}
栈
- peek:返回栈顶元素
- pop:返回栈顶元素,并删除
- push:压入栈
- clear:清除栈内元素
- length: 栈内元素的个数
function Stack() {
this.dataSource = [];
this.top = 0;
this.push = push;
this.pop = pop;
this.peek = peek;
this.clear = clear;
this.length = length;
}
function push(element) {
this.dataSource[this.top++] = element;
}
// 只返回栈顶元素
function peek() {
return this.dataSource[this.top-1];
}
function pop() {
return this.dataSource[--this.top];
}
function clear() {
this.top = 0;
}
function length() {
return this.top;
}
链表
function Node(element) {
this.element = element;
this.next = null;
}
function LList() {
this.head = new Node("head");
this.find = find;
this.insert = insert;
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)) {
console.log(currNode.next.element);
currNode = currNode.next;
}
}
function find(item) {
var currNode = this.head;
while (currNode.element != item) {
currNode = currNode.next;
}
return currNode;
}
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();
列表
- listSize 列表的元素个数
- pos 列表的当前位置
- length 返回列表中元素的个数
- clear 清空列表列表中所有的元素
- toString 返回列表中以字符串的形式
- getElement 返回当前位置的元素
- insert 在现有元素后插入新元素
- append 在列表的末尾插入新元素
- remove 从列表中删除元素
- front 将列表中的当前位置移动到第一个位置
- end 将列表中的当前位置移动到最后一个位置
- pre 将当前位置往后移一位
- next 将当前位置往前移一位
- hasNext 判断后一位
- hasPre 判断前一位
- currPos 返回列表的当前位置
- moveTo 将当前位置移动到指定位置
- contains 判断给定值是否在列表中
function List() {
this.dataSource = [];
this.listSize = 0;
this.pos = 0;
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.pre = pre;
this.next = next;
this.hasNext = hesNext;
this.hasPre = hasPre;
this.length = lenght;
this.currPos = currPos;
this.moveTo = moveTo;
this.getElement = getElement;
this.contains = contains;
}
function append(element) {
this.dataSource[this.listSize++] = append;
}
function find(element) {
return this.dataSource.indexOf(element);
}
function remove(element) {
var index = this.find(element);
if (index > -1) {
this.dataSource.splice(index, 1);
--this.listSize;
return true;
}
return false;
}
function length() {
return this.listSize;
}
function toString() {
return this.dataSource;
}
function insert(element, after) {
var inserPos = this.find(after);
if (insertPos > -1) {
this.dataSource.splice(inserPos, 0, element);
++this.listSize;
return true;
}
return false;
}
function clear() {
delete this.dataSource;
this.dataSource = [];
this.listSize = this.pos = 0
}
function contain(element) {
return this.dataSource.includes(element);
}
function front() {
this.pos = 0;
}
function end() {
this.pos = this.listSize - 1;
}
function pre() {
--this.pos;
}
function next() {
++this.pos;
}
function currPos() {
return this.pos;
}
function moveTo(positin) {
this.pos = position;
}
function getElement(postion) {
return this.dataSource[postion];
}
function hasNext() {
return this.pos < this.listSize
}
function hasPrev() {
return this.pos >= 0
}