Two special data structures: queue and stack; two types of data structure queue

Source: Internet
Author: User

Two special data structures: queue and stack; two types of data structure queue

I. Queue

Queue (Queue: the first data structure to be introduced in this article): You can only add (offer) elements from one end of a linear table and retrieve (poll) elements from the other end, and follow FIFO (first-in-first-out)

Select the Queue list to implement the Queue interface, because the Queue list is more efficient in insert and delete operations.

Related Operations:

Boolean offer (E e): append an elementEnd of queue

E poll (): deletes and returns the first element of the team.

E peek (): returns only the first element of the team and does not delete it.

Instance programming:

1 package Java20170331; 2 3 import java. util. optional list; 4 import java. util. queue; 5 6 public class BlogDemo {7 public static void main (String [] args) {8/** 9 * Queue10 * boolean offer (E) add elements, put 11 * E poll () at the end of the queue to extract and delete the first element of the queue 12 * E peek () to extract the first element of the queue, do not delete 13 */14 Queue <String> queue = new queue list <String> (); 15 Queue. offer ("one"); 16 queue. offer ("two"); 17 queue. offer ("three"); 18 queue. offer ("four"); 19 System. out. println ("original queue column:" + queue); 20 queue. offer ("1"); 21 System. out. println ("added after 1:" + queue); 22 String str = queue. poll (); 23 System. out. println ("extracted elements:" + str); 24 System. out. println ("the queue after poll operation is:" + queue); 25 String str1 = queue. peek (); 26 System. out. println ("extracted elements:" + str1); 27 System. out. println ("the queue after the peek operation is:" + queue); 28 while (queue. size ()> 0) {29 str = queue. poll (); 30 System. out. println (str); 31} 32 System. out. println (queue); 33} 34}
The running result is:
The original queue column is [one, two, three, four]. After 1 is added, the extracted elements are: [one, two, three, four, 1]. The queue after onepoll operation is: [two, three, four, 1] The extracted elements are: [two, three, four, 1] twothreefour1 []

 

Ii. Stack

(Dual-end Queue) Deque is a sub-interface of Queue. It can be used to Queue and Queue from both ends of the Queue.

If Deque is restricted to only one end of the queue, that is, Stack (Stack: the second data structure to be introduced in this article), push, and pop ), follow FILO (Advanced and later)

Related Operations:

Void push (E e): elements are put into the stackStack head

E pop (): deletes and returns the first element of the stack.

E peek (): only the first element of the stack is returned. It is not deleted (same as the queue)

 

Package Java20170331; import java. util. deque; import java. util. optional list; public class BlogDemo {public static void main (String [] args) {/*** Deque-> Stack * void push (E e): add elements, put it in the first stack * E pop (): delete and return the first stack element * E peek (): return the first stack element, not delete (same as the queue) * // check the API. Stack is also a class. Based on the API, The Stack class is not used to create stacks because: // The Deque interface and its implementation provide a more complete and consistent set Deque <String> stack = new consumer list <String> (); stack for LIFO stack operations. push ("one"); stack. push ("two"); stack. push ("three"); stack. push ("four"); System. out. println ("original stack:" + stack); String str1 = stack. pop (); System. out. println ("pop () operation extracts the following elements:" + str1); System. out. the stack after the println ("pop () operation is:" + stack); String str2 = stack. peek (); System. out. the println ("peek () operation extracts the following elements:" + str2); System. out. println ("peek () stack:" + stack );}}

 

The running result is:
The original stack is: [four, three, two, one] The elements extracted by the pop () operation are: the stack after the four pop () operation is: [three, two, one] The elements extracted by the peek () operation are: [three, two, one] stacks after the threepeek () operation.

 

Related Article

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.