Algorithm (algorithms) version 4th practice 1.3.8

Source: Internet
Author: User
Tags stdin

Method Implementation:

//1.3.8 packagecom.qiusongde;Importjava.util.Iterator;Importjava.util.NoSuchElementException;Importedu.princeton.cs.algs4.StdIn;Importedu.princeton.cs.algs4.StdOut; public classResizingarraystack<item>ImplementsIterable<item> {    Privateitem[] content; Private intnumber ;  publicresizingarraystack () {content= (item[])NewObject[1]; number= 0; }        Private voidResizearray (intmax) {                if(max <number )Throw NewIllegalArgumentException ("the size of the new array must larger than the size of the Stack"); item[] Temp= (item[])Newobject[max];  for(inti = 0; I < number; i++) {temp[i]=content[i]; } content=temp; }             public BooleanisEmpty () {returnNumber = = 0; }         public intsize () {returnnumber ; }         public voidpush (item Item) {if(number = =Content.length) Resizearray (2 *content.length); Content[number++] =item; }         publicItem pop () {if(isEmpty ())Throw NewNosuchelementexception ("Stack is empty"); Item Item= content[--number ]; content[number]=NULL;//aoid loitering                if(number = = CONTENT.LENGTH/4 && number > 0) Resizearray (content.length/2); returnitem; } @Override publicIterator<item>iterator () {return NewReversearrayiterator (); }        Private classReversearrayiteratorImplementsIterator<item> {        Private inti =number ; @Override public Booleanhasnext () {returnI > 0; } @Override publicItem next () {if(!Hasnext ())Throw NewNosuchelementexception ("Stack is empty"); returncontent[--i]; } @Override public voidRemove () {Throw Newunsupportedoperationexception (); }            }        //Just for Test (main)    Private intarraylength () {returncontent.length; }         public Static voidmain (string[] Args) {resizingarraystack<String> stack =NewResizingarraystack<string>(); Stdout.println ("Initialized size:" + stack.size () + "Array size:" +Stack.arraylength ());  while(!Stdin.isempty ()) {String Item=stdin.readstring (); if(!item.equals ("-") {stack.push (item); Stdout.println ("push success:" + Item + "size:" + stack.size () + "Array size:" +Stack.arraylength ()); Stdout.print ("left on stack:");  for(String s:stack) {stdout.print (s+ " ");                            } stdout.println (); } Else {                if(stack.isempty ()) stdout.println ("pop error, Stack empty"); Else{stdout.println ("pop success:" + stack.pop () + "size:" + stack.size () + "Array size:" +Stack.arraylength ()); Stdout.print ("left on stack:");  for(String s:stack) {stdout.print (s+ " ");                } stdout.println (); }            }                    }            }    }

Test Results:

Initialized size:0 Array Size:1itpush success:it size:1 array size:1left on stack:it waspush success:was size:2 array size:2left on STACK:W As It-pop Success:was size:1 array size:2left on stack:it thepush success:the size:2 Array size:2left on stack:the it b  Estpush success:best size:3 Array size:4left on Stack:best the It-pop success:best size:2 Array size:4left on stack:the It ofpush success:of size:3 array size:4left on stack:of the IT timespush success:times size:4 Array size:4left on stack : Times of the It-pop success:times size:3 array size:4left on stack:of the It-pop success:of size:2 Array size:4left o n stack:the it-pop success:the size:1 array size:2left on stack:it itpush success:it size:2 Array size:2left on Stack: It it waspush success:was size:3 array size:4left on Stack:was it it-pop success:was size:2 Array size:4left on stack:i  T it thepush success:the size:3 array size:4left on stack:the it it-pop success:the size:2 Array size:4left on Stack:it it-Pop success:it size:1 Array size:2left on Stack:it 

Algorithm (algorithms) version 4th practice 1.3.8

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.