Skip to content

Commit 2d4d7dd

Browse files
committed
Rewrote Stack.java
The previous implementation would show unexpected behaviour when using the values returned by pop() and peek(). Generic type was needed but not used in implementation thus leading to redundancy. push() had unnecessary return type.
1 parent 573b1ce commit 2d4d7dd

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

src/main/java/com/dataStructures/Stack.java

+18-29
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,38 @@ public class Stack<E> implements Serializable {
1919
/**
2020
* Position of tail in stack
2121
*/
22-
2322
private int tail = -1;
2423

2524
/**
2625
* Size of stack at any given time
2726
*/
28-
2927
private int size;
3028

3129
/**
3230
* Uninitialized array to hold stack elements.
33-
* WIll be initialized with initial capacity once the object is created
31+
* Will be initialized with initial capacity once the object is created
3432
*/
35-
private Object[] elements;
33+
private E[] elements;
3634

3735
/**
3836
* No argument to create stack object with initial capacity
3937
*/
38+
@SuppressWarnings("unchecked")
4039
public Stack() {
41-
elements = new Object[INITIAL_CAPACITY];
40+
elements = (E[]) new Object[INITIAL_CAPACITY];
4241
}
4342

4443
/**
4544
* Method to check if the given stack is empty or not
4645
*/
47-
4846
public boolean empty() {
4947
return elements == null || size == 0;
5048
}
5149

52-
5350
/**
5451
* Method to check the element on head without removing it
5552
*/
56-
57-
public Object peek() {
53+
public E peek() {
5854
if (empty()) {
5955
throw new EmptyStackException();
6056
}
@@ -65,13 +61,12 @@ public Object peek() {
6561
/**
6662
* Method to remove the top element from stack
6763
*/
68-
69-
public Object pop() {
64+
public E pop() {
7065
if (empty()) {
7166
throw new EmptyStackException();
7267
}
7368

74-
Object removedElement = elements[tail];
69+
E removedElement = elements[tail];
7570
tail--;
7671
size--;
7772
return removedElement;
@@ -80,29 +75,23 @@ public Object pop() {
8075
/**
8176
* Method to add element to stack
8277
*/
83-
public Object push(Object e) {
84-
85-
boolean isSuccess = false;
86-
if (tail < (INITIAL_CAPACITY - 1)) {
87-
tail++;
88-
elements[tail] = e;
89-
} else {
90-
Object[] extendedElements = new Object[INITIAL_CAPACITY + EXTENDED_CAPACITY];
91-
System.arraycopy(elements, 0, extendedElements, 0, (tail + 1));
78+
@SuppressWarnings("unchecked")
79+
public void push(E e) {
80+
81+
tail = tail + 1;
82+
if (tail >= INITIAL_CAPACITY) {
83+
E[] extendedElements = (E[]) new Object[INITIAL_CAPACITY + EXTENDED_CAPACITY];
84+
System.arraycopy(elements, 0, extendedElements, 0, tail);
9285
elements = extendedElements;
93-
tail++;
94-
elements[tail] = e;
9586
}
96-
size++;
97-
return e;
98-
87+
elements[tail] = e;
88+
size = size + 1;
9989
}
10090

10191
/**
10292
* Method to search for an element in stack
10393
*/
104-
105-
public int search(Object o) {
94+
public int search(E o) {
10695

10796
int index = -1;
10897
boolean found = false;
@@ -111,7 +100,7 @@ public int search(Object o) {
111100
}
112101

113102
for (int i = 0; i < size(); i++) {
114-
if (elements[i] == o) {
103+
if (elements[i].equals(o)) {
115104
index = i;
116105
found = true;
117106
break;

0 commit comments

Comments
 (0)