栈的接口
public interface StackInterface <T>{
public void push(T newEntry);
public T pop();
public T peek();
public boolean isEmpty();
public void clear();
}
链表实现栈
import java.io.Serializable;
public class LinkedStack<T> implements StackInterface<T>,Serializable{
private Node topNode; //链表实现栈,表头结点存放栈顶元素
public LinkedStack()
{
topNode=null;
}
private class Node implements Serializable
{
private T data;
private Node next;
public Node(T data, Node next) {
super();
this.data = data;
this.next = next;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
@Override
public void push(T newEntry) {
// TODO Auto-generated method stub
Node newNode =new Node(newEntry,topNode);
topNode=newNode;
}
@Override
public T pop() {
// TODO Auto-generated method stub
T top=null;
if(topNode!=null)
{
top=topNode.getData();
topNode=topNode.getNext();
}
return top;
}
@Override
public T peek() {
// TODO Auto-generated method stub
T top=null;
if(topNode!=null)
top=topNode.getData();
return top;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return topNode==null;
}
@Override
public void clear() {
// TODO Auto-generated method stub
topNode=null;
}
public static void main(String[] argv)
{
LinkedStack<Integer> stack=new LinkedStack<Integer>();
stack.push(1);
System.out.println(stack.peek());
stack.push(2);
stack.push(3);
System.out.println(stack.peek());
stack.pop();
System.out.println(stack.peek());
System.out.println(stack.isEmpty());
stack.clear();
System.out.println(stack.isEmpty());
}
}
循环链表实现队列
import java.io.Serializable;
public class CircularLinkedQueue<T> implements QueueInterface<T> ,Serializable{
private Node queueNode;
private Node freeNode;
public CircularLinkedQueue()
{
freeNode=new Node(null,null);
freeNode.setNext(freeNode);
queueNode=freeNode;
}
private class Node implements Serializable
{
private T data;
private Node next;
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node(T data, Node next) {
super();
this.data = data;
this.next = next;
}
}
@Override
public void enqueue(T newEntry) {
// TODO Auto-generated method stub
if(isChainFull())
{
Node newNode=new Node(null, freeNode.getNext());
freeNode.setNext(newNode);
}
freeNode.setData(newEntry);
freeNode=freeNode.getNext();
}
@Override
public T dequeue() {
// TODO Auto-generated method stub
T front=null;
if(!isEmpty())
{
front=queueNode.getData();
queueNode.setData(null);
queueNode=queueNode.getNext();
}
return front;
}
@Override
public T getFront() {
// TODO Auto-generated method stub
T front=null;
if(!isEmpty())
front=queueNode.getData();
return front;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return freeNode==queueNode;
}
@Override
public void clear() {
// TODO Auto-generated method stub
if(!isEmpty())
{
while(queueNode!=freeNode)
{
queueNode.setData(null);
queueNode=queueNode.getNext();
}
}
queueNode=freeNode;
}
private boolean isChainFull()
{
return queueNode==freeNode.getNext();
}
public static void main(String[] argv)
{
LinkedQueue<Integer> queue=new LinkedQueue<Integer>();
queue.enqueue(1);
System.out.println(queue.isEmpty());
System.out.println(queue.getFront());
queue.enqueue(2);
System.out.println(queue.dequeue());
queue.clear();
System.out.println(queue.getFront());
}
}
数组实现栈
import java.io.Serializable;
import javax.xml.soap.SAAJMetaFactory;
public class ArrayStack<T> implements StackInterface<T>,Serializable {
private T[] list;
private int topIndex;
private static final int INICIAL_SIZE=1;
public ArrayStack()
{
this(INICIAL_SIZE);
}
public ArrayStack(int inicialSize)
{
list=(T[])new Object[inicialSize];
topIndex=-1;
}
public void doubleArray() {
T[] old = list;
int oldsize = old.length;
list = (T[]) new Object[2 * oldsize];
for(int i=0;i<oldsize;i++)
list[i]=old[i];
}
public boolean isArrayFull()
{
return topIndex+1==list.length;
}
@Override
public void push(T newEntry) {
// TODO Auto-generated method stub
if(isArrayFull())
doubleArray();
topIndex++;
list[topIndex]=newEntry;
}
@Override
public T pop() {
// TODO Auto-generated method stub
T top=null;
if(!isEmpty())
{
top=list[topIndex];
list[topIndex]=null;
topIndex--;
}
return top;
}
@Override
public T peek() {
// TODO Auto-generated method stub
T top=null;
if(!isEmpty())
top=list[topIndex];
return top;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return topIndex<0;
}
@Override
public void clear() {
// TODO Auto-generated method stub
for(int i=0;i<list.length;i++)
list[i]=null;
topIndex=-1;
}
public int getLength()
{
return list.length;
}
public static void main(String[] argv)
{
ArrayStack<Integer> stack=new ArrayStack<Integer>();
stack.push(1);
System.out.println("length : "+stack.getLength());
System.out.println(stack.peek());
stack.push(2);
System.out.println("length : "+stack.getLength());
stack.push(3);
System.out.println(stack.peek());
stack.pop();
System.out.println(stack.peek());
System.out.println(stack.isEmpty());
stack.clear();
System.out.println(stack.isEmpty());
System.out.println("length : "+stack.getLength());
}
}