3
3
import java .io .Serializable ;
4
4
import java .util .EmptyStackException ;
5
5
6
- public class Stack <E > implements Serializable {
6
+ public class Stack <E > implements Serializable {
7
7
8
8
/**
9
- * Inital capacity alloted to stack on object creation
9
+ * Initial capacity allocated to stack on object creation
10
10
*/
11
11
private final int INITIAL_CAPACITY = 10 ;
12
12
13
13
/**
14
14
* Increment in memory space once stack is out of space
15
15
*/
16
- private final int EXNTEDED_CAPACITY = 10 ;
16
+ private final int EXTENDED_CAPACITY = 10 ;
17
17
18
18
19
19
/**
@@ -30,15 +30,14 @@ public class Stack<E> implements Serializable {
30
30
31
31
/**
32
32
* Uninitialized array to hold stack elements.
33
- * WIll be intialized with inital capacity once the object is created
33
+ * WIll be initialized with initial capacity once the object is created
34
34
*/
35
35
private Object [] elements ;
36
36
37
37
/**
38
- * No argument to create stack object with inital capacity
38
+ * No argument to create stack object with initial capacity
39
39
*/
40
40
public Stack () {
41
-
42
41
elements = new Object [INITIAL_CAPACITY ];
43
42
}
44
43
@@ -47,16 +46,7 @@ public Stack() {
47
46
*/
48
47
49
48
public boolean empty () {
50
-
51
- if (null == elements ) {
52
- return true ;
53
- }
54
-
55
- if (size == 0 ){
56
- return true ;
57
- }
58
-
59
- return false ;
49
+ return elements == null || size == 0 ;
60
50
}
61
51
62
52
@@ -65,21 +55,19 @@ public boolean empty() {
65
55
*/
66
56
67
57
public Object peek () {
58
+ if (empty ()) {
59
+ throw new EmptyStackException ();
60
+ }
68
61
69
- if (empty ()) {
70
- throw new EmptyStackException ();
71
- }
72
-
73
- return elements [tail ];
62
+ return elements [tail ];
74
63
}
75
64
76
65
/**
77
66
* Method to remove the top element from stack
78
67
*/
79
68
80
69
public Object pop () {
81
-
82
- if (empty ()) {
70
+ if (empty ()) {
83
71
throw new EmptyStackException ();
84
72
}
85
73
@@ -95,12 +83,12 @@ public Object pop() {
95
83
public Object push (Object e ) {
96
84
97
85
boolean isSuccess = false ;
98
- if (tail < (INITIAL_CAPACITY - 1 )){
86
+ if (tail < (INITIAL_CAPACITY - 1 )) {
99
87
tail ++;
100
88
elements [tail ] = e ;
101
- }else {
102
- Object [] extendedElements = new Object [INITIAL_CAPACITY + EXNTEDED_CAPACITY ];
103
- System .arraycopy (elements , 0 , extendedElements , 0 , (tail + 1 ));
89
+ } else {
90
+ Object [] extendedElements = new Object [INITIAL_CAPACITY + EXTENDED_CAPACITY ];
91
+ System .arraycopy (elements , 0 , extendedElements , 0 , (tail + 1 ));
104
92
elements = extendedElements ;
105
93
tail ++;
106
94
elements [tail ] = e ;
@@ -118,19 +106,19 @@ public int search(Object o) {
118
106
119
107
int index = -1 ;
120
108
boolean found = false ;
121
- if (empty ()) {
109
+ if (empty ()) {
122
110
return -1 ;
123
111
}
124
112
125
- for (int i = 0 ; i < size ();i ++) {
126
- if (elements [i ] == o ) {
113
+ for (int i = 0 ; i < size (); i ++) {
114
+ if (elements [i ] == o ) {
127
115
index = i ;
128
116
found = true ;
129
117
break ;
130
118
}
131
119
}
132
120
133
- if (found ) {
121
+ if (found ) {
134
122
index = tail - index + 1 ;
135
123
}
136
124
@@ -143,5 +131,4 @@ public int search(Object o) {
143
131
public int size () {
144
132
return size ;
145
133
}
146
-
147
134
}
0 commit comments