33import java .io .Serializable ;
44import java .util .EmptyStackException ;
55
6- public class Stack <E > implements Serializable {
6+ public class Stack <E > implements Serializable {
77
88 /**
9- * Inital capacity alloted to stack on object creation
9+ * Initial capacity allocated to stack on object creation
1010 */
1111 private final int INITIAL_CAPACITY = 10 ;
1212
1313 /**
1414 * Increment in memory space once stack is out of space
1515 */
16- private final int EXNTEDED_CAPACITY = 10 ;
16+ private final int EXTENDED_CAPACITY = 10 ;
1717
1818
1919 /**
@@ -30,15 +30,14 @@ public class Stack<E> implements Serializable {
3030
3131 /**
3232 * 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
3434 */
3535 private Object [] elements ;
3636
3737 /**
38- * No argument to create stack object with inital capacity
38+ * No argument to create stack object with initial capacity
3939 */
4040 public Stack () {
41-
4241 elements = new Object [INITIAL_CAPACITY ];
4342 }
4443
@@ -47,16 +46,7 @@ public Stack() {
4746 */
4847
4948 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 ;
6050 }
6151
6252
@@ -65,21 +55,19 @@ public boolean empty() {
6555 */
6656
6757 public Object peek () {
58+ if (empty ()) {
59+ throw new EmptyStackException ();
60+ }
6861
69- if (empty ()) {
70- throw new EmptyStackException ();
71- }
72-
73- return elements [tail ];
62+ return elements [tail ];
7463 }
7564
7665 /**
7766 * Method to remove the top element from stack
7867 */
7968
8069 public Object pop () {
81-
82- if (empty ()) {
70+ if (empty ()) {
8371 throw new EmptyStackException ();
8472 }
8573
@@ -95,12 +83,12 @@ public Object pop() {
9583 public Object push (Object e ) {
9684
9785 boolean isSuccess = false ;
98- if (tail < (INITIAL_CAPACITY - 1 )){
86+ if (tail < (INITIAL_CAPACITY - 1 )) {
9987 tail ++;
10088 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 ));
10492 elements = extendedElements ;
10593 tail ++;
10694 elements [tail ] = e ;
@@ -118,19 +106,19 @@ public int search(Object o) {
118106
119107 int index = -1 ;
120108 boolean found = false ;
121- if (empty ()) {
109+ if (empty ()) {
122110 return -1 ;
123111 }
124112
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 ) {
127115 index = i ;
128116 found = true ;
129117 break ;
130118 }
131119 }
132120
133- if (found ) {
121+ if (found ) {
134122 index = tail - index + 1 ;
135123 }
136124
@@ -143,5 +131,4 @@ public int search(Object o) {
143131 public int size () {
144132 return size ;
145133 }
146-
147134}
0 commit comments