Skip to content

Commit 8d11e5b

Browse files
authored
Merge pull request TheAlgorithms#672 from hayderhassan/separate-stacks-class to fix TheAlgorithms#667
Separate stacks class
2 parents b9aacd4 + 7c33d2e commit 8d11e5b

5 files changed

Lines changed: 250 additions & 242 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/**
2+
* This class implements a Stack using a regular array.
3+
* <p>
4+
* A stack is exactly what it sounds like. An element gets added to the top of
5+
* the stack and only the element on the top may be removed. This is an example
6+
* of an array implementation of a Stack. So an element can only be added/removed
7+
* from the end of the array. In theory stack have no fixed size, but with an
8+
* array implementation it does.
9+
*
10+
* @author Unknown
11+
*/
12+
public class StackArray {
13+
14+
/**
15+
* Main method
16+
*
17+
* @param args Command line arguments
18+
*/
19+
public static void main(String[] args) {
20+
// Declare a stack of maximum size 4
21+
StackArray myStackArray = new StackArray(4);
22+
23+
// Populate the stack
24+
myStackArray.push(5);
25+
myStackArray.push(8);
26+
myStackArray.push(2);
27+
myStackArray.push(9);
28+
29+
System.out.println("*********************Stack Array Implementation*********************");
30+
System.out.println(myStackArray.isEmpty()); // will print false
31+
System.out.println(myStackArray.isFull()); // will print true
32+
System.out.println(myStackArray.peek()); // will print 9
33+
System.out.println(myStackArray.pop()); // will print 9
34+
System.out.println(myStackArray.peek()); // will print 2
35+
}
36+
37+
/**
38+
* The max size of the Stack
39+
*/
40+
private int maxSize;
41+
42+
/**
43+
* The array representation of the Stack
44+
*/
45+
private int[] stackArray;
46+
47+
/**
48+
* The top of the stack
49+
*/
50+
private int top;
51+
52+
/**
53+
* Constructor
54+
*
55+
* @param size Size of the Stack
56+
*/
57+
public StackArray(int size) {
58+
maxSize = size;
59+
stackArray = new int[maxSize];
60+
top = -1;
61+
}
62+
63+
/**
64+
* Adds an element to the top of the stack
65+
*
66+
* @param value The element added
67+
*/
68+
public void push(int value) {
69+
if (!isFull()) { // Checks for a full stack
70+
top++;
71+
stackArray[top] = value;
72+
} else {
73+
resize(maxSize * 2);
74+
push(value); // don't forget push after resizing
75+
}
76+
}
77+
78+
/**
79+
* Removes the top element of the stack and returns the value you've removed
80+
*
81+
* @return value popped off the Stack
82+
*/
83+
public int pop() {
84+
if (!isEmpty()) { // Checks for an empty stack
85+
return stackArray[top--];
86+
}
87+
88+
if (top < maxSize / 4) {
89+
resize(maxSize / 2);
90+
return pop();// don't forget pop after resizing
91+
} else {
92+
System.out.println("The stack is already empty");
93+
return -1;
94+
}
95+
}
96+
97+
/**
98+
* Returns the element at the top of the stack
99+
*
100+
* @return element at the top of the stack
101+
*/
102+
public int peek() {
103+
if (!isEmpty()) { // Checks for an empty stack
104+
return stackArray[top];
105+
} else {
106+
System.out.println("The stack is empty, cant peek");
107+
return -1;
108+
}
109+
}
110+
111+
private void resize(int newSize) {
112+
// private int[] transferArray = new int[newSize]; we can't put modifiers here !
113+
int[] transferArray = new int[newSize];
114+
115+
// for(int i = 0; i < stackArray.length(); i++){ the length isn't a method .
116+
for (int i = 0; i < stackArray.length; i++) {
117+
transferArray[i] = stackArray[i];
118+
stackArray = transferArray;
119+
}
120+
maxSize = newSize;
121+
}
122+
123+
/**
124+
* Returns true if the stack is empty
125+
*
126+
* @return true if the stack is empty
127+
*/
128+
public boolean isEmpty() {
129+
return (top == -1);
130+
}
131+
132+
/**
133+
* Returns true if the stack is full
134+
*
135+
* @return true if the stack is full
136+
*/
137+
public boolean isFull() {
138+
return (top + 1 == maxSize);
139+
}
140+
141+
/**
142+
* Deletes everything in the Stack
143+
* <p>
144+
* Doesn't delete elements in the array
145+
* but if you call push method after calling
146+
* makeEmpty it will overwrite previous
147+
* values
148+
*/
149+
public void makeEmpty() { // Doesn't delete elements in the array but if you call
150+
top = -1; // push method after calling makeEmpty it will overwrite previous values
151+
}
152+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import java.util.ArrayList;
2+
3+
/**
4+
* This class implements a Stack using an ArrayList.
5+
* <p>
6+
* A stack is exactly what it sounds like. An element gets added to the top of
7+
* the stack and only the element on the top may be removed.
8+
* <p>
9+
* This is an ArrayList Implementation of a stack, where size is not
10+
* a problem we can extend the stack as much as we want.
11+
*
12+
* @author Unknown
13+
*/
14+
public class StackArrayList {
15+
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);
29+
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+
}
37+
38+
/**
39+
* ArrayList representation of the stack
40+
*/
41+
private ArrayList<Integer> stackList;
42+
43+
/**
44+
* Constructor
45+
*/
46+
public StackArrayList() {
47+
stackList = new ArrayList<>();
48+
}
49+
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+
}
59+
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() {
67+
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+
}
73+
74+
System.out.print("The stack is already empty!");
75+
return -1;
76+
}
77+
78+
/**
79+
* Checks for empty Stack
80+
*
81+
* @return true if stack is empty
82+
*/
83+
public boolean isEmpty() {
84+
return stackList.isEmpty();
85+
}
86+
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)