Algorithm Sedgewick Fourth Edition-1th Chapter Foundation-151 stack only retains the last pointer

Source: Internet
Author: User
Tags stdin

1 /*************************************************************************2  *3 * A generic queue, implemented using A *circular* linked list.4 * (Exercise 1.3.29)5  *6 *% java ex_1_3_29 < tobe.txt7 * To IS or not to is (2 left on queue)8  *9  *************************************************************************/Ten  One ImportJava.util.Iterator; A Importjava.util.NoSuchElementException; -  -  Public classEx_1_3_29<item>ImplementsIterable<item> { the     Private intN; -     PrivateNode last; -  -     Private classNode { +         Privateitem Item; -         PrivateNode Next; +     } A  at    /** - * Create an empty queue. -      */ -      Publicex_1_3_29 () { -Last =NULL; -     } in  -    /** to * is the queue empty? +      */ -      Public BooleanIsEmpty () { the         returnlast = =NULL; *     } $ Panax Notoginseng    /** - * Return the number of items in the queue. the      */ +      Public intsize () { A         returnN;  the     } +  -    /** $ * Return The item least recently added to the queue. $ * Throw An exception if the queue is empty. -      */ -      PublicItem Peek () { the         if(IsEmpty ())Throw NewRuntimeException ("Queue underflow"); -         returnLast.next.item;Wuyi     } the  -    /** Wu * ADD the item to the queue. -      */ About      Public voidEnqueue (item item) { $Node x =NewNode (); -X.item =item; -         if(IsEmpty ()) -X.next =x; A         Else +         { theX.next =Last.next; -Last.next =x; $         } theLast =x; then++; the     } the  -    /** in * Remove and return the item on the queue least recently added. the * Throw An exception if the queue is empty. the      */ About      PublicItem dequeue () { the         if(IsEmpty ())Throw NewRuntimeException ("Queue underflow"); theItem item =Last.next.item; the         if(Last.next = =Last ) +Last =NULL; -         Else theLast.next =Last.next.next;Bayin--; the         returnitem; the     } -  -    /** the * Return string representation. the      */ the      PublicString toString () { theStringBuilder s =NewStringBuilder (); -          for(Item Item: This) theS.append (item + "")); the         returns.tostring (); the     } 94   the  the    /** the * Return An iterator this iterates over the items on the ' queue ' in FIFO order.98      */ About      PublicIterator<item>iterator () { -         return NewListiterator (); 101     }102 103     Private classListiteratorImplementsIterator<item> {104         Private intn =N; the         PrivateNode current =Last ;106 107          Public BooleanHasnext () {returnn > 0; }108          Public voidRemove () {Throw Newunsupportedoperationexception (); }109  the          PublicItem Next () {111             if(!hasnext ())Throw Newnosuchelementexception (); theItem item =Current.next.item;113Current =Current.next; then--; the             returnitem; the         }117     }118 119  -      Public Static voidMain (string[] args) {121ex_1_3_29<string> q =NewEx_1_3_29<string>();122          while(!Stdin.isempty ()) {123String item =stdin.readstring ();124             if(!item.equals ("-") ) Q.enqueue (item); the             Else if(!q.isempty ()) Stdout.print (Q.dequeue () + "");126         }127Stdout.println ("(" + q.size () + "left on queue: [" + Q + "])"); -     }129}

Algorithm Sedgewick Fourth Edition-1th Chapter Foundation-151 stack only retains the last pointer

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.