Implementing stacks and queues with arrays

Source: Internet
Author: User

This article is a simulation of linkedlist implementation of which: Add, value, queue, out team, into the stack, out of the stack

1. Implementation of dynamic arrays

Package Com.sxt.test.java; Public classArray {protectedobject[] Arrays =Newobject[0]; protectedObject[] Arrays2=NULL;
With each additional element, create a new array that is +1 longer than the original.
Put the data of the original array into a new array, and finally add the new data to the last
Original array Empty Public voidAdd (Object o) {
if(arrays!=NULL&& arrays2==NULL) {Arrays2=Newobject[arrays.length+1]; for(intI=0; i<arrays.length;i++) {Arrays2[i]=Arrays[i]; } Arrays2[arrays.length]=o; Arrays=NULL; return; } if(arrays==NULL&& arrays2!=NULL) {Arrays=Newobject[arrays2.length+1]; for(intI=0; i<arrays2.length;i++) {Arrays[i]=Arrays2[i]; } Arrays[arrays2.length]=o; Arrays2=NULL; return; }}//Fetch data, two arrays must have one empty, one with data PublicObjectGet(intindex) {Object o=NULL; Try { if(arrays!=NULL) {o=Arrays[index]; } if(arrays2!=NULL) {o=Arrays[index]; } }Catch(Exception e) {Throw NewRuntimeException ("something went wrong .", E); } returno; } Public intLength () {intLength =0; if(arrays!=NULL) {length=arrays.length; }Else if(arrays2!=NULL) {length=arrays2.length; } returnlength; }}

2. Implementation of the queue

Package Com.sxt.test.java; Public classQueue extends array{/** * Queue **/     Public voidAdd (Object o) {super.add (o); }    /** * out of the team
* Two arrays must have data, one is empty
* Take out the first data and create a new array that is 1 longer than the previous one
* From the back forward, the data into the new array, the original first data is not put in
* Finally return the first data retrieved **/ PublicObject Poll () {Object o=NULL; if(arrays!=NULL) {o= arrays[0]; Arrays2=Newobject[arrays.length-1]; for(inti=arrays.length-1;i>0; i--) {Arrays2[i-1]=Arrays[i]; } Arrays=NULL; }Else if(arrays2!=NULL) {o= arrays2[0]; Arrays=Newobject[arrays2.length-1]; for(inti=arrays2.length-1;i>0; i--) {arrays[i-1]=Arrays2[i]; } arrays2=NULL; } returno; }}

3. Implementation of the Stack

Package Com.sxt.test.java; Public classStack extends array{/** * Press Stack **/     Public voidpush (Object o) {super.add (o); }        /** * out of the stack
* Take the last data of the array to return
* Then create an array that is 1 longer than the original, and put the data into a new array before the last data is placed
*
* Finally return the last data retrieved **/ PublicObject Pop () {object o=NULL; if(arrays!=NULL) {o= arrays[arrays.length-1]; Arrays2=Newobject[arrays.length-1]; for(intI=0; i<arrays.length-1; i++) {Arrays2[i]=Arrays[i]; } Arrays=NULL; }Else if(arrays2!=NULL) {o= arrays2[arrays2.length-1]; Arrays=Newobject[arrays2.length-1]; for(intI=0; i<arrays2.length-1; i++) {Arrays[i]=Arrays2[i]; } arrays2=NULL; } returno; }}

4. Test class

/** * Dynamic Arrays Array Test **/Array a=NewArray (); A.add ("a"); A.add ("b"); A.add ("C"); A.add ("D"); System. out. println (A.length ());  for(intI=0; I<a.length (); i++) {System. out. println (A.Get(i)); } System. out. println ("------------"); /** * Queue Test **/Queue Queue=NewQueue (); Queue.add ("a"); Queue.add ("b"); Queue.add ("C"); Queue.add ("D"); Queue.add ("e"); Queue.add ("F"); System. out. println (Queue.length ());  while(Queue.length () >0) {System. out. println (Queue.poll ()); } System. out. println ("----------"); /** * Stack Test **/Stack Stack=NewStack (); Stack.push ("a"); Stack.push ("b"); Stack.push ("C"); Stack.push ("D"); Stack.push ("e"); Stack.push ("F"); System. out. println (Stack.length ());  while(Stack.length () >0) {System. out. println (Stack.pop ()); }

Implementing stacks and queues with arrays

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.