Stack of Java Data structures

Source: Internet
Author: User

Tag:java    data structure     stack    

Stack of package com.xingej.algorithm.datastructure.stack;/** *  data Structures stack *  *  Type long as test case  *  *  @author  erjun 2017 December 4   PM 10:22:34 */public  class longstack {    //  underlying data storage     private long[]  arr;    //  maximum number of elements     private int maxSize;     //  pointer to current element     private int top;     Public longstack (int maxsize)  {        this.maxsize  = maxSize;        arr = new long[maxSize];         top = -1;//  default value is-1, no element in stack      }    //  Add Data     public void push (Long value)  {        arr[++top] = value;    }    //  View, and delete element data     public long pop ()  {         return arr[top--]; //  returns first, then the pointer is reduced by one     }    //   Just view element     public long peek ()  {         return arr[top];    }    //  See if the current stack space is empty      public boolean isempty ()  {         return top == -1;    }    //  See if the current stack space is full      public boolean isfull ()  {         return top ==  (maxsize - 1);     }}


Test Case:

package com.xingej.algorithm.datastructure.stack;import org.junit.test;/** *  Stack features: advanced after out; The difference between a  *  *  stack and a queue? The main composition of the  *  *  stack: a container for data storage, an array, or a linked list; a pointer; Other API behaviors are all around the container  *  *  The main composition of a queue: a container for data storage, an array of arrays, or a list of two pointers.  *  *  is not suitable for large amounts of storage, just a means of implementing some sort of algorithm., *  *  Restricted access Mode  *  * @ author erjun 2017 December 6   Morning 9:11:40 */public class longstacktest {      @Test     public void test ()  {         longstack thestack = new longstack (Ten);         thestack.push (        thestack.push); (2);         thestack.push (9);         thestack.push (5);        while  (!theStaCk.isempty ())  {            system.out.print ( Thestack.pop ()  +  " ");        }         system.out.println ();     }}


The code has been uploaded to Git:

Https://github.com/xej520/xingej-algorithm












Stack of Java Data structures

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.