目录
1.栈
1.栈中的方法
push()、pop()、peek()(查看栈顶元素)、isEmpty()、clear()、size()。
2.用js数组实现栈
//类封装栈---用js数组实现栈
var Stack = function(){
var items = [] //定义私有数组,用以存储数据
//入栈
this.push = function(element){ //this.--公有的
items.push(element) //push--数组的方法,向数组中添加元素
}
//出栈
this.pop = function(){
return items.pop() //pop--数组的方法,取出将数组尾部元素
}
//检查栈顶
this.peek = function(){
return items[items.length - 1]
}
//是否为空
this.isEmpty = function(){
return items.length == 0
}
//长度
this.size = function(){
return items.length
}
this.getItems = function(){
return items
}
}
3.递归(自己调用自己)
不停的函数递归,即不停的将函数入栈,如果不出栈,会导致栈溢出。
**栈作用:在编程语言的编译器和内存(计算机的)中保存变量、方法调用。
------理解:在运行嵌套的函数时,编译器或计算机内存会首先将嵌套的函数从外层依次入栈,之后再将函数依次出栈执行。
2.队列
1.使用数组实现队列
/****js数组实现队列******/
var Queue = function(){
var items = []
//入队列
this.enqueue = function(element){
items.push(element)
}
//出队列
this.dequeue = function(){
return items.shift() //shift--删除数组的第一个元素
}
//查看头
this.front = function(){
return items[0]
}
//是否为空
this.isEmpty = function(){
return items.length == 0
}
//长度
this.size = function(){
return items.length
}
this.getItems = function(){
return items
}
}
2.循环队列(实现击鼓传花)
/****循环队列实现击鼓传花******/
var chuanhua = function(){
var q = new Queue();
for(var i=0; i<names.length; i++){
q.enqueue(names[i])
}
while(q.size() > 1){
for(var i = 0; i < number-1; i++){
q.enqueue(q.dequeue())
}
var taotai = q.dequeue()
}
return q.dequeue();
}
//玩家列表
var names = ['a','b','c','d','e','f']
//游戏规则
var number = 3; //当第三个人拿到花时被淘汰掉
3.优先队列
/*****优先队列****/
//将优先级高的数据放到队列前面
var PriorityQueue = function(){
//定义存储队列的数组
var items = [];
//定义存储的数据格式---辅助类
var QueueItem = function(element, priority){
this.element = element;
this.priority = priority;
}
this.enqueue = function(element, priority){
var queueItem = new QueueItem(element, priority);
var added = false;
for(var i=0; i<items.length; i++){
if(queueItem.priority > items[i].priority){
items.splice(i,0,queueItem); //利用数组的splice方法将queueItem插入队列中
added = true; //成功将其插入队列中
break; //如果不break退出的话,会一直循环完整个items
}
}
//如果优先级低于队列中的所有数据,就放到最后
if(!added){
items.push(queueItem)
}
}
this.getItems = function(){
return items
}
}
//测试程序
var pq = new PriorityQueue()
pq.enqueue('小黑',10)
pq.enqueue('小明',12)
3.链表
单向链表、双向链表、双向循环链表。
实现链表类:
//实现链表类
var LinkList = function(){
//链表头
var head = null;
//链表长度
var length = 0;
//辅助: 节点
var Node = function (element){ //next无需手动添加,程序在插入Node时会自动添加next
this.element = element;
this.next = null;
}
//链表尾添加元素
this.append = function(element){
var node = new Node(element); //创建新节点
//node = {
// element: element
// next: null
//}
if(head==null){
head = node;
}else{
var current = head;
while(current.next){
current = current.next;
}
current.next = node;
}
length ++;
}
//链表某一位置添加元素
this.insert = function(position, element){
//越界
if(position > -1 && position < length){
var node = new Node(element);
//在链表头插入节点
if(position == 0){
var current = head;
head = node;
head.next = current;
}
//在链表中插入节点
else{
var index = 0;
var previous = null;
var current = head;
//在某个位置插入元素,需要知道该位置原本的元素current以及前一位置上元素previous
//故需要从头head循环至插入位置position,即index==position
while(index < position){
previous = current;
current = current.next;
index ++;
}
//index == position时,current为要插入位置,previous为前一元素,执行插入
previous.next = node;
node.next = current;
}
length ++;
}
}
//移除链表任意位置元素
this.removeAt = function(position){
if(position > -1 && position < length){
//移除首位元素
if(position == 0){
head = head.next;
}
//移除其他位置元素
else{
var index = 0;
var previous = null;
var current = head;
while(index < position){
previous = current;
current = current.next;
index ++;
}
//执行删除
previous.next = current.next;
}
length --;
return current; //将移除元素返回
}
else{
return null;
}
}
//获取元素位置
this.indexOf = function(element){
var index = 0;
var current = head;
while(index < length){
if(current.element!=element){
current = current.next;
index ++;
}else{
return index;
}
}
return -1;
}
// 获取元素位置
// this.indexOf = function(element){
// var index = 0;
// var current = head;
// while(current){
// if(current.element==element){
// return index;
// }else{
// current = current.next;
// index ++;
// }
// }
// return -1;
// }
//删除某一元素
this.remove = function(element){
return this.removeAt(this.indexOf(element));
//这里不用length--,因为removeAt中已经减过了
}
this.isEmpty = function(){
return length == 0;
}
this.size = function(){
return length;
}
this.getHead = function(){
return head;
}
}
//测试程序
var link = new LinkList;
link.append(1);
link.append(2);
link.append(3);
link.append(4);
link.append(5);
link.getHead();
link.indexOf(3);
link.removeAt(3);
link.remove(4);