您的位置:首页 > 理论基础 > 数据结构算法

java数据结构--stack

2011-08-12 19:28 429 查看
下面介绍三种实现stack的方法。尽管java中有stack类,但是已经被废弃了。自己动手实现!!

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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: