数据结构之动态栈的java简单实现
2018-01-25 09:32
579 查看
最近在看数据结构的书,个人感觉了解数据结构对编程能力的提升还有比较大的帮助的,这里就比较常见的两种数据结构栈和队列的简单实现和大家分享,下面贴出动态栈的简单java实现,实际是以链表的形式进行构建
1、定义数据节点
public class StackNode<T> {
private T x;
private StackNode<T> stackNode;
public StackNode<T> getStackNode() {
return stackNode;
}
public void setStackNode(StackNode<T> stackNode) {
this.stackNode = stackNode;
}
public T getT() {
return this.x;
}
public void setT(T n) {
this.x = n;
}
public StackNode(T name) {
super();
this.x = name;
}
public StackNode() {
super();
}
}
2、定义栈
public class Stack<T> {
private int stactSize = 0;
private StackNode<T> head;
private StackNode<T> bottom;
/**
* 初始化栈
*/
public void init() {
StackNode<T> sn = new StackNode<T>();
this.head = sn;
this.bottom = sn;
this.stactSize = 0;
}
/**
* 压栈
*/
public void push(StackNode<T> node){
node.setStackNode(this.head);
this.head=node;
this.stactSize++;
}
/**
* 出栈
* @return
*/
public StackNode<T> pull() {
if(this.bottom.equals(this.head)) {
System.out.println("当前栈已经到栈底了 "+this.head.getT());
return null;
}else {
StackNode<T> hnode = this.head;
this.head = hnode.getStackNode();
//清空出栈节点指针域,只能通过出栈方发访问栈中数据
hnode.setStackNode(null);
this.stactSize--;
return hnode;
}
}
/**
* 返回栈的有效数据节点长度信息
* @return
*/
public int size() {
return this.stactSize;
}
public Stack() {
init();
}
}
1、定义数据节点
public class StackNode<T> {
private T x;
private StackNode<T> stackNode;
public StackNode<T> getStackNode() {
return stackNode;
}
public void setStackNode(StackNode<T> stackNode) {
this.stackNode = stackNode;
}
public T getT() {
return this.x;
}
public void setT(T n) {
this.x = n;
}
public StackNode(T name) {
super();
this.x = name;
}
public StackNode() {
super();
}
}
2、定义栈
public class Stack<T> {
private int stactSize = 0;
private StackNode<T> head;
private StackNode<T> bottom;
/**
* 初始化栈
*/
public void init() {
StackNode<T> sn = new StackNode<T>();
this.head = sn;
this.bottom = sn;
this.stactSize = 0;
}
/**
* 压栈
*/
public void push(StackNode<T> node){
node.setStackNode(this.head);
this.head=node;
this.stactSize++;
}
/**
* 出栈
* @return
*/
public StackNode<T> pull() {
if(this.bottom.equals(this.head)) {
System.out.println("当前栈已经到栈底了 "+this.head.getT());
return null;
}else {
StackNode<T> hnode = this.head;
this.head = hnode.getStackNode();
//清空出栈节点指针域,只能通过出栈方发访问栈中数据
hnode.setStackNode(null);
this.stactSize--;
return hnode;
}
}
/**
* 返回栈的有效数据节点长度信息
* @return
*/
public int size() {
return this.stactSize;
}
public Stack() {
init();
}
}
相关文章推荐
- 【数据结构】单链表的倒序、删除相同结点、按值排序等简单操作用java实现
- java数据结构之单链表的简单实现
- 数据结构(Java语言)——Stack简单实现
- 数据结构之静态队列的java简单实现
- Android表格布局TableLayout简单实现(Java动态添加,设置边框,删除数据(单行,多行))
- 数据结构与算法——基数排序简单Java实现
- 数据结构(Java语言)——AVLTree简单实现
- 数据结构(java语言描述)-- 栈的两种简单实现
- 数据结构(Java语言)——BinarySearchTree简单实现
- 数据结构(Java语言)——Queue简单实现
- 数据结构书中基于整数的简单排序Java实现,巩固一下基础
- 数据结构(Java语言)——LinkedList简单实现
- Json树形结构数据转Java对象并存储到数据库的实现-超简单的JSON复杂数据处理 .
- 数据结构(Java语言)——BinaryHeap简单实现
- 数据结构(Java语言)——HashTable(分离链接法)简单实现
- Json树形结构数据转Java对象并存储到数据库的实现-超简单的JSON复杂数据处理
- Java动态代理简单实现
- C#:简单实现动态数据生成Word文档并保存
- Java动态代理实现简单AOP
- Java动态显示文件上传进度的简单实现