Skip to content

Commit d6b6012

Browse files
Merge pull request #10 from yash-b98/patch-2
Stack List Implementation
2 parents 5427f0d + 23a099d commit d6b6012

File tree

1 file changed

+75
-14
lines changed

1 file changed

+75
-14
lines changed

data_structures/Stacks.java

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,81 @@ public void makeEmpty(){ //Doesn't delete elements in the array but if you call
3838
top = -1; //push method after calling makeEmpty it will overwrite previous values
3939
}
4040
}
41+
42+
43+
/* This is ArrayList Implementation of stack , Where size is not
44+
a problem we can extend the stack as much as we want*/
45+
class Stack2{
46+
47+
48+
ArrayList<Integer> stackList;
49+
50+
Stack2(){ //constructor
51+
stackList=new ArrayList<>();
52+
}
53+
54+
55+
void push(int value){ //adds value to the end of list which is the top for stack
56+
stackList.add(value);
57+
}
58+
59+
int pop(){ //pops last element of list which is indeed the top for Stack
60+
61+
if(!isEmpty()){ // checks for an empty Stack
62+
63+
int popValue=stackList.get(stackList.size()-1);
64+
stackList.remove(stackList.size()-1); //removes the poped element from the list
65+
return popValue;
66+
}
67+
else{
68+
System.out.print("The stack is already empty ");
69+
return -1;
70+
}
71+
72+
}
73+
74+
boolean isEmpty(){ //checks for empty Stack
75+
if(stackList.isEmpty())
76+
return true;
77+
78+
else return false;
79+
80+
}
81+
82+
int peek(){ //top element of stack
83+
return stackList.get(stackList.size()-1);
84+
}
85+
}
86+
4187
//Example
4288
public class Stacks{
4389
public static void main(String args[]){
44-
Stack myStack = new Stack(4); //Declare a stack of maximum size 4
45-
//Populate the stack
46-
myStack.push(5);
47-
myStack.push(8);
48-
myStack.push(2);
49-
myStack.push(9);
50-
51-
System.out.println(myStack.isEmpty()); //will print false
52-
System.out.println(myStack.isFull()); //will print true
53-
System.out.println(myStack.peek()); //will print 9
54-
System.out.println(myStack.pop()); //will print 9
55-
System.out.println(myStack.peek()); // will print 2
56-
}
57-
}
90+
Stack myStack = new Stack(4); //Declare a stack of maximum size 4
91+
//Populate the stack
92+
myStack.push(5);
93+
myStack.push(8);
94+
myStack.push(2);
95+
myStack.push(9);
96+
97+
System.out.println("*********************Stack Array Implementation*********************");
98+
System.out.println(myStack.isEmpty()); //will print false
99+
System.out.println(myStack.isFull()); //will print true
100+
System.out.println(myStack.peek()); //will print 9
101+
System.out.println(myStack.pop()); //will print 9
102+
System.out.println(myStack.peek()); // will print 2
103+
104+
Stack2 myStack2 = new Stack2(); //Declare a stack of maximum size 4
105+
//Populate the stack
106+
myStack2.push(5);
107+
myStack2.push(8);
108+
myStack2.push(2);
109+
myStack2.push(9);
110+
111+
System.out.println("*********************Stack List Implementation*********************");
112+
System.out.println(myStack2.isEmpty()); //will print false
113+
System.out.println(myStack2.peek()); //will print 9
114+
System.out.println(myStack2.pop()); //will print 9
115+
System.out.println(myStack2.peek()); // will print 2
116+
System.out.println(myStack2.pop()); //will print 2
117+
}
118+
}

0 commit comments

Comments
 (0)