@@ -19,42 +19,38 @@ public class Stack<E> implements Serializable {
1919/**
2020 * Position of tail in stack
2121 */
22-
2322private int tail = -1 ;
2423
2524/**
2625 * Size of stack at any given time
2726 */
28-
2927private 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" )
4039public 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-
4846public boolean empty () {
4947return 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 () {
5854if (empty ()) {
5955throw 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 () {
7065if (empty ()) {
7166throw new EmptyStackException ();
7267 }
7368
74- Object removedElement =elements [tail ];
69+ E removedElement =elements [tail ];
7570tail --;
7671size --;
7772return 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 );
9285elements =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
10796int index = -1 ;
10897boolean found =false ;
@@ -111,7 +100,7 @@ public int search(Object o) {
111100 }
112101
113102for (int i =0 ;i <size ();i ++) {
114- if (elements [i ] == o ) {
103+ if (elements [i ]. equals ( o ) ) {
115104index =i ;
116105found =true ;
117106break ;