First, we will reference the definition of the detail list in jdk api: "These operations allow linked lists to be used as a stack, queue, or double-ended queue. "From this, we can know that using the consumer list can easily implement the stack and queue functions. By viewing the javaslist source code implementation, it is found that the underlying implementation uses a two-way linked list.
I. Use consumer list to implement Stack
Public class teststack { Public static void main (string [] ARGs) { Mystack MS = new mystack ();
Ms. Push ("AAA "); Ms. Push ("BBB "); Ms. Push (New INTEGER (3 ));
System. Out. println (Ms. Peek ()); System. Out. println (Ms. Pop ()); System. Out. println (Ms. isempty ()); System. Out. println (Ms. Pop ()); System. Out. println (Ms. Pop ()); System. Out. println (Ms. isempty ()); } } |
|
|
|
Public class teststack
{
Public static void main (string [] ARGs)
{
Mystack MS = new mystack ();
Ms. Push ("AAA ");
Ms. Push ("BBB ");
Ms. Push (New INTEGER (3 ));
System. Out. println (Ms. Peek ());
System. Out. println (Ms. Pop ());
System. Out. println (Ms. isempty ());
System. Out. println (Ms. Pop ());
System. Out. println (Ms. Pop ());
System. Out. println (Ms. isempty ());
}
}
Import Java. util. extends list; public class teststack {public static void main (string [] ARGs) {mystack MS = new mystack (); Ms. push ("AAA"); Ms. push ("BBB"); Ms. push (New INTEGER (3); system. out. println (Ms. peek (); system. out. println (Ms. pop (); system. out. println (Ms. isempty (); system. out. println (Ms. pop (); system. out. println (Ms. pop (); system. out. println (Ms. isempty ();} class mystack {// maintained in both the stack and queue A member variable used to store the elements of a stack or queue. The member variable is of the sort list type. Private synchronized list = new synchronized list (); Public void push (Object O) {list. add (o);} public object POP () {return list. removelast ();} public object PEEK () // view the top element of the stack {return list. getlast ();} public Boolean isempty () {return list. isempty ();}}
The output result in eclipse is:
--------------------------------------------------------------------------------
3
3
False
Bbb
Aaa
True
--------------------------------------------------------------------------------
Ii. Use Queue list to implement queue
Import Java. util. required list; public class testqueue {public static void main (string [] ARGs) {myqueue MQ = new myqueue (); MQ. put ("AAA"); MQ. put ("BBB"); MQ. put (New INTEGER (3); system. out. println (MQ. get (); system. out. println (MQ. isempty (); system. out. println (MQ. get (); system. out. println (MQ. get (); system. out. println (MQ. isempty ();} class myqueue {// a member variable is maintained in both the stack and queue. The member variable is used to store the elements of the stack or queue. The member variable uses Link Edlist type. Private synchronized list = new synchronized list (); Public void put (Object O) {list. add (o); // This method is equivalent list. addfirst (o);} public object get () {return list. remove (); // or return list. removefirst ();} public Boolean isempty () {return list. isempty ();}}
The output result in eclipse is:
-------------------------------------------------------------------------------
Aaa
False
Bbb
3
True
-------------------------------------------------------------------------------
Use consumer list to implement stack and queue)