mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 10:15:51 +08:00
Syntax fix
This commit is contained in:
BIN
data_structures/Stacks/Stacks.class
Normal file
BIN
data_structures/Stacks/Stacks.class
Normal file
Binary file not shown.
@ -3,13 +3,13 @@ import java.util.ArrayList;
|
|||||||
/**
|
/**
|
||||||
* This class implements a Stack using two different implementations.
|
* This class implements a Stack using two different implementations.
|
||||||
* Stack is used with a regular array and Stack2 uses an ArrayList.
|
* Stack is used with a regular array and Stack2 uses an ArrayList.
|
||||||
*
|
*
|
||||||
* 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
|
||||||
* from the end of the array. In theory stack have no fixed size, but with an
|
* from the end of the array. In theory stack have no fixed size, but with an
|
||||||
* array implementation it does.
|
* array implementation it does.
|
||||||
*
|
*
|
||||||
* @author Unknown
|
* @author Unknown
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@ -23,7 +23,7 @@ class Stack{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
* @param size Size of the Stack
|
* @param size Size of the Stack
|
||||||
*/
|
*/
|
||||||
public Stack(int size){
|
public Stack(int size){
|
||||||
@ -34,7 +34,7 @@ class Stack{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an element to the top of the stack
|
* Adds an element to the top of the stack
|
||||||
*
|
*
|
||||||
* @param value The element added
|
* @param value The element added
|
||||||
*/
|
*/
|
||||||
public void push(int value){
|
public void push(int value){
|
||||||
@ -42,13 +42,13 @@ class Stack{
|
|||||||
top++;
|
top++;
|
||||||
stackArray[top] = value;
|
stackArray[top] = value;
|
||||||
}else{
|
}else{
|
||||||
System.out.prinln("The stack is full, can't insert value");
|
System.out.println("The stack is full, can't insert value");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the top element of the stack and returns the value you've removed
|
* Removes the top element of the stack and returns the value you've removed
|
||||||
*
|
*
|
||||||
* @return value popped off the Stack
|
* @return value popped off the Stack
|
||||||
*/
|
*/
|
||||||
public int pop(){
|
public int pop(){
|
||||||
@ -62,7 +62,7 @@ class Stack{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the element at the top of the stack
|
* Returns the element at the top of the stack
|
||||||
*
|
*
|
||||||
* @return element at the top of the stack
|
* @return element at the top of the stack
|
||||||
*/
|
*/
|
||||||
public int peek(){
|
public int peek(){
|
||||||
@ -76,7 +76,7 @@ class Stack{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the stack is empty
|
* Returns true if the stack is empty
|
||||||
*
|
*
|
||||||
* @return true if the stack is empty
|
* @return true if the stack is empty
|
||||||
*/
|
*/
|
||||||
public boolean isEmpty(){
|
public boolean isEmpty(){
|
||||||
@ -85,16 +85,16 @@ class Stack{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the stack is full
|
* Returns true if the stack is full
|
||||||
*
|
*
|
||||||
* @return true if the stack is full
|
* @return true if the stack is full
|
||||||
*/
|
*/
|
||||||
public boolean isFull(){
|
public boolean isFull(){
|
||||||
return(top+1 == maxSize);
|
return(top+1 == maxSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes everything in the Stack
|
* Deletes everything in the Stack
|
||||||
*
|
*
|
||||||
* Doesn't delete elements in the array
|
* Doesn't delete elements in the array
|
||||||
* but if you call push method after calling
|
* but if you call push method after calling
|
||||||
* makeEmpty it will overwrite previous
|
* makeEmpty it will overwrite previous
|
||||||
@ -108,41 +108,41 @@ class Stack{
|
|||||||
/**
|
/**
|
||||||
* This is an ArrayList Implementation of stack, Where size is not
|
* This is an ArrayList Implementation of stack, Where size is not
|
||||||
* a problem we can extend the stack as much as we want.
|
* a problem we can extend the stack as much as we want.
|
||||||
*
|
*
|
||||||
* @author Unknown
|
* @author Unknown
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class Stack2{
|
class Stack2{
|
||||||
/** ArrayList representation of the stack */
|
/** ArrayList representation of the stack */
|
||||||
ArrayList<Integer> stackList;
|
ArrayList<Integer> stackList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*/
|
*/
|
||||||
Stack2(){
|
Stack2(){
|
||||||
stackList=new ArrayList<>();
|
stackList=new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds value to the end of list which
|
* Adds value to the end of list which
|
||||||
* is the top for stack
|
* is the top for stack
|
||||||
*
|
*
|
||||||
* @param value value to be added
|
* @param value value to be added
|
||||||
*/
|
*/
|
||||||
void push(int value){
|
void push(int value){
|
||||||
stackList.add(value);
|
stackList.add(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pops last element of list which is indeed
|
* Pops last element of list which is indeed
|
||||||
* the top for Stack
|
* the top for Stack
|
||||||
*
|
*
|
||||||
* @return Element popped
|
* @return Element popped
|
||||||
*/
|
*/
|
||||||
int pop(){
|
int pop(){
|
||||||
|
|
||||||
if(!isEmpty()){ // checks for an empty Stack
|
if(!isEmpty()){ // checks for an empty Stack
|
||||||
|
|
||||||
int popValue=stackList.get(stackList.size()-1);
|
int popValue=stackList.get(stackList.size()-1);
|
||||||
stackList.remove(stackList.size()-1); //removes the poped element from the list
|
stackList.remove(stackList.size()-1); //removes the poped element from the list
|
||||||
return popValue;
|
return popValue;
|
||||||
@ -151,25 +151,25 @@ class Stack2{
|
|||||||
System.out.print("The stack is already empty ");
|
System.out.print("The stack is already empty ");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks for empty Stack
|
* Checks for empty Stack
|
||||||
*
|
*
|
||||||
* @return true if stack is empty
|
* @return true if stack is empty
|
||||||
*/
|
*/
|
||||||
boolean isEmpty(){
|
boolean isEmpty(){
|
||||||
if(stackList.isEmpty())
|
if(stackList.isEmpty())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
else return false;
|
else return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Top element of stack
|
* Top element of stack
|
||||||
*
|
*
|
||||||
* @return top element of stack
|
* @return top element of stack
|
||||||
*/
|
*/
|
||||||
int peek(){
|
int peek(){
|
||||||
@ -179,14 +179,14 @@ class Stack2{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This class implements the Stack and Stack2 created above
|
* This class implements the Stack and Stack2 created above
|
||||||
*
|
*
|
||||||
* @author Unknown
|
* @author Unknown
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Stacks{
|
public class Stacks{
|
||||||
/**
|
/**
|
||||||
* 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[]){
|
||||||
@ -196,21 +196,21 @@ public class Stacks{
|
|||||||
myStack.push(8);
|
myStack.push(8);
|
||||||
myStack.push(2);
|
myStack.push(2);
|
||||||
myStack.push(9);
|
myStack.push(9);
|
||||||
|
|
||||||
System.out.println("*********************Stack Array Implementation*********************");
|
System.out.println("*********************Stack Array Implementation*********************");
|
||||||
System.out.println(myStack.isEmpty()); //will print false
|
System.out.println(myStack.isEmpty()); //will print false
|
||||||
System.out.println(myStack.isFull()); //will print true
|
System.out.println(myStack.isFull()); //will print true
|
||||||
System.out.println(myStack.peek()); //will print 9
|
System.out.println(myStack.peek()); //will print 9
|
||||||
System.out.println(myStack.pop()); //will print 9
|
System.out.println(myStack.pop()); //will print 9
|
||||||
System.out.println(myStack.peek()); // will print 2
|
System.out.println(myStack.peek()); // will print 2
|
||||||
|
|
||||||
Stack2 myStack2 = new Stack2(); //Declare a stack of maximum size 4
|
Stack2 myStack2 = new Stack2(); //Declare a stack of maximum size 4
|
||||||
//Populate the stack
|
//Populate the stack
|
||||||
myStack2.push(5);
|
myStack2.push(5);
|
||||||
myStack2.push(8);
|
myStack2.push(8);
|
||||||
myStack2.push(2);
|
myStack2.push(2);
|
||||||
myStack2.push(9);
|
myStack2.push(9);
|
||||||
|
|
||||||
System.out.println("*********************Stack List Implementation*********************");
|
System.out.println("*********************Stack List Implementation*********************");
|
||||||
System.out.println(myStack2.isEmpty()); //will print false
|
System.out.println(myStack2.isEmpty()); //will print false
|
||||||
System.out.println(myStack2.peek()); //will print 9
|
System.out.println(myStack2.peek()); //will print 9
|
||||||
|
Reference in New Issue
Block a user