commit
d6b60123ef
@ -38,6 +38,52 @@ class Stack{
|
|||||||
top = -1; //push method after calling makeEmpty it will overwrite previous values
|
top = -1; //push method after calling makeEmpty it will overwrite previous values
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* This is ArrayList Implementation of stack , Where size is not
|
||||||
|
a problem we can extend the stack as much as we want*/
|
||||||
|
class Stack2{
|
||||||
|
|
||||||
|
|
||||||
|
ArrayList<Integer> stackList;
|
||||||
|
|
||||||
|
Stack2(){ //constructor
|
||||||
|
stackList=new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void push(int value){ //adds value to the end of list which is the top for stack
|
||||||
|
stackList.add(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
int pop(){ //pops last element of list which is indeed the top for Stack
|
||||||
|
|
||||||
|
if(!isEmpty()){ // checks for an empty Stack
|
||||||
|
|
||||||
|
int popValue=stackList.get(stackList.size()-1);
|
||||||
|
stackList.remove(stackList.size()-1); //removes the poped element from the list
|
||||||
|
return popValue;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
System.out.print("The stack is already empty ");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean isEmpty(){ //checks for empty Stack
|
||||||
|
if(stackList.isEmpty())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
else return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int peek(){ //top element of stack
|
||||||
|
return stackList.get(stackList.size()-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Example
|
//Example
|
||||||
public class Stacks{
|
public class Stacks{
|
||||||
public static void main(String args[]){
|
public static void main(String args[]){
|
||||||
@ -48,10 +94,25 @@ public class Stacks{
|
|||||||
myStack.push(2);
|
myStack.push(2);
|
||||||
myStack.push(9);
|
myStack.push(9);
|
||||||
|
|
||||||
|
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
|
||||||
|
//Populate the stack
|
||||||
|
myStack2.push(5);
|
||||||
|
myStack2.push(8);
|
||||||
|
myStack2.push(2);
|
||||||
|
myStack2.push(9);
|
||||||
|
|
||||||
|
System.out.println("*********************Stack List Implementation*********************");
|
||||||
|
System.out.println(myStack2.isEmpty()); //will print false
|
||||||
|
System.out.println(myStack2.peek()); //will print 9
|
||||||
|
System.out.println(myStack2.pop()); //will print 9
|
||||||
|
System.out.println(myStack2.peek()); // will print 2
|
||||||
|
System.out.println(myStack2.pop()); //will print 2
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user