com.ibm.nicky.stack--
++ExceptionStackEmpty
++Stack
++StackInterface
++TestStack
********************************************************************************************
1
package com.ibm.nicky.stack;
2
3
/**
4
* @author QuQiang
5
*
6
* Try to use pop(top) method when the Stack is null, ExceptionStackEmpty.
7
*/
8
public class ExceptionStackEmpty extends Exception{
9
public ExceptionStackEmpty(String e){
10
super(e);
11
}
12
13
private static final long serialVersionUID = 4983277878327382L;// Enable at once but have no impact on the sub-class
14
}
15

2

3

4

5

6

7

8

9

10

11

12

13

14

15

1
package com.ibm.nicky.stack;
2
3
/**
4
* @author QuQiang
5
*
6
*/
7
public class Stack<E> implements StackInterface<E> {
8
9
private final static int CAPACITY = 1024; // The default capacity of Stack
10
private int capacity = 0; // The current capacity of Stack
11
private Object[] element; // E array
12
private int toppiont = -1; // the current top piont
13
14
public Stack(){
15
this(CAPACITY);
16
}
17
public Stack(int cap){
18
capacity = cap ;
19
element = new Object[capacity];
20
}
21
/* (non-Javadoc)
22
* @see com.ibm.nicky.stack.Stack#getSize()
23
*/
24
public int getSize() {
25
return (toppiont+1);
26
}
27
28
/* (non-Javadoc)
29
* @see com.ibm.nicky.stack.Stack#isEmpty()
30
*/
31
public boolean isEmpty() {
32
return (toppiont<0);
33
}
34
35
/* (non-Javadoc)
36
* @see com.ibm.nicky.stack.Stack#pop()
37
*/
38
public E pop() throws ExceptionStackEmpty {
39
E TopPiontValue;
40
if(isEmpty()){
41
throw new ExceptionStackEmpty("Stack is Empty");
42
}
43
TopPiontValue = (E) element[toppiont];
44
element[toppiont--] = null;
45
return TopPiontValue;
46
}
47
48
/* (non-Javadoc)
49
* @see com.ibm.nicky.stack.Stack#push()
50
*/
51
public void push(E object) {
52
if(getSize() == capacity){
53
creatNew();
54
}
55
element[++toppiont] = object;
56
}
57
58
/* (non-Javadoc)
59
* @see com.ibm.nicky.stack.Stack#top()
60
*/
61
public E top() throws ExceptionStackEmpty {
62
if(isEmpty()){
63
throw new ExceptionStackEmpty("Stack is Empty");
64
}
65
return (E)element[toppiont];
66
}
67
68
private Object creatNew(){
69
int size = 0;
70
if(capacity == 0){
71
size = 1;
72
}else{
73
size = CAPACITY; // Not optimized method
74
}
75
76
Object[] temp = new Object[element.length+size];
77
System.arraycopy(element, 0, temp, 0, element.length);
78
element = temp;
79
System.out.println("hava invoke this createNew");
80
System.out.print("the current size of the Stack is :"+ element.length);
81
return element;
82
}
83
84
}
85

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

1
package com.ibm.nicky.stack;
2
3
/**
4
* @author QuQiang
5
*
6
* Stack ADT interface
7
*/
8
public interface StackInterface<E> {
9
/**
10
* @return boolean if the Stack is empty.
11
*/
12
public boolean isEmpty();
13
14
/**
15
* @return int, the Size of the Stack.
16
*/
17
public int getSize();
18
19
/**
20
*
21
* @return the Object of the Stack top but not delete it.
22
* @throws ExceptionStackEmpty
23
* the Stack is empty
24
*/
25
public E top() throws ExceptionStackEmpty;
26
27
/**
28
*
29
* @return the Object of the Stack top but delete it.
30
* @throws ExceptionStackEmpty
31
* the Stack is empty
32
*/
33
public E pop() throws ExceptionStackEmpty;
34
35
/**
36
* push a object to the Stack top.
37
*/
38
public void push(E object);
39
}
40

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

1
package com.ibm.nicky.stack;
2
3
/**
4
* @author QuQiang
5
*
6
*/
7
public class TestStack {
8
9
public static void main(String[] args){
10
Stack<Integer> testStack = new Stack<Integer>(2);
11
12
testStack.push(21);
13
testStack.push(12);
14
testStack.push(-9);
15
16
try {
17
System.out.println("testStack.top()="+testStack.top());
18
System.out.println("testStack.pop()="+testStack.pop());
19
System.out.println("testStack.top()="+testStack.top());
20
} catch (ExceptionStackEmpty e) {
21
e.printStackTrace();
22
}
23
}
24
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24
