Use consumer list to implement stack and queue)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.