1. Provides a set of stacks of interfaces that are associated with a linkedlist (two-terminal queue) instance. It can provide a secure stack implementation because it exposes only some of the interfaces that are based on the stack implementation.
Import java.util.LinkedList;
public class Stack<T> {
private LinkedList<T> storage = new LinkedList<T>();
/** 入栈 */
public void push(T v) {
storage.addFirst(v);
}
/** 出栈,但不删除 */
public T peek() {
return storage.getFirst();
}
/** 出栈 */
public T pop() {
return storage.removeFirst();
}
/** 栈是否为空 */
public boolean empty() {
return storage.isEmpty();
}
/** 打印栈元素 */
public String toString() {
return storage.toString();
}
}
2. Using the Java.util.Queue interface, the bottom layer is associated with a linkedlist (two-terminal queue) instance. A secure queue implementation can be provided by exposing only some of the queued implementation-based interfaces.
Import java.util.LinkedList;
Import java.util.Queue
public class Myqueue<t> {
Private queue<t> storage = new LinkedList <T> ();
/** inserts the specified element into the team tail */
public void offer (T v) {
Storage.offer (v);
/** retrieves, but does not remove the header of the queue, and returns NULL */
Public T Peek () {
return Storage.peek () If this queue is empty;
/** retrieves, but does not remove the header */
/** This method is the only difference from the Peek method is that if the queue is empty, it throws an exception */
Public T Element () {
Return St Orage.element ();
}
/** retrieves and removes the header of this queue and returns NULL */
Public T poll () {
return Storage.poll () if the queue is empty;
/** Retrieves and removes the header */
of this queue/** this method differs from the poll method in that if the queue is empty, it throws an exception */
Public T Remove () {
Return storage . remove ();
}
/** queue is empty */
Public boolean empty () {
return storage.isempty ();
/** Print Queue element */
Public String toString () {
return storage.tostring ();
}
}
This article is from the "Zi Postman" blog, please be sure to keep this source http://zhangjunhd.blog.51cto.com/113473/69912