js数据结构与算法 第一天(栈,队列,优先级队列)

什么是数据结构

数据结构就是计算机中存储和组织数据的方式。
就像是图书馆中大量的书,我们不仅仅要把书放入,还要方便取出。

常见的数据结构

  • 数组(Array)
  • 栈(Stack)
  • 链表(Linked List)
  • 图(Graph)
  • 散列表(Hash)
  • 队列(Queue)
  • 树(Tree)
  • 堆(Heap)

数组存放在栈中,链表存放在堆中

什么是算法(Algorithm)

(解决办法的办法/步骤逻辑)
一个有限指令集,每条指令的描述不依赖于语言;
接受一些输入(有些情况下不需要输入);
产生输出;
一定在有限步骤之后终止。

数组(线性结构)

js数组就是API的调用
数组是一种线性结构,并且在数组的任意位置可以插入和删除数组

栈(线性结构)

栈结构示意图:
在这里插入图片描述
栈是一种受限的线性表,后进先出(LIFO-last in first out)。
函数调用栈
A调用B,B调用C,C调用D。依次将A,B,C,D压入栈,D执行完后,按D,C,B,A的顺序出栈
实现栈结构有两种比较常见的方式:
基于数组实现;
基于链表实现。
用数组实现栈的封装

//封装栈类
function Stack(){
	//栈中的属性 定义一个数组来存储
	this.items=[];
	//栈的操作
	//1.将元素压入栈
	Stack.prototype.push=function(element){
		this.items.push(element);
	}
	//2.从栈中提出元素
	Stack.prototype.pop=function(){
		 return this.items.pop();
	}
	//3.查看栈顶元素
	Stack.prototype.peak=function(){
		return this.items[this.items.length-1];
	}
	//4.判断栈是否为空
	Stack.prototype.isEmpty=function(){
		return this.items.length==0;
	}
	//5.获取栈中元素的个数
	Stack.prototype.size=function(){
		return this.items.length;
	}
	//6.toString方法
	Stack.prototype.toString=function(){
		var resultString='';
		for(var i=0;i<this.items.length;i++){
			resultString+=this.items[i]+' ';
		}
		return resultString;
	}
}

应用-十进制转二进制

//十进制转二进制
function dectoBin(num){
	var stack=new Stack();
	while(num>0){
		stack.push(num%2);
		num=Math.floor(num/2);
	}
	
	var binStr='';
	while(!stack.isEmpty()){
		binStr+=stack.pop();
	}
	return binStr;
}

Math. floor()方法是向下取整,Math. ceil()方法是向上取整
还有一个应用-中缀表达式转换成后缀表达式

队列(线性结构)

先进先出(FIFO-first in first out)
队列结构示意图:
在这里插入图片描述
队列的实现:
基于数组实现;
基于链表实现
用数组实现队列的封装

function Queue(){
	//用数组存储
	this.items=[];
	//操作
	//1.向尾部添加一个新项
	Queue.prototype.enqueue=function(el){
		this.items.push(el);
	}
	//2.移除队列的第一项,并返回被移除的元素
	Queue.prototype.dequeue=function(){
		return this.items.shift();
	}
	//3.查看前端元素
	Queue.prototype.front=function(){
		return this.items[0];
	}
	//4.查看队列是否为空
	Queue.prototype.isEmpty=function(){
		return this.items.length==0;
	}
	//5.查看队列中元素的个数
	Queue.prototype.size=function(){
		return this.items.length;
	}
	//6.toString()方法
	Queue.prototype.toString=function(){
		var resultString='';
		for(var i=0;i<this.items.length;i++){
			resultString+=this.items[i]+' ';
		}
		return resultString;
	}
}

应用-击鼓传花

//击鼓传花
function passGame(namelist,num){
	var queue=new Queue();
	//将所有人加入队列中
	for(var i=0;i<namelist.length;i++){
		queue.enqueue(namelist[i]);
	}
	//开始数数 不是num则将其放到队列末尾 是的话则将其删除
	while(queue.size()>1){
		for(var i=0;i<num-1;i++){
			queue.enqueue(queue.dequeue());
		}
		queue.dequeue();
	}
	//获取剩下的那个人
	alert('剩下的人数'+queue.size());
	console.log(queue.front());
	var endname=queue.front();
	alert('剩下的那个人为'+endname);
	alert('下标值为'+namelist.indexOf(queue.front()));
	return namelist.indexOf(endname);
}

优先级队列

每个元素不再只是一个数据,而且包含数据的优先级;
在添加方式中,根据优先级放入正确位置
优先级队列的封装

//优先级队列的封装
function PriorityQueue(){
	//创建一个对象
	function QueueElement(element,priority){
		this.element=element;
		this.priority=priority;
	}
	//属性
	this.items=[];
	//操作
	PriorityQueue.prototype.enqueue=function(el,priority){
		//创建PriorityQueue对象
		var queueElement=new QueueElement(el,priority);
		//判断队列是否为空
		if(this.items.length==0){
			this.items.push(queueElement);
		}
		else{
			var added=false;
			for(var i=0;i<this.items.length;i++){
				if(queueElement.priority<this.items[i].priority){
					this.items.splice(i,0,queueElement);
					added=true;
					break;
				}
			}
			if (!added) {
				this.items.push(queueElement);
			}
		}
	}
	//2.移除队列的第一项,并返回被移除的元素
	PriorityQueue.prototype.dequeue=function(){
		return this.items.shift();
	}
	//3.查看前端元素
	PriorityQueue.prototype.front=function(){
		return this.items[0];
	}
	//4.查看队列是否为空
	PriorityQueue.prototype.isEmpty=function(){
		return this.items.length==0;
	}
	//5.查看队列中元素的个数
	PriorityQueue.prototype.size=function(){
		return this.items.length;
	}
	//6.toString()方法
	PriorityQueue.prototype.toString=function(){
		var resultString='';
		for(var i=0;i<this.items.length;i++){
			resultString+=this.items[i].element+'-'+this.items[i].priority+' ';
		}
		return resultString;
	}
}

主要注意优先级队列的数据插入方法,里面定义了一个对象,用来存储每个数据的值和优先级

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值