Java实现栈和队列


import java.util.LinkedList;

/**栈的定义*/
public class Stack {
	/**定义栈的存储结构,LinkedList*/
	private LinkedList storage = new LinkedList();
	
	/**压栈操作*/
	public void push(T t){storage.addFirst(t); }
	
	/**获取栈顶元素*/
	public T getTop() {return storage.getFirst(); }
	
	/**出栈操作*/
	public T pop() {return storage.removeFirst(); }
	
	/**判断栈是否为空*/
	public boolean isEmpty() {return storage.isEmpty(); }
	
	/**打印栈中的元素*/
	public String toString() {return storage.toString(); } 
	
}

 
  

import java.util.LinkedList;

/**队列的定义*/
public class Queue {

	/**定义队列的存储结构,LinkedList*/
	private LinkedList storage = new LinkedList();
	
	/**进队操作*/
	public void push(T t) {storage.addLast(t);}
	
	/**获取队首元素*/
	public T getHead() {return storage.getFirst();}
	
	/**出队操作*/
	public T pop(){return storage.removeFirst();}
	
	/**判断队列是否为空*/
	public boolean isEmpty() {return storage.isEmpty();}
	
	/**打印队列元素*/
	public String toString(){return storage.toString();}
}



你可能感兴趣的:(数据结构)