1. Provides an interface for a set of stacks whose underlying is associated to an LinkedList (double-ended queue) instance. A secure stack implementation can be provided because only a portion of the stack-based interface is exposed.
Package Junit;import java.util.linkedlist;/** * with LinkedList implementation stack * * Queue and Stack differences: Queue FIFO First, stack advanced out. * * @author Lin Yi-chin * @version 1.0 Sep 5, 11:24:34 PM */public class Stack<t> {private linkedlist<t> sto Rage = new linkedlist<t> (); /** into stack */public void push (T v) {Storage.addfirst (v); }/** out the stack, but does not delete */public T Peek () {return Storage.getfirst (); }/** out the stack, delete */public T pop () {return Storage.removefirst (); }/** Stack is empty */public Boolean empty () {return storage.isempty (); }/** Print Stack element */public String toString () {return storage.tostring (); } public static void Main (string[] args) {Stack stack=new stack<string> (); Stack.push ("a"); Stack.push ("B"); Stack.push ("C"); [C, B, a] System.out.println (stack.tostring ()); C--[c, B, a] Object obj=stack.peek (); System.out.println (obj+ "--" +stack.tostring ()); Obj=stack.pop (); C--[b, A] System.out.println (obj+ "--" +stack.tostring ()); False System.out.println (Stack.empty ()); }}
2. Using the Java.util.Queue interface, the underlying is associated to an LinkedList (double-ended queue) instance. A secure queue implementation can be provided because only some of the interfaces that are based on the queue implementation are exposed.
Package Junit;import java.util.linkedlist;import java.util.queue;/** * Queue and Stack differences with LinkedList: Queue FIFO First, stack advanced out. * * @author Lin Yi-chin * @version 1.0 Sep 5, 11:26:45 PM */public class Myqueue<t> {private queue<t> stor Age = new linkedlist<t> (); /** inserts the specified element into the end of the queue */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 of this queue *//** This method differs from the Peek method only if it throws an exception if this queue is empty */public T Element () {return Storage.elem ENT (); /** 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 this queue is empty, it throws an exception */public T remove () {return storage.remove (); The/** queue is empty */public Boolean empty () {return storage.isempty (); /** print Queue Element */public String toString () {return storage.tostring (); } public static void Main (String[] args) {myqueue stack=new myqueue<string> (); Stack.offer ("a"); Stack.offer ("B"); Stack.offer ("C"); [A, B, C] System.out.println (stack.tostring ()); Object Obj=stack.peek (); A--[a, B, c] System.out.println (obj+ "--" +stack.tostring ()); Obj=stack.element (); A--[a, B, c] System.out.println (obj+ "--" +stack.tostring ()); Obj=stack.poll (); A--[b, C] System.out.println (obj+ "--" +stack.tostring ()); Obj=stack.remove (); B--[c] System.out.println (obj+ "--" +stack.tostring ()); False System.out.println (Stack.empty ()); }}
Java: Implementing Stacks and queues based on LinkedList