采用数组和节点方式实现的堆栈
来源:互联网 发布:spring boot show sql 编辑:程序博客网 时间:2024/05/16 07:01
基于数组方式的栈的实现:
package com.study.link;public class ArrayStack {private Node[] objectArray;private int topIndex;public ArrayStack(int length){objectArray = new Node[length];}public Node pop(){if(topIndex>0){Node node = objectArray[topIndex-1];topIndex--;return node;}else{return null;}}public void push(Node node){topIndex++;objectArray[topIndex-1]=node;}public Node[] getObjectArray() {return objectArray;}public void setObjectArray(Node[] objectArray) {this.objectArray = objectArray;}public int getTopIndex() {return topIndex;}public void setTopIndex(int topIndex) {this.topIndex = topIndex;}public static void main(String[] args) {ArrayStack stack=new ArrayStack(100); stack.push(new Node(1)); stack.push(new Node(2)); stack.push(new Node(3)); stack.push(new Node(4)); stack.push(new Node(5)); while(stack.getTopIndex()>0) { System.out.println(stack.pop().getValue()); } }}
基于节点方式栈的实现
package com.study.link;/** * 基于节点方式实现的栈 * 先进后出 * @author Administrator * */public class NodeStack {private Node top;//栈顶元素public Node pop(){if(top!=null){Object value = top.getValue();Node temp = new Node(value);top = top.getNext();return temp;}else{return null;}}public void push(Node node){if(node!=null){node.setNext(top);top = node;}else{top = node;}}public static void main(String[] args) {NodeStack stack=new NodeStack(); stack.push(new Node(1)); stack.push(new Node(2)); stack.push(new Node(3)); stack.push(new Node(4)); while(stack.top!=null) { System.out.println(stack.pop().getValue()); } }}节点:
package com.study.link;/** * 链表节点 * 每个链表都有本身值和指向下一个节点的链接 * @author Administrator * */public class Node {private Object value;private Node next;public Node(Object value){this.value = value;next = null;}public Object getValue() {return value;}public void setValue(Object value) {this.value = value;}public Node getNext() {return next;}public void setNext(Node next) {this.next = next;}}
- 采用数组和节点方式实现的堆栈
- 基于数组和节点方式的队列的实现
- 堆栈的数组实现
- 堆栈的数组实现
- 链式堆栈的实现 (带头节点的链式堆栈)
- java中堆栈和队列的实现方式
- 静态数组实现的堆栈
- 动态数组实现的堆栈
- 4.2 堆栈的数组实现
- 基于数组的堆栈实现
- java数据结构-图的实现 采用数组和邻接矩阵
- 堆栈的两种实现形式:数组和链表
- 树的java底层实现 保存子节点方式和保存父节点方式
- 用数组实现堆栈和队列
- 用数组实现堆栈和队列
- Java数组实现堆栈和队列
- 使用数组实现堆栈和队列
- JS数组实现队列和堆栈
- C++的switch语句
- C语言和设计模式(迭代器模式) 【文章都是来自网络,只要自己吸取了就是自己的】
- tomcat7 启动项目报错 java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()
- algorithm 有什么牛B东西?
- Android实现全屏显示的方法
- 采用数组和节点方式实现的堆栈
- C语言和设计模式(外观模式) 【文章都是来自网络,只要自己吸取了就是自己的】
- 标准库全排列next_permutation()
- css
- s3c-nand: ECC uncorrectable error detected
- Matlab中mex与C混合编程总结
- Git - SVN Crash Course
- hibernate.cfg.xml
- vector C++ 详细用法