列表
内部定义
属性:
- listSize 描述列表的长度
- pos 列表当前的位置
- length() 返回列表的元素的个数
方法:
- clearList() 清空列表
- toString() 返回列表的字符串
- find() 查找列表中的数据
- insert() 在现有元素后面插入元素
- append() 在列表末尾添加元素
- remove() 从列表中删除元素
- front() 将列表的当前位置移动到一个位置
- end() 将列表的当前位置移动到最后位置
- prev() 将当前位置前移一位
- next() 将当前位置后移一位
- currPos() 放回列表当前位置
- moveTo() 移动到指定位置
- contains() 判断给定值是否在列表中
- getElement() 返回当前位置的元素
实现:
function List() {
this.listSize = 0;
this.pos = 0;
this.dataStore = []; // 初始化一个空数组来保存列表元素
}
List.prototype = {
constructor: List,
//向列表添加数据
append: function (element) {
this.dataStore[this.listSize++] = element;
},
//查找列表中的数据
find: function (element) {
for (var i = 0; i < this.dataStore.length; ++i) {
if (this.dataStore[i] == element) {
return i;
}
}
return -1;
},
//从列表删除数据
remove: function (element) {
var foundAt = this.find(element);
if (foundAt > -1) {
this.dataStore.splice(foundAt[i], 1);
--this.listSize;
return true;
}
return false;
},
//列表中有多少个元素
length: function () {
return this.listSize
},
// 显示列表中的元素
toString: function () {
return this.dataStore;
},
//向列表中插入一个元素
insert: function (element, after) {
var insertPos = this.find(after); //find() 方法寻找传入的 after 参数在列表中的位置
if (insertPos > -1) {
this.dataStore.splice(insertPos + 1, 0, element);
++this.listSize;
return true;
}
return false;
},
//清空列表
clearList: function () {
delete this.dataStore;
this.dataStore = [];
this.listSize = this.pos = 0;
},
// 判断给定值是否在列表中
contains: function (element) {
for (var i = 0; i < this.dataStore.length; ++i) {
if (this.dataStore[i] == element) {
return true;
}
}
return false;
},
//将列表的当前位置移动到第一个元素
front: function () {
this.pos = 0;
},
//将列表的当前位置移动到最后一个元素
end: function () {
this.pos = this.listSize - 1;
},
//将当前位置前移一位
prev: function () {
if (this.pos > 0) {
--this.pos;
}
},
//将当前位置后移一位
next: function () {
if (this.pos < this.listSize - 1) {
++this.pos;
}
},
currPos: function () {
return this.pos;
},
//将当前位置移动到指定位置
moveTo: function (position) {
this.pos = position;
},
getElement: function () {
return this.dataStore[this.pos];
}
}
简单验证:
a = new List()
for(let i = 0; i<20; i++){
a.append(i)
}
console.log(a.dataStore) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
console.log(a.find(9)) // 9
console.log(a.length()) //20
console.log(a.pos) //0
a.end()
console.log(a.pos) //19
栈
栈stack是一种特殊的列表,栈内元素只能通过栈顶进行操作。
特点:后入先出(LIFO,last-in-first-out)。
内部定义:
属性:
- dataStore 栈中元素的存储位置
- top() 记录栈元素顶端的位置
方法
- push() 入栈方法
- pop() 出栈方法
- peek() 预览
- clear() 清除
- length() 元素个数
实现:
function Stack() {
this.top = 0;
this.dataStore = [];
}
Stack.prototype = {
constructor: Stack,
//向栈中添加元素
push: function (element) {
this.dataStore[this.top++] = element;
},
//栈顶元素出栈,并返回
pop: function () {
return this.dataStore[--this.top];
},
//返回栈顶元素
peek: function () {
return this.dataStore[this.top - 1];
},
//返回栈的长度
length: function () {
return this.top;
},
//清空栈
clear: function () {
this.top = 0;
this.dataStore = []
}
}
队列
队列是一种列表,不同的是队列只能在队尾插入元素,在队首删除元素。
特点:先进先出(First-In-First-Out,FIFO)
内部定义:
队列的属性
- dataStore:队列的存储
方法
- enqueue 向队尾添加一个元素
- dequeue 删除队首的元素
- front 返回队首元素
- end 返回队尾元素
- length 队列长度
- toString 显示队列
- isEmpty 是否为空
实现:
function Queue() {
this.dataStore = [];
}
Queue.prototype = {
constructor: Queue,
// 向队尾添加一个元素
enqueue: function (element) {
this.dataStore.push(element);
},
// 删除队首的元素
dequeue: function () {
return this.dataStore.shift();
},
// 读取队首的元素
front: function () {
return this.dataStore[0];
},
// 读取队尾的元素:
end: function () {
return this.dataStore[this.dataStore.length - 1];
},
// 队列长度
length: function () {
return this.dataStore.length;
},
// 清空队列
clear: function () {
this.dataStore = [];
},
// 显示队列
toString: function () {
return this.dataStore.map(function (item) {
return item;
}).join(',');
},
// 判断队列是否为空
isEmpty: function () {
return this.dataStore.length <= 0;
}
}
链表
链表:
- 是一组节点的集合
- 每个节点都使用一个对象的引用来指向的后继
- 每个节点的引用叫做链表
特点:链表靠相互之间的关系进行引用,与数组使用位置来指向引用不同
内部定义:
属性
- head:链表的第一个节点
方法
- find 寻找节点
- insert 插入节点
- findPreNode 寻找这个节点的上一个节点
- remove:删除节点
- display 显示全部节点
- findLast 返回最后一个节点
实现:
//链表上的节点
function Node(element) {
this.element = element;
this.next = null;
}
//链表
function LinkedList() {
this.head = new Node("head");
}
LinkedList.prototype = {
constructor: LinkedList,
//寻找节点
find: function (item) {
var currNode = this.head;
while (currNode.element != item) {
currNode = currNode.next;
}
return currNode;
},
//插入节点
insert: function (nNode, item) {
var newNode = new Node(nNode);
var current = this.find(item);
newNode.next = current.next;
current.next = newNode;
},
display: function () {
var headNode = this.head;
var resMsg = "";
while (headNode.next != null) {
resMsg += headNode.element + ",";
headNode = headNode.next;
}
return resMsg;
},
//寻找这个节点的上一个节点
findPreNode: function (item) {
var currNode = this.head;
while (!(currNode.next == null) && (currNode.next.element != item)) {
currNode = currNode.next;
}
return currNode;
},
//删除节点
remove: function (item) {
var pre = this.findPreNode(item);
pre.next = pre.next.next;
},
findLast: function () {
var curNode = this.head;
while (curNode.next != null) {
curNode = curNode.next;
}
return curNode;
}
}