java数据结构--stack
2011-08-12 19:28
429 查看
下面介绍三种实现stack的方法。尽管java中有stack类,但是已经被废弃了。自己动手实现!!
1. 用链表实现
2. 用数组实现
3. 用vector实现
1. 用链表实现
package com.jzm.stackQueueTree; public class LinkedStack<T> { private Node topNode; //引用链表中的第一个节点 public LinkedStack(){ clear(); }//end default constructor private class Node{ private T data; //栈的元素 private Node next; //指向下一个节点的连接 private Node(T data){ this.data = data; next = null; }//end constructor private T getData(){ return data; } private Node getNextNode(){ return next; } } private boolean isEmpty(){ return topNode == null; } private void clear(){ topNode = null; } public void push(T newEntry){ Node newNode = new Node(newEntry); newNode.next = topNode; topNode = newNode; } public T pop(){ T top = null; if(!isEmpty()){ top = topNode.getData(); topNode = topNode.getNextNode(); } return top; } public void display(){ Node point = topNode; System.out.println("从顶端元素开始:"); while(point != null){ System.out.println(point.getData()); point = point.next; } } public static void main(String[] args) { LinkedStack<Integer> linkedStack = new LinkedStack<Integer>(); for (int i = 0; i < 100; i++){ linkedStack.push(i); if (i %2 ==0) { linkedStack.pop(); } } linkedStack.display(); } }
2. 用数组实现
package com.jzm.stackQueueTree; public class ArrayStack<T> { private T[] a; //用于存放值的数组 private int topIndex; //栈的顶端索引 private final static int MAX = 500; //默认栈大小 public ArrayStack(){ clear(); a = (T[]) new Object [MAX]; } public ArrayStack(int size){ //自定义栈大小 clear(); a = (T[])new Object[size]; } private boolean isFull(){ return (topIndex >= a.length-1); } public void clear(){ //清空栈 topIndex = -1; a = null; } public void push(T newEntry){ if (!isFull()) { a[++topIndex] = newEntry; }else { System.out.println("栈满了 topIndex="+topIndex); } } public T pop(){ T top = null; if(topIndex >= 0){ top = a[topIndex]; topIndex--; }else{ System.out.println("栈为空"); } return top; } public void display(){ System.out.println("ArrayStack从顶端元素开始:"); for(int i=topIndex; i>=0; i--){ System.out.println(a[i]); } } public static void main(String[] args) { ArrayStack<Integer> arrayStack = new ArrayStack<Integer>(); for (int i = 0; i < 100; i++){ arrayStack.push(i); if (i %2 == 0){ arrayStack.pop(); } } //end for arrayStack.display(); } }
3. 用vector实现
package com.jzm.stackQueueTree; import java.util.Vector; public class VectorStack<T>{ private Vector <T> stack; public VectorStack (){ stack = new Vector<T>(); } public VectorStack (int maxsize){ stack = new Vector<T>(maxsize); } public void push(T newEntry){ stack.addElement(newEntry); } public T pop(){ T top = null; if (!isEmpty()) { top = stack.lastElement(); stack.removeElementAt(stack.size()-1); } return top; } private boolean isEmpty(){ return stack.isEmpty(); } public void display(){ System.out.println("vectorStack从顶端元素开始:"); for (int i = stack.size()-1; i>=0; i--) { System.out.println(stack.elementAt(i)); } } public static void main(String[] args) { VectorStack<Integer> vectorStack = new VectorStack<Integer>(); for (int i = 0; i < 100; i++){ vectorStack.push(i); if (i %2 == 0){ vectorStack.pop(); } } //end for vectorStack.display(); } }
相关文章推荐
- Java中各种常用数据结构的使用(Vector、Stack、LinkedList)
- java数据结构——5栈(Stack)
- 共同学习Java源代码-数据结构-Stack
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)
- Java数据结构----栈(Stack)源码分析和用链表简单实现
- Java 数据结构 --> Stack 类
- 数据结构习作之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java) (技术含量少许)
- java实现的stack数据结构
- Java数据结构 老版本的数据结构Vector、Stack
- 用java源代码学数据结构<五>: Stack 详解
- 数据结构(Java语言)——Stack简单实现
- Java数据结构——Stack和父类Vector
- Java 数据结构之Stack
- java数据结构之堆栈Stack
- JAVA中的数据结构——集合类(线性表:Vector、Stack、LinkedList、set接口;键值对:Hashtable、Map接口<HashMap类、TreeMap类>)
- C++程序员学Java系列之三一:数据结构之Stack(栈)
- 【Java数据结构学习笔记之二】Java数据结构与算法之栈(Stack)实现
- JAVA vector Stack数据结构简单介绍
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java) (转载)
- Java 数据结构stack、queue的常用类