Java Data Structure-Stack

Source: Internet
Author: User

 

The following describes three stack implementation methods. Although Java has a stack class, it has been deprecated. Implement it by yourself !!

1. Implement with linked list

Package COM. jzm. stackqueuetree; public class secure stack <t> {private node topnode; // reference the first public secure stack () {clear ();} in the linked list ();} // end default constructorprivate class node {private t data; // stack element private node next; // connection to the next node private node (t data) {This. data = data; next = NULL;} // end constructor private t getdata () {return data;} private node getnextnode () {return next ;}} private Boolean isempty () {Return topnode = NULL;} private void clear () {topnode = NULL;} public void push (T newentry) {node newnode = new node (newentry); newnode. next = topnode; topnode = newnode;} public t POP () {t top = NULL; If (! Isempty () {Top = topnode. getdata (); topnode = topnode. getnextnode () ;}return top;} public void display () {node point = topnode; system. out. println ("starting from the top element:"); While (point! = NULL) {system. out. println (point. getdata (); point = point. next ;}} public static void main (string [] ARGs) {consumer stack <integer> consumer stack = new consumer stack <integer> (); For (INT I = 0; I <100; I ++) {consumer stack. push (I); if (I % 2 = 0) {consumer stack. pop () ;}} producer stack. display ();}}

2. Implement with Array

 

Package COM. jzm. stackqueuetree; public class arraystack <t> {private T [] A; // The array private int topindex used to store values; // The top index of the stack private final static int max = 500; // default stack size: Public arraystack () {clear (); A = (T []) New object [Max];} public arraystack (INT size) {// custom stack size clear (); A = (T []) New object [size];} private Boolean isfull () {return (topindex> =. length-1);} public void clear () {// clear stack topindex =-1; A = NULL ;} Public void push (T newentry) {If (! Isfull () {A [++ topindex] = newentry;} else {system. out. println ("stack full topindex =" + topindex) ;}} public t POP () {t top = NULL; If (topindex> = 0) {Top = A [topindex]; topindex --;} else {system. out. println ("Stack is empty");} return top;} public void display () {system. out. println ("arraystack starts from the top element:"); For (INT I = topindex; I> = 0; I --) {system. out. println (A [I]) ;}} public static void main (string [] ARGs) {arraystack <integer> arraystack = new arraystack <integer> (); for (INT I = 0; I <100; I ++) {arraystack. push (I); if (I % 2 = 0) {arraystack. pop () ;}// end for arraystack. display ();}}

3. Implement with Vector

Package COM. jzm. stackqueuetree; import Java. util. vector; public class vectorstack <t> {private vector <t> stack; Public vectorstack () {stack = new vector <t> ();} public vectorstack (INT maxsize) {stack = new vector <t> (maxsize);} public void push (T newentry) {stack. addelement (newentry);} public t POP () {t top = NULL; If (! Isempty () {Top = stack. lastelement (); stack. removeelementat (stack. size ()-1);} return top;} private Boolean isempty () {return stack. isempty ();} public void display () {system. out. println ("vectorstack starts from the top element:"); For (INT I = stack. size ()-1; I> = 0; I --) {system. out. println (stack. elementat (I) ;}} public static void main (string [] ARGs) {vectorstack <integer> vectorstack = new vectorstack <integer> (); For (INT I = 0; I <100; I ++) {vectorstack. push (I); if (I % 2 = 0) {vectorstack. pop () ;}// end for vectorstack. display ();}}

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.