3
3
/**
4
4
* This class implements a Stack using two different implementations.
5
5
* Stack is used with a regular array and Stack2 uses an ArrayList.
6
- *
6
+ *
7
7
* A stack is exactly what it sounds like. An element gets added to the top of
8
8
* the stack and only the element on the top may be removed. This is an example
9
9
* of an array implementation of a Stack. So an element can only be added/removed
10
10
* from the end of the array. In theory stack have no fixed size, but with an
11
11
* array implementation it does.
12
- *
12
+ *
13
13
* @author Unknown
14
14
*
15
15
*/
@@ -23,7 +23,7 @@ class Stack{
23
23
24
24
/**
25
25
* Constructor
26
- *
26
+ *
27
27
* @param size Size of the Stack
28
28
*/
29
29
public Stack (int size ){
@@ -34,21 +34,21 @@ public Stack(int size){
34
34
35
35
/**
36
36
* Adds an element to the top of the stack
37
- *
37
+ *
38
38
* @param value The element added
39
39
*/
40
40
public void push (int value ){
41
41
if (!isFull ()){ //Checks for a full stack
42
42
top ++;
43
43
stackArray [top ] = value ;
44
44
}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" );
46
46
}
47
47
}
48
48
49
49
/**
50
50
* Removes the top element of the stack and returns the value you've removed
51
- *
51
+ *
52
52
* @return value popped off the Stack
53
53
*/
54
54
public int pop (){
@@ -62,7 +62,7 @@ public int pop(){
62
62
63
63
/**
64
64
* Returns the element at the top of the stack
65
- *
65
+ *
66
66
* @return element at the top of the stack
67
67
*/
68
68
public int peek (){
@@ -76,7 +76,7 @@ public int peek(){
76
76
77
77
/**
78
78
* Returns true if the stack is empty
79
- *
79
+ *
80
80
* @return true if the stack is empty
81
81
*/
82
82
public boolean isEmpty (){
@@ -85,16 +85,16 @@ public boolean isEmpty(){
85
85
86
86
/**
87
87
* Returns true if the stack is full
88
- *
88
+ *
89
89
* @return true if the stack is full
90
90
*/
91
91
public boolean isFull (){
92
92
return (top +1 == maxSize );
93
93
}
94
-
94
+
95
95
/**
96
96
* Deletes everything in the Stack
97
- *
97
+ *
98
98
* Doesn't delete elements in the array
99
99
* but if you call push method after calling
100
100
* makeEmpty it will overwrite previous
@@ -108,41 +108,41 @@ public void makeEmpty(){ //Doesn't delete elements in the array but if you call
108
108
/**
109
109
* This is an ArrayList Implementation of stack, Where size is not
110
110
* a problem we can extend the stack as much as we want.
111
- *
111
+ *
112
112
* @author Unknown
113
113
*
114
114
*/
115
115
class Stack2 {
116
116
/** ArrayList representation of the stack */
117
117
ArrayList <Integer > stackList ;
118
-
118
+
119
119
/**
120
120
* Constructor
121
121
*/
122
122
Stack2 (){
123
123
stackList =new ArrayList <>();
124
124
}
125
-
125
+
126
126
/**
127
127
* Adds value to the end of list which
128
128
* is the top for stack
129
- *
129
+ *
130
130
* @param value value to be added
131
131
*/
132
132
void push (int value ){
133
133
stackList .add (value );
134
134
}
135
-
135
+
136
136
/**
137
137
* Pops last element of list which is indeed
138
138
* the top for Stack
139
- *
139
+ *
140
140
* @return Element popped
141
141
*/
142
142
int pop (){
143
-
143
+
144
144
if (!isEmpty ()){ // checks for an empty Stack
145
-
145
+
146
146
int popValue =stackList .get (stackList .size ()-1 );
147
147
stackList .remove (stackList .size ()-1 ); //removes the poped element from the list
148
148
return popValue ;
@@ -151,25 +151,25 @@ int pop(){
151
151
System .out .print ("The stack is already empty " );
152
152
return -1 ;
153
153
}
154
-
154
+
155
155
}
156
-
156
+
157
157
/**
158
158
* Checks for empty Stack
159
- *
159
+ *
160
160
* @return true if stack is empty
161
161
*/
162
162
boolean isEmpty (){
163
163
if (stackList .isEmpty ())
164
164
return true ;
165
-
165
+
166
166
else return false ;
167
-
167
+
168
168
}
169
-
169
+
170
170
/**
171
171
* Top element of stack
172
- *
172
+ *
173
173
* @return top element of stack
174
174
*/
175
175
int peek (){
@@ -179,14 +179,14 @@ int peek(){
179
179
180
180
/**
181
181
* This class implements the Stack and Stack2 created above
182
- *
182
+ *
183
183
* @author Unknown
184
184
*
185
185
*/
186
186
public class Stacks {
187
187
/**
188
188
* Main method
189
- *
189
+ *
190
190
* @param args Command line arguments
191
191
*/
192
192
public static void main (String args []){
@@ -196,21 +196,21 @@ public static void main(String args[]){
196
196
myStack .push (8 );
197
197
myStack .push (2 );
198
198
myStack .push (9 );
199
-
199
+
200
200
System .out .println ("*********************Stack Array Implementation*********************" );
201
201
System .out .println (myStack .isEmpty ()); //will print false
202
202
System .out .println (myStack .isFull ()); //will print true
203
203
System .out .println (myStack .peek ()); //will print 9
204
204
System .out .println (myStack .pop ()); //will print 9
205
205
System .out .println (myStack .peek ()); // will print 2
206
-
206
+
207
207
Stack2 myStack2 = new Stack2 (); //Declare a stack of maximum size 4
208
208
//Populate the stack
209
209
myStack2 .push (5 );
210
210
myStack2 .push (8 );
211
211
myStack2 .push (2 );
212
212
myStack2 .push (9 );
213
-
213
+
214
214
System .out .println ("*********************Stack List Implementation*********************" );
215
215
System .out .println (myStack2 .isEmpty ()); //will print false
216
216
System .out .println (myStack2 .peek ()); //will print 9
0 commit comments