Java implementation stack and queue

Source: Internet
Author: User

First, stack

A stack is a special kind of linear table. The peculiarity is that the manipulation of inserting and deleting data elements can only be done at one end of the linear table. (Advanced post-exit)

Access rights: The stack restricts access, only the tail node, which is the element that is the top of the stack, is the last element to add

1 /**2 * Stack advanced back out3  * @authorAdministrator4  *5  */6  Public classMystack {7     Private Long[] arr;8     Private inttop;9      PublicMystack () {TenArr=New Long[10]; OneTop=-1;//the stack is empty when initialized A     } -      PublicMystack (intmaxSize) { -Arr=New Long[maxSize]; theTop=-1; -     } -     /** - * into the stack +      */ -      Public voidPushLongvalue) { +arr[++top]=value; A     } at     /** - * Move out of Stack -      */ -      Public Longpop () { -         returnarr[top--]; -     } in     /** - * View data at the top of the stack to      */ +      Public LongPeek () { -         returnArr[top]; the     } *     /** $ * To see if it is emptyPanax Notoginseng      */ -      Public BooleanIsEmpty () { the         returnTop==-1; +     } A     /** the * see if it's full +      */ -      Public BooleanIsfull () { $         returnTop==arr.length-1; $     } -     //Test -      Public Static voidMain (string[] args) { theMystack mystack=NewMystack (); -Mystack.push (12);WuyiMystack.push (10); theMystack.push (34); -Mystack.push (40); Wu System.out.println (Mystack.peek ()); -          while(!Mystack.isempty ()) { AboutSystem.out.print (Mystack.pop () + ","); $         } - System.out.println (Mystack.isempty ()); - System.out.println (Mystack.isfull ()); -     } A}
View Code

Second, the queue
Queue is also an operation constrained linear table, its operation limit is different from the stack, there is a limit at both ends, the insertion can only be done at one end of the table (not in),
The deletion can only be done at the other end of the table (only out of the rear), allowing one end of the deletion to be called the tail of the queue (a), allowing the inserted end to be called the team header (Front);

Access rights: The queue restricts access to only the data of the team header, which is the first element to be added.

/*** Queue FIFO *@authorAdministrator **/ Public classMyqueue {Private Long[] arr; Private intElements//Number of elements    Private intFront//Queue Header    Private intEnd//Queue Tail     PublicMyqueue () {arr=New Long[10]; Elements=0; Front=0; End=-1; }       PublicMyqueue (intsize) {arr=New Long[size]; Elements=0; Front=0; End=-1; }      /*** Add data queue to add from trailer*/     Public voidInsertLongvalue) {        if(end==arr.length-1) {End=-1; } arr[++end]=value; Elements++; }    /*** Remove data queue from head*/     Public LongRemove () {Longvalue=arr[front++]; if(front==arr.length) {Front=0; } Elements--; returnvalue; }    /*** View data at the head of the queue*/     Public LongPeek () {returnArr[front]; }    /*** Check if the queue is empty *@return     */     Public BooleanIsEmpty () {returnElements==0; }    /*** Determine if the queue is full*/     Public BooleanIsfull () {returnelements==arr.length; }     Public Static voidMain (string[] args) {myqueue myqueue=NewMyqueue (); Myqueue.insert (1001); Myqueue.insert (1000); Myqueue.insert (1003); Myqueue.insert (1005);        System.out.println (Myqueue.peek ());  while(!Myqueue.isempty ()) {System.out.print (Myqueue.remove ()+", ");        } System.out.println ();        System.out.println (Myqueue.isempty ());                System.out.println (Myqueue.isfull ()); Myqueue.insert (1007); Myqueue.insert (1002); Myqueue.insert (1008); Myqueue.insert (1009);  while(!Myqueue.isempty ()) {System.out.print (Myqueue.remove ()+", ");        } System.out.println ();        System.out.println (Myqueue.isempty ());                System.out.println (Myqueue.isfull ()); Myqueue.insert (1010); Myqueue.insert (1012); Myqueue.insert (1018); Myqueue.insert (1019);  while(!Myqueue.isempty ()) {System.out.print (Myqueue.remove ()+", ");        } System.out.println ();        System.out.println (Myqueue.isempty ());                System.out.println (Myqueue.isfull ()); Myqueue.insert (1021); Myqueue.insert (1022); Myqueue.insert (1023); Myqueue.insert (1024);  while(!Myqueue.isempty ()) {System.out.print (Myqueue.remove ()+", ");        } System.out.println ();        System.out.println (Myqueue.isempty ());    System.out.println (Myqueue.isfull ()); }}
View Code

Java implementation stack and queue

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.