33/**
44 * This class implements a Stack using two different implementations.
55 * Stack is used with a regular array and Stack2 uses an ArrayList.
6- *
6+ *
77 * A stack is exactly what it sounds like. An element gets added to the top of
88 * the stack and only the element on the top may be removed. This is an example
99 * of an array implementation of a Stack. So an element can only be added/removed
1010 * from the end of the array. In theory stack have no fixed size, but with an
1111 * array implementation it does.
12- *
12+ *
1313 * @author Unknown
1414 *
1515 */
@@ -23,7 +23,7 @@ class Stack{
2323
2424 /**
2525 * Constructor
26- *
26+ *
2727 * @param size Size of the Stack
2828 */
2929 public Stack (int size ){
@@ -34,21 +34,21 @@ public Stack(int size){
3434
3535 /**
3636 * Adds an element to the top of the stack
37- *
37+ *
3838 * @param value The element added
3939 */
4040 public void push (int value ){
4141 if (!isFull ()){ //Checks for a full stack
4242 top ++;
4343 stackArray [top ] = value ;
4444 }else {
45- System .out .prinln ("The stack is full, can't insert value" );
45+ System .out .println ("The stack is full, can't insert value" );
4646 }
4747 }
4848
4949 /**
5050 * Removes the top element of the stack and returns the value you've removed
51- *
51+ *
5252 * @return value popped off the Stack
5353 */
5454 public int pop (){
@@ -62,7 +62,7 @@ public int pop(){
6262
6363 /**
6464 * Returns the element at the top of the stack
65- *
65+ *
6666 * @return element at the top of the stack
6767 */
6868 public int peek (){
@@ -76,7 +76,7 @@ public int peek(){
7676
7777 /**
7878 * Returns true if the stack is empty
79- *
79+ *
8080 * @return true if the stack is empty
8181 */
8282 public boolean isEmpty (){
@@ -85,16 +85,16 @@ public boolean isEmpty(){
8585
8686 /**
8787 * Returns true if the stack is full
88- *
88+ *
8989 * @return true if the stack is full
9090 */
9191 public boolean isFull (){
9292 return (top +1 == maxSize );
9393 }
94-
94+
9595 /**
9696 * Deletes everything in the Stack
97- *
97+ *
9898 * Doesn't delete elements in the array
9999 * but if you call push method after calling
100100 * makeEmpty it will overwrite previous
@@ -108,41 +108,41 @@ public void makeEmpty(){ //Doesn't delete elements in the array but if you call
108108/**
109109 * This is an ArrayList Implementation of stack, Where size is not
110110 * a problem we can extend the stack as much as we want.
111- *
111+ *
112112 * @author Unknown
113113 *
114114 */
115115class Stack2 {
116116 /** ArrayList representation of the stack */
117117 ArrayList <Integer > stackList ;
118-
118+
119119 /**
120120 * Constructor
121121 */
122122 Stack2 (){
123123 stackList =new ArrayList <>();
124124 }
125-
125+
126126 /**
127127 * Adds value to the end of list which
128128 * is the top for stack
129- *
129+ *
130130 * @param value value to be added
131131 */
132132 void push (int value ){
133133 stackList .add (value );
134134 }
135-
135+
136136 /**
137137 * Pops last element of list which is indeed
138138 * the top for Stack
139- *
139+ *
140140 * @return Element popped
141141 */
142142 int pop (){
143-
143+
144144 if (!isEmpty ()){ // checks for an empty Stack
145-
145+
146146 int popValue =stackList .get (stackList .size ()-1 );
147147 stackList .remove (stackList .size ()-1 ); //removes the poped element from the list
148148 return popValue ;
@@ -151,25 +151,25 @@ int pop(){
151151 System .out .print ("The stack is already empty " );
152152 return -1 ;
153153 }
154-
154+
155155 }
156-
156+
157157 /**
158158 * Checks for empty Stack
159- *
159+ *
160160 * @return true if stack is empty
161161 */
162162 boolean isEmpty (){
163163 if (stackList .isEmpty ())
164164 return true ;
165-
165+
166166 else return false ;
167-
167+
168168 }
169-
169+
170170 /**
171171 * Top element of stack
172- *
172+ *
173173 * @return top element of stack
174174 */
175175 int peek (){
@@ -179,14 +179,14 @@ int peek(){
179179
180180/**
181181 * This class implements the Stack and Stack2 created above
182- *
182+ *
183183 * @author Unknown
184184 *
185185 */
186186public class Stacks {
187187 /**
188188 * Main method
189- *
189+ *
190190 * @param args Command line arguments
191191 */
192192 public static void main (String args []){
@@ -196,21 +196,21 @@ public static void main(String args[]){
196196 myStack .push (8 );
197197 myStack .push (2 );
198198 myStack .push (9 );
199-
199+
200200 System .out .println ("*********************Stack Array Implementation*********************" );
201201 System .out .println (myStack .isEmpty ()); //will print false
202202 System .out .println (myStack .isFull ()); //will print true
203203 System .out .println (myStack .peek ()); //will print 9
204204 System .out .println (myStack .pop ()); //will print 9
205205 System .out .println (myStack .peek ()); // will print 2
206-
206+
207207 Stack2 myStack2 = new Stack2 (); //Declare a stack of maximum size 4
208208 //Populate the stack
209209 myStack2 .push (5 );
210210 myStack2 .push (8 );
211211 myStack2 .push (2 );
212212 myStack2 .push (9 );
213-
213+
214214 System .out .println ("*********************Stack List Implementation*********************" );
215215 System .out .println (myStack2 .isEmpty ()); //will print false
216216 System .out .println (myStack2 .peek ()); //will print 9
0 commit comments