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