发布网友 发布时间:2022-05-01 19:49
共2个回答
懂视网 时间:2022-04-22 19:40
本篇文章给大家带来的内容是关于js实现链式栈的代码实例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。官方定义:链式栈是一种数据存储结构,可以通过单链表的方式来实现,使用链式栈的优点在于它能够克服用数组实现的顺序栈空间利用率不高的特点,但是需要为每个栈元素分配额外的指针空间用来存放指针域。
具体的实现
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript"> function LinkStack(){ this.length = 0; this.top = null;//栈顶指针 }; LinkStack.prototype.Node = function(el){ this.element = el; this.next = null; }; //压栈 LinkStack.prototype.push = function(el){ var current, Node = this.Node, node = new Node(el); if(!this.top){ this.top = node; this.length++; return true; }else{ current = this.top; node.next = current; this.top = node; this.length++; return true; } }; //退栈 LinkStack.prototype.pop = function(){ var current = this.top; if(current){ this.top = current.next; current.next = null; this.length--; return current; }else{ throw "error null stack" } }; LinkStack.prototype.toString = function(){ var str = "", current = this.top; while(current){ str += current.element + " "; current = current.next; } return str; }; //清空栈 LinkStack.prototype.clear = function(){ this.top = null; this.length = 0; return true; }; /***************测试代码******************/ function test(){ var linkStack = new LinkStack(); //压栈 for(var i=1;i<21;i++){ linkStack.push(i); } console.log("压栈->" + linkStack.toString()); //退栈 linkStack.pop(); linkStack.pop(); linkStack.pop(); console.log("退栈->" + linkStack.toString()); //清空栈 linkStack.clear(); console.log("清空栈->" + linkStack.toString()); } test(); </script> </head> <body> </body> </html>
相关推荐:
js的代码如何进行压缩?js代码压缩的简单方法
Nodejs中buffer是什么?Nodejs中buffer类的用法
热心网友 时间:2022-04-22 16:48
栈分顺序栈和链式栈,下面程序介绍了顺序栈的实现。 1.数组型
Const
m=栈表目数的上限;
Type
stack=array[1..m] of stype; {栈类型}
Var
s:stack;{栈}
top:integer;
2.记录型
const
m=栈表目数的上限;
type
stack=record
elem: array[1..m] of elemtp;
top:0..m; {栈顶指针}
end;
Var
s:stack;{栈} #include<stdio.h>#include<malloc.h>#defineDataTypeint#defineMAXSIZE1024typedefstruct{DataTypedata[MAXSIZE];inttop;}SeqStack;SeqStack*Init_SeqStack()//栈初始化{SeqStack*s;s=(SeqStack*)malloc(sizeof(SeqStack));if(!s){printf(空间不足\n);returnNULL;}else{s->top=-1;returns;}}intEmpty_SeqStack(SeqStack*s)//判栈空{if(s->top==-1)return1;elsereturn0;}intPush_SeqStack(SeqStack*s,DataTypex)//入栈{if(s->top==MAXSIZE-1)return0;//栈满不能入栈else{s->top++;s->data[s->top]=x;return1;}}intPop_SeqStack(SeqStack*s,DataType*x)//出栈{if(Empty_SeqStack(s))return0;//栈空不能出栈else{*x=s->data[s->top];s->top--;return1;}//栈顶元素存入*x,返回}DataTypeTop_SeqStack(SeqStack*s)//取栈顶元素{if(Empty_SeqStack(s))return0;//栈空elsereturns->data[s->top];}intPrint_SeqStack(SeqStack*s){inti;printf(当前栈中的元素:\n);for(i=s->top;i>=0;i--)printf(%3d,s->data[i]);printf(\n);return0;}intmain(){SeqStack*L;intn,num,m;inti;L=Init_SeqStack();printf(初始化完成\n);printf(栈空:%d\n,Empty_SeqStack(L));printf(请输入入栈元素个数:\n);scanf(%d,&n);printf(请输入要入栈的%d个元素:\n,n);for(i=0;i<n;i++){scanf(%d,&num);Push_SeqStack(L,num);}Print_SeqStack(L);printf(栈顶元素:%d\n,Top_SeqStack(L));printf(请输入要出栈的元素个数(不能超过%d个):\n,n);scanf(%d,&n);printf(依次出栈的%d个元素:\n,n);for(i=0;i<n;i++){Pop_SeqStack(L,&m);printf(%3d,m);}printf(\n);Print_SeqStack(L);printf(栈顶元素:%d\n,Top_SeqStack(L));return0;}定义stack的简单代码:
stack<int> sta;
入栈:sta.push(x);
出栈:sta.pop();
判断栈的大小: sta.size();
判断栈是否为空:sta.empty();