This example for you to share the Java implementation of a simple stack of the specific code for your reference, the specific contents are as follows
/**
* Created by Frank */public
class Toystack {
/**
* Stack maximum depth
**/
protected int max_depth = Ten;
/**
* Current Depth of stack * *
protected int depth = 0;
/**
* Actual stack *
/protected int[] stack = new int[max_depth];
/**
* Push, add an element to the stack
*
* @param n to add the integer *
*
protected void push (int n) {
if depth = = Max_d EPTH-1) {
throw new RuntimeException ("The stack is full and no more elements can be added.") ");
}
stack[depth++] = n;
}
/**
* Pop, return top element of stack and remove from stack
*
* @return stack top element
/protected int pop () {
if (depth = 0) {
throw new RuntimeException ("The elements in the stack have been taken out and cannot be fetched.") ");
}
--depth,dept subtracts 1 and assigns a value to the variable dept, so that the depth of the stack is reduced by 1 (equivalent to removing from the stack). return
stack[--depth];
}
/**
* Peek, return top of stack element but not remove from stack
*
* @return
/
protected int peek () {
if (depth = 0) {
throw new RuntimeException ("The elements in the stack have been taken out and cannot be fetched.") ");
}
return stack[depth-1];
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.