温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在JavaScript中查找栈和队列

发布时间:2021-06-02 17:10:35 来源:亿速云 阅读:179 作者:Leah 栏目:web开发

这期内容当中小编将会给大家带来有关怎么在JavaScript中查找栈和队列,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

一、栈的介绍

栈就是和列表类似的一种数据结构,数据只能在栈顶添加或者删除。栈是一种特殊的列表,栈内的元素只能通过列表的一端访问,成为栈顶。栈具有后进先出的特点,所以任何不在栈顶的元素都无法访问。

后进先出(LIFO,last-in-first-out)的数据结构。

对栈的操作

1.对栈的两种主要操作为将一个元素压入栈和将一个元素弹出栈。

入栈:push();
出栈:pop();

2.预览栈顶的元素peek();

pop()虽然可以访问栈顶元素,但调用后,栈顶元素也从栈中永久性的被删除。peek()方法只返回栈顶元素,并不删除它。

对栈的实现

定义stack类的构造函数:

function Stack(){
 this.dataStore=[];//数组dataStore保存栈内元素,初始化为空数组
 this.top=0;
 //top为栈顶位置,被构造函数初始化为0,表示栈顶对应数组的起始位置0
 this.push=push;
 this.pop=pop;
 this.peek=peek;
}

实现push()方法:

function push(element){
 this.dataStore[this.top++]=element;
}

实现pop()方法:

function pop(element){
 return this.dataStore[--this.top];
 //pop方法与push方法相反,它返回栈顶元素,同时将变量top的值减1
}

实现peek()方法:

function peek(element){
 return this.dataStore[this.top-1];
 //peek方法返回数组的第top-1个位置的元素,即栈顶元素。
}

如果对一个空栈调用peek()方法,结果为undefined,因为栈是空的,栈顶没有任何元素。

实现length()

需要知道栈内存储了多少元素,length()方法通过返回变量top值得方法返回栈内的元素个数。

function length(){
 return this.top();
}

实现clear()

clear()将变量top的值设置为0,清空一个栈:

function clear(){
 this.top=0;
}

总结:Stack类

function stack(){
 this.dataStore=[];
 this.top=0;
 this.push=push;
 this.pop=pop;
 this.peek=peek;
 this.clear=clear;
 this.length=length;
}
function push(element){
 this.dataStore[this.top++]=element;
}
function peek(){
 return this.dataStore[this.top-1];
}
function pop(){
 return this.dataStore[--this.top];
}
function clear(){
 this.top=0;
}
function length(){
 return this.top;
}

二、队列

队列是一种列表,队列智能在队尾插入元素,在队首删除元素。队列用于存储按顺序排列的数据,先进先出。

对队列的操作

队列主要两种操作,入队和出队,入队是在队尾插入新元素,出队是删除队首的元素。另一种是读取队头的元素,peek()

push()在数组末尾添加元素

names=[];
names.push("hling");
names.push("aling");
print(names); //显示hling,aling

shift()删除数组中第一个元素

names.shift();
print(names); //显示aling

定义Queue

function Queue(){
 this.dataStore=[];
 this.enqueue=enqueue;
 this.dequeue=dequeue;
 this.front=front;
 this.back=back;
 this.toString=toString;
 this.empty=empty;
}

enqueue()向队尾添加一个元素

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

dequeue()向队尾添加一个元素

function dequeue(element){
 return this.dataStore.shift(element);
}

读取队首和队尾的元素

function front(){
 return this.dataStore[0];
}
function back(){
 return this.dataStore[this.dataStore.length-1];
}

toString()显示队列内的所有元素

function toString(){
 var retStr="";
 for(var i=0;i<this.dataStore.length;i++){
  retStr+=this.dataStore[i]+"\n";
 }
 return retStr;

empty()方法盘对队列是否为空

function empty(){
 if(this.dataStore.length==0){
  return true;
 }else{
  return false;
 }
}

**Queue队列的类

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(element){
 return this.dataStore.shift(element);
}
function front(){
 return this.dataStore[0];
}
function back(){
 return this.dataStore[this.dataStore.length-1];
}
function toString(){
 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;
 }
}

上述就是小编为大家分享的怎么在JavaScript中查找栈和队列了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI