链表 数组 向量实现的栈(范型)

公共接口

package edu.cumt.jnotnull;

public interface StackInterface<T> {
	public void push(T newEntry);
	
	public T pop();
	
	public  T peek();
	
	public boolean isEmpty();
	
	public void clear();
}

 

链表

package edu.cumt.jnotnull;

import java.io.Serializable;

public class LinkedStack<T> implements StackInterface<T> , Serializable {

	private Node<T> topNode;
	public LinkedStack(){
		topNode = null;
	}
	@Override
	public void clear() {
		topNode = null;
	}

	@Override
	public boolean isEmpty() {
		return topNode == null;
	}

	@Override
	public T peek() {
		T top = null;
		if(topNode!=null){
			top = topNode.getData();
		}
		return top;
	}

	@Override
	public T pop() {
		T top = null;
		if(topNode!=null){
			top = topNode.getData();
			topNode = topNode.getNext();
		}
		return top;
	}

	@Override
	public void push(T newEntry) {
		Node<T> newNode = new Node<T>(newEntry,topNode);
		topNode = newNode;
	}
	
	private class Node<T> implements Serializable {
		private T data;
		private Node<T> next;
		
		private Node(T dataPortion){
			data = dataPortion;
			next = null;
		}
		
		private Node(T dataPortion,Node<T> nextNode){
			data = dataPortion;
			next = nextNode;
		}

		public T getData() {
			return data;
		}

		public void setData(T data) {
			this.data = data;
		}

		public Node<T> getNext() {
			return next;
		}

		public void setNext(Node<T> next) {
			this.next = next;
		}
		
		
	}

}

 

数组

package edu.cumt.jnotnull;

import java.io.Serializable;

public class ArrayStack<T> implements Serializable, StackInterface<T> {

	private T[] stack;
	private int topIndex;
	private static final int DEFAULT_MAX_SIZE = 50;
	
	public ArrayStack(){
		this(DEFAULT_MAX_SIZE);
	}
	public ArrayStack(int initialCapacity){
		stack = (T[])new Object[initialCapacity];
	}
	@Override
	public void clear() {
		stack = (T[])new Object[DEFAULT_MAX_SIZE];
	}

	@Override
	public boolean isEmpty() {
		return topIndex < 0;
	}

	@Override
	public T peek() {
		T top = null;
		if(!isEmpty()){
			top = stack[topIndex];
		}
		return top;
	}

	@Override
	public T pop() {
		T top = null;
		if(!isEmpty()){
			top = stack[topIndex];
			stack[topIndex] = null;
			topIndex--;
		}
		return top;
	}

	@Override
	public void push(T newEntry) {
		topIndex++;
		if(topIndex>=stack.length){
			doubleArray();
		}
		stack[topIndex] = newEntry;
	}
	
	private void doubleArray(){
		T[] temp = null;
		if(!isEmpty()){
			temp = (T[])new Object[stack.length*2];
			for(int i=0;i<stack.length;i++){
				temp[i] = stack[i];
			}
		}
		stack = temp;
	}

}

 

向量

package edu.cumt.jnotnull;

import java.io.Serializable;
import java.util.Vector;

public class VectorStack<T> implements Serializable, StackInterface<T> {

	public Vector<T> stack;
	
	public VectorStack(){
		stack = new Vector<T>();
	}
	
	public VectorStack(int maxSize){
		stack = new Vector<T>(maxSize);
	}
	@Override
	public void clear() {
		stack.removeAllElements();
	}

	@Override
	public boolean isEmpty() {
		return stack.isEmpty();
	}

	@Override
	public T peek() {
		T top = null;
		if(!isEmpty())
			top = stack.lastElement();
		return top;
	}

	@Override
	public T pop() {
		T top = null;
		if(!isEmpty()){
			top = stack.lastElement();
			stack.removeElementAt(stack.size()-1);
		}
		return top;
	}

	@Override
	public void push(T newEntry) {
		stack.addElement(newEntry);
	}

}

 

你可能感兴趣的:(数组)