java数据结构(二)——栈
来源:互联网 发布:金仕达期货交易软件 编辑:程序博客网 时间:2024/06/03 18:51
在程序设计中,大家一定接触过“堆栈”,其实堆和栈是两个完全不同的概念,栈是一种特殊的数据结构。在中断处理中常用来保护现场。
在栈的结构中,只能在栈的一端进行数据操作,这一端成为栈顶,另一端成为栈底。即:数据的存取只能在栈顶进行。从数据运算的角度来分析,栈结构式按照“后进先出”(Last In Firt OUT,LIFO)的原则处理结点的。
1.数据准备部分
<h3><pre name="code" class="java"><span style="font-weight: normal;"><span style="font-size:14px;">//数据</span></span>
<span style="font-weight: normal;"><span style="font-size:14px;">class DATA{ String name; int age;}//结构class StackType{static final int MAXLEN=50; DATA3[] data=new DATA3[MAXLEN+1]; //数据元素 int top; //栈顶 </span></span>
<span style="font-family: Arial, Helvetica, sans-serif; font-weight: normal;"><span style="font-size:10px;"> }</span></span>
2.初始化栈
<span style="font-weight: normal;"> StackType STInit() { StackType p; if((p=new StackType())!=null) //申请栈内存 { p.top=0; //设置栈顶为0 return p;//返回指向栈的指针 } return null; }</span>
3.判断栈空/栈满
<span style="font-weight: normal;"> boolean STIsEmpty(StackType s) //判断栈是否为空 { boolean t; t=(s.top==0); return t; }</span>
<span style="font-weight: normal;"> boolean STIsFull(StackType s) //判断栈是否已满 { boolean t; t=(s.top==MAXLEN); return t; }</span>
4.清空栈/释放空间
<span style="font-weight: normal;"> void STClear(StackType s) //清空栈 { s.top=0; }</span>
<span style="font-weight: normal;"> void STFree(StackType s) //释放栈所占用空间 { if(s!=null) { s=null; } }</span>
5.入栈/出栈
<span style="font-weight: normal;"> int PushST(StackType s,DATA3 data)//入栈操作 { if((s.top+1)>MAXLEN) { System.out.print("栈溢出!\n"); return 0; } s.data[++s.top]=data;//将元素入栈 return 1; }</span>
<span style="font-weight: normal;"> DATA PopST(StackType s) //出栈操作 { if(s.top==0) { System.out.print("栈为空!\n");// System.exit(0); } return (s.data[s.top--]); }</span>
6.读取结点数据
<span style="font-weight: normal;"> DATA PeekST(StackType s) //读栈顶数据 { if(s.top==0) { System.out.printf("栈为空!\n");// System.exit(0); } return (s.data[s.top]); }}</span>
以上就是数据结构中的栈。
0 0
- java数据结构(二)——栈
- 【Java】Java数据结构和算法(二)——栈和队列
- Java数据结构与算法解析(二)——栈
- Java数据结构与算法解析(二)——栈
- java核心数据结构(二)——Map类族
- 数据结构 JAVA描述(二) 栈
- 数据结构复习(二)—— 栈
- C++ 数据结构(二)——栈
- 数据结构—链表(二)
- 数据结构——二
- 数据结构(二)——栈及实现、括号匹配
- 【数据结构】——-栈、队列和数组(二)
- 数据结构与算法(11)——栈习题二
- 学习JavaScript数据结构与算法(二)——栈
- Java数据结构和算法(二)——数组
- Java数据结构和算法(二)——数组
- Java数据结构和算法(二)——数组
- JAVA数据结构和算法(二)—线性查找
- 二进制、八进制、十进制、十六进制之间转换
- 打开或者关闭wifi
- 山东多校联合模拟赛 Day2
- 在陌生Linux环境查看Tomcat服务的方法
- 传智播客说:大学生们,先让自己值钱,再赚钱!
- java数据结构(二)——栈
- CF 675ETrains and Statistic(线段树 + Dp)
- Face Recognizer
- Foundation库下的NSString,NSArray, NSDictionary……可以被继承吗
- react-native 把图片转化base64字符串
- Unity 手势缩放UI
- windows linux共享
- VC读取文件的文件版本号
- cell自适应高度