Skip to content

Commit 7c33d2e

Browse files
authored
Update StackArrayList.java
1 parent fc64d05 commit 7c33d2e

File tree

1 file changed

+73
-72
lines changed

1 file changed

+73
-72
lines changed

DataStructures/Stacks/StackArrayList.java

+73-72
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,94 @@
22

33
/**
44
* This class implements a Stack using an ArrayList.
5-
*
5+
* <p>
66
* A stack is exactly what it sounds like. An element gets added to the top of
77
* the stack and only the element on the top may be removed.
8-
*
8+
* <p>
99
* This is an ArrayList Implementation of a stack, where size is not
1010
* a problem we can extend the stack as much as we want.
1111
*
1212
* @author Unknown
13-
*
1413
*/
15-
public class StackArrayList{
14+
public class StackArrayList {
1615

17-
/**
18-
* Main method
19-
*
20-
* @param args Command line arguments
21-
*/
22-
public static void main(String[] args) {
23-
StackArrayList myStackArrayList = new StackArrayList(); //Declare a stack of maximum size 4
24-
//Populate the stack
25-
myStackArrayList.push(5);
26-
myStackArrayList.push(8);
27-
myStackArrayList.push(2);
28-
myStackArrayList.push(9);
16+
/**
17+
* Main method
18+
*
19+
* @param args Command line arguments
20+
*/
21+
public static void main(String[] args) {
22+
23+
StackArrayList myStackArrayList = new StackArrayList();
24+
25+
myStackArrayList.push(5);
26+
myStackArrayList.push(8);
27+
myStackArrayList.push(2);
28+
myStackArrayList.push(9);
2929

30-
System.out.println("*********************Stack List Implementation*********************");
31-
System.out.println(myStackArrayList.isEmpty()); //will print false
32-
System.out.println(myStackArrayList.peek()); //will print 9
33-
System.out.println(myStackArrayList.pop()); //will print 9
34-
System.out.println(myStackArrayList.peek()); // will print 2
35-
System.out.println(myStackArrayList.pop()); //will print 2
36-
}
30+
System.out.println("*********************Stack List Implementation*********************");
31+
System.out.println(myStackArrayList.isEmpty()); // will print false
32+
System.out.println(myStackArrayList.peek()); // will print 9
33+
System.out.println(myStackArrayList.pop()); // will print 9
34+
System.out.println(myStackArrayList.peek()); // will print 2
35+
System.out.println(myStackArrayList.pop()); // will print 2
36+
}
3737

38-
/** ArrayList representation of the stack */
39-
private ArrayList<Integer> stackList;
38+
/**
39+
* ArrayList representation of the stack
40+
*/
41+
private ArrayList<Integer> stackList;
4042

41-
/**
42-
* Constructor
43-
*/
44-
public StackArrayList(){
45-
stackList = new ArrayList<>();
46-
}
43+
/**
44+
* Constructor
45+
*/
46+
public StackArrayList() {
47+
stackList = new ArrayList<>();
48+
}
4749

48-
/**
49-
* Adds value to the end of list which
50-
* is the top for stack
51-
*
52-
* @param value value to be added
53-
*/
54-
public void push(int value){
55-
stackList.add(value);
56-
}
50+
/**
51+
* Adds value to the end of list which
52+
* is the top for stack
53+
*
54+
* @param value value to be added
55+
*/
56+
public void push(int value) {
57+
stackList.add(value);
58+
}
5759

58-
/**
59-
* Pops last element of list which is indeed
60-
* the top for Stack
61-
*
62-
* @return Element popped
63-
*/
64-
public int pop(){
60+
/**
61+
* Pops last element of list which is indeed
62+
* the top for Stack
63+
*
64+
* @return Element popped
65+
*/
66+
public int pop() {
6567

66-
if(!isEmpty()){ // checks for an empty Stack
68+
if (!isEmpty()) { // checks for an empty Stack
69+
int popValue = stackList.get(stackList.size() - 1);
70+
stackList.remove(stackList.size() - 1); // removes the poped element from the list
71+
return popValue;
72+
}
6773

68-
int popValue=stackList.get(stackList.size()-1);
69-
stackList.remove(stackList.size()-1); //removes the poped element from the list
70-
return popValue;
74+
System.out.print("The stack is already empty!");
75+
return -1;
7176
}
7277

73-
System.out.print("The stack is already empty ");
74-
return -1;
75-
}
76-
77-
/**
78-
* Checks for empty Stack
79-
*
80-
* @return true if stack is empty
81-
*/
82-
public boolean isEmpty(){
83-
return stackList.isEmpty();
84-
}
78+
/**
79+
* Checks for empty Stack
80+
*
81+
* @return true if stack is empty
82+
*/
83+
public boolean isEmpty() {
84+
return stackList.isEmpty();
85+
}
8586

86-
/**
87-
* Top element of stack
88-
*
89-
* @return top element of stack
90-
*/
91-
public int peek(){
92-
return stackList.get(stackList.size()-1);
93-
}
94-
}
87+
/**
88+
* Top element of stack
89+
*
90+
* @return top element of stack
91+
*/
92+
public int peek() {
93+
return stackList.get(stackList.size() - 1);
94+
}
95+
}

0 commit comments

Comments
 (0)