mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-13 07:13:37 +08:00
Format code in StackArray
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* This class implements a Stack using a regular array.
|
* This class implements a Stack using a regular array.
|
||||||
*
|
* <p>
|
||||||
* A stack is exactly what it sounds like. An element gets added to the top of
|
* A stack is exactly what it sounds like. An element gets added to the top of
|
||||||
* the stack and only the element on the top may be removed. This is an example
|
* the stack and only the element on the top may be removed. This is an example
|
||||||
* of an array implementation of a Stack. So an element can only be added/removed
|
* of an array implementation of a Stack. So an element can only be added/removed
|
||||||
@ -8,139 +8,145 @@
|
|||||||
* array implementation it does.
|
* array implementation it does.
|
||||||
*
|
*
|
||||||
* @author Unknown
|
* @author Unknown
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class StackArray{
|
public class StackArray {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main method
|
* Main method
|
||||||
*
|
*
|
||||||
* @param args Command line arguments
|
* @param args Command line arguments
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
StackArray myStackArray = new StackArray(4); //Declare a stack of maximum size 4
|
// Declare a stack of maximum size 4
|
||||||
//Populate the stack
|
StackArray myStackArray = new StackArray(4);
|
||||||
myStackArray.push(5);
|
|
||||||
myStackArray.push(8);
|
|
||||||
myStackArray.push(2);
|
|
||||||
myStackArray.push(9);
|
|
||||||
|
|
||||||
System.out.println("*********************Stack Array Implementation*********************");
|
// Populate the stack
|
||||||
System.out.println(myStackArray.isEmpty()); //will print false
|
myStackArray.push(5);
|
||||||
System.out.println(myStackArray.isFull()); //will print true
|
myStackArray.push(8);
|
||||||
System.out.println(myStackArray.peek()); //will print 9
|
myStackArray.push(2);
|
||||||
System.out.println(myStackArray.pop()); //will print 9
|
myStackArray.push(9);
|
||||||
System.out.println(myStackArray.peek()); // will print 2
|
|
||||||
}
|
|
||||||
|
|
||||||
/** The max size of the Stack */
|
System.out.println("*********************Stack Array Implementation*********************");
|
||||||
private int maxSize;
|
System.out.println(myStackArray.isEmpty()); // will print false
|
||||||
|
System.out.println(myStackArray.isFull()); // will print true
|
||||||
/** The array representation of the Stack */
|
System.out.println(myStackArray.peek()); // will print 9
|
||||||
private int[] stackArray;
|
System.out.println(myStackArray.pop()); // will print 9
|
||||||
|
System.out.println(myStackArray.peek()); // will print 2
|
||||||
/** The top of the stack */
|
|
||||||
private int top;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
*
|
|
||||||
* @param size Size of the Stack
|
|
||||||
*/
|
|
||||||
public StackArray(int size){
|
|
||||||
maxSize = size;
|
|
||||||
stackArray = new int[maxSize];
|
|
||||||
top = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds an element to the top of the stack
|
|
||||||
*
|
|
||||||
* @param value The element added
|
|
||||||
*/
|
|
||||||
public void push(int value){
|
|
||||||
if(!isFull()){ //Checks for a full stack
|
|
||||||
top++;
|
|
||||||
stackArray[top] = value;
|
|
||||||
}else{
|
|
||||||
resize(maxSize*2);
|
|
||||||
push(value);// don't forget push after resizing
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes the top element of the stack and returns the value you've removed
|
|
||||||
*
|
|
||||||
* @return value popped off the Stack
|
|
||||||
*/
|
|
||||||
public int pop(){
|
|
||||||
if(!isEmpty()){ //Checks for an empty stack
|
|
||||||
return stackArray[top--];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(top < maxSize/4){
|
/**
|
||||||
resize(maxSize/2);
|
* The max size of the Stack
|
||||||
return pop();// don't forget pop after resizing
|
*/
|
||||||
|
private int maxSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The array representation of the Stack
|
||||||
|
*/
|
||||||
|
private int[] stackArray;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The top of the stack
|
||||||
|
*/
|
||||||
|
private int top;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param size Size of the Stack
|
||||||
|
*/
|
||||||
|
public StackArray(int size) {
|
||||||
|
maxSize = size;
|
||||||
|
stackArray = new int[maxSize];
|
||||||
|
top = -1;
|
||||||
}
|
}
|
||||||
else{
|
|
||||||
System.out.println("The stack is already empty");
|
/**
|
||||||
return -1;
|
* Adds an element to the top of the stack
|
||||||
|
*
|
||||||
|
* @param value The element added
|
||||||
|
*/
|
||||||
|
public void push(int value) {
|
||||||
|
if (!isFull()) { // Checks for a full stack
|
||||||
|
top++;
|
||||||
|
stackArray[top] = value;
|
||||||
|
} else {
|
||||||
|
resize(maxSize * 2);
|
||||||
|
push(value); // don't forget push after resizing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the element at the top of the stack
|
* Removes the top element of the stack and returns the value you've removed
|
||||||
*
|
*
|
||||||
* @return element at the top of the stack
|
* @return value popped off the Stack
|
||||||
*/
|
*/
|
||||||
public int peek(){
|
public int pop() {
|
||||||
if(!isEmpty()){ //Checks for an empty stack
|
if (!isEmpty()) { // Checks for an empty stack
|
||||||
return stackArray[top];
|
return stackArray[top--];
|
||||||
}else{
|
}
|
||||||
System.out.println("The stack is empty, cant peek");
|
|
||||||
return -1;
|
if (top < maxSize / 4) {
|
||||||
|
resize(maxSize / 2);
|
||||||
|
return pop();// don't forget pop after resizing
|
||||||
|
} else {
|
||||||
|
System.out.println("The stack is already empty");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void resize(int newSize){
|
/**
|
||||||
//private int[] transferArray = new int[newSize]; we can't put modifires here !
|
* Returns the element at the top of the stack
|
||||||
int[] transferArray = new int[newSize];
|
*
|
||||||
|
* @return element at the top of the stack
|
||||||
//for(int i = 0; i < stackArray.length(); i++){ the length isn't a method .
|
*/
|
||||||
for(int i = 0; i < stackArray.length; i++){
|
public int peek() {
|
||||||
transferArray[i] = stackArray[i];
|
if (!isEmpty()) { // Checks for an empty stack
|
||||||
stackArray = transferArray;
|
return stackArray[top];
|
||||||
|
} else {
|
||||||
|
System.out.println("The stack is empty, cant peek");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
maxSize = newSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
private void resize(int newSize) {
|
||||||
* Returns true if the stack is empty
|
// private int[] transferArray = new int[newSize]; we can't put modifiers here !
|
||||||
*
|
int[] transferArray = new int[newSize];
|
||||||
* @return true if the stack is empty
|
|
||||||
*/
|
|
||||||
public boolean isEmpty(){
|
|
||||||
return(top == -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// for(int i = 0; i < stackArray.length(); i++){ the length isn't a method .
|
||||||
* Returns true if the stack is full
|
for (int i = 0; i < stackArray.length; i++) {
|
||||||
*
|
transferArray[i] = stackArray[i];
|
||||||
* @return true if the stack is full
|
stackArray = transferArray;
|
||||||
*/
|
}
|
||||||
public boolean isFull(){
|
maxSize = newSize;
|
||||||
return(top+1 == maxSize);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes everything in the Stack
|
* Returns true if the stack is empty
|
||||||
*
|
*
|
||||||
* Doesn't delete elements in the array
|
* @return true if the stack is empty
|
||||||
* but if you call push method after calling
|
*/
|
||||||
* makeEmpty it will overwrite previous
|
public boolean isEmpty() {
|
||||||
* values
|
return (top == -1);
|
||||||
*/
|
}
|
||||||
public void makeEmpty(){ //Doesn't delete elements in the array but if you call
|
|
||||||
top = -1; //push method after calling makeEmpty it will overwrite previous values
|
/**
|
||||||
}
|
* Returns true if the stack is full
|
||||||
}
|
*
|
||||||
|
* @return true if the stack is full
|
||||||
|
*/
|
||||||
|
public boolean isFull() {
|
||||||
|
return (top + 1 == maxSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes everything in the Stack
|
||||||
|
* <p>
|
||||||
|
* Doesn't delete elements in the array
|
||||||
|
* but if you call push method after calling
|
||||||
|
* makeEmpty it will overwrite previous
|
||||||
|
* values
|
||||||
|
*/
|
||||||
|
public void makeEmpty() { // Doesn't delete elements in the array but if you call
|
||||||
|
top = -1; // push method after calling makeEmpty it will overwrite previous values
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user