Queuedemo+deque Two-way queue demo

Source: Internet
Author: User

Queuedemo

/*** Presentation of queue queues * knowledge points: Queues are a common data organization that follows the principle of FIFO * The queue can be viewed as a special linear table, and the queue restricts access to the linear table: * You can only add (offer) elements from one end of the linear table, from the other end The (poll) element. * The queue interface is provided in the JDK and allows LinkedList to implement the interface * Cause: Select LinkedList to implement the queue because the queue is often inserted and deleted, * and LinkedList is more efficient in this regard. * Applications: such as the game queue, need to orderly access, there are queuing requirements, you can use the queue data structure. * * Main Method: * 1.    Boolean offer (E): Appends the element to the end of the queue and returns true if the addition succeeds.    2. E poll (): Removes and returns the element from the first team. 3. E Peek (): Returns the first element of the team, but does not delete it.    * * Exercise: * 1) use LinkedList to build the queue and put the string "a", "B", "C" into the queue.    2) Gets the first element of the queue Squadron.    3 Add the string "D" to the queue.    4) Delete the element from the beginning of the team until there are no elements in the queue and output the deleted team first element at the same time as the deletion. Personal Summary: 1. About the queue data structure, is linear, is only from the tail in, head out.    Follow the FIFO principle (such as a multi-section pencil) 2. The traversal operation is not used in the queue with a while, and the size of the queue () is the loop condition and the loop variable.    3. Traversal operations are one-time in the queue, traversed once, and the queue has no elements. 4. If the Peek method is used to traverse the queue, only the first element of the same team can be removed indefinitely. Because it only gets the first element of the team, it does not remove the first element of the team. */ Public classQueuedemo { Public Static voidMain (string[] args) {//1. Use LinkedList to build the queue, placing the string "a", "B", "C" in the queue. Queue<string> queuelist =NewLinkedlist<string>(); Queuelist.offer (A); Queuelist.offer ("B"); Queuelist.offer (C); //2. Get the first element of the queue Squadron.         /** E-peek ()*/String First= Queuelist.peek ();//gets the header of this queue without removing it, or null if this queue is empty. //returns the first element of a value queue, but does not remove the first element of the queue.System.out.println ("The first element of the queue is" +first);//the first element of the queue is aSystem.out.println (queuelist);//[A, B, c]//3. Add the string "D" to the queue.        /** Boolean offer () * Inserts the specified element into this queue, or returns True if the element has been added to this queue; otherwise false*/        Booleanr = Queuelist.offer ("D"); System.out.println ("String is added successfully:" +R); //whether the string was added successfully: TrueSystem.out.println (queuelist); //[A, B, C, d]//4. Delete the element from the beginning of the team until there are no elements in the queue and output the deleted team first element at the same time as the deletion.         /** E Poll * Gets and removes the header of this queue, and returns null if this queue is empty.         * Idea: Traverse the queue, constantly remove the team head, that is, delete success * Note: Traverse the queue do not often do, because you need to take out to traverse all elements.         * The method of traversing the queue is used while, because for the experiment, it is not appropriate because the size of the queue (), in the continuous cycle * process, the continuous decrease. So the while (size () >0 of the queue). *          */         while(Queuelist.size () >0) {String delstr=Queuelist.poll (); //Note: If you use List.peek (), you will be looping, constantly viewing the same team first elementSystem.out.println ("delete element (first element of team):" +delstr);        } System.out.println (Queuelist); /** Output: * Delete Element (team first element): A DELETE element (team first element): B Delete Element (team first element): C Delete Element (first element of team): d []*/    }}
View Code

Stackdemo Dual-ended queue

/*** Deque interface, which is the sub-interface of the queue. Defines the so-called "double-ended queue." * that is, from the two ends of the queue can be queued (offer) and the team (poll). * The same linkedlist implemented the interface. If the deque is restricted to only one end When the queue (push) is out of the queue (POP), the data structure of the stack is formed. * Stack follows the advanced post-out principle. * Application scenario: Need to reverse the operation, forward operation (such as WinDOS's forward and backward function) * * Deque provides a method of Operation Stack, the main method is as follows: void Push (E): The given element is "pressed into" the stack. The deposited element will be at the top of the stack. That is: The first element of the stack E pop (): Delete and return the first element of the stack.    * This case requires the use of test deque, detailed requirements are as follows: 1) using the LinkedList build stack, put the string "a", "B", "C" into the stack.    2) gets the stack top element in the stack. 3) Delete the element from the top of the stack until there are no elements in the stack, and output the deleted stack top element at the same time as the deletion. *     */ Public classStackdemo { Public Static voidMain (string[] args) {//1) using the LinkedList build stack, put the string "a", "B", "C" into the stack. Deque<string> stacklist =NewLinkedlist<string>(); Stacklist.push (A); Stacklist.push ("B"); Stacklist.push (C); /** void push (e E) * pushes the given element into the stack.         The deposited element will be at the top of the stack. */Stacklist.push ("D");//"D" into the stack//2) Gets the stack top element in the stack.         /** E Peek () gets, but does not remove the head of the queue represented by this double-ended queue * (in other words, the first element of this double-ended queue); * Returns null if this double-ended queue is empty. */String topstr=Stacklist.peek (); System.out.println ("Stack top element:" +TOPSTR);//stack top element:dSystem.out.println (stacklist);//[D, C, B, a]//3) Delete the element from the top of the stack until there are no elements in the stack, and output the deleted stack top element at the same time as the deletion.         /** E pop () * Deletes and returns the first element of the stack. * Idea: Traverse stack queue, bounce stack (POP)*/         while(Stacklist.size () >0) {String outstr=Stacklist.pop (); System.out.println ("Stack element:" +outstr);        } System.out.println (Stacklist); /*output: Stack element: D -Stack element: C-Stack element: B-Stack element: a []*/    }}
View Code

Queuedemo+deque Two-way queue demo

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.