-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.java
executable file
·124 lines (110 loc) · 3.12 KB
/
Stack.java
1
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Stack data structure
* working : last in first out
* class implement following interface
* methods:
* push item : store item
* pop item : remove top item
* peek item : return top item (not remove)
* isempty : return true if stack has no element
* isFull : return true if stack is full no capacity to store further
* createStack : re-create stack means wash previous values
* getStackSize : return stack total capacity
* display : print elements of stack on screen
* toString : return elements of stack in string format
* */
interface StackI<E> {
public void createStack();
public void push ( E e );
public E pop ();
public E peek();
public boolean isEmpty();
public boolean isFull();
}
public class Stack<E> implements StackI<E>{
private int numberOfStackElements = 0;
private int top = -1;
private E[] aStack;
/**
* Default constructor
*/
Stack() {
this.aStack = (E[])new Object[10];
this.numberOfStackElements = 0;
this.top = -1;
}
Stack( int size ) {
if ( size <=0) {
System.out.println("Size of array not allowed!");
size = 10;
}
this.aStack = (E[])new Object[size];
this.numberOfStackElements = 0;
this.top = -1;
}
@Override
public void createStack() {
this.aStack = (E[])new Object[aStack.length];
this.numberOfStackElements = 0;
this.top = -1;
}
@Override
public void push(E e) {
if ( isFull() ) {
System.out.println(String.format("Array is full."));
} else {
this.top ++;
this.aStack[this.top] = e;
this.numberOfStackElements ++;
}
}
@Override
public E pop() {
if ( isEmpty() ) {
throw new NullPointerException("Can't pop stack its Empty!!");
} else {
this.numberOfStackElements --;
E e = this.aStack[this.top];
this.top --;
return e;
}
}
@Override
public E peek() {
if ( isEmpty() )
throw new NullPointerException("Can't peek at top item as the stack is Empty!!");
else
return this.aStack[this.top];
}
@Override
public boolean isEmpty() {
return ( this.getStackSize() == 0 );
}
@Override
public boolean isFull() {
return ( this.numberOfStackElements == aStack.length);
}
public void displayStack() {
System.out.println( this.toString() );
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder( "\n" );
int tmp = this.getStackSize() - 1;
if(isEmpty())
{
sb.append("The stack is Empty!!");
}
else
{
while (tmp > -1) {
sb.append( this.aStack[ tmp ] + "\n");
tmp --;
}
}
return sb.toString();
}
public int getStackSize() {
return this.numberOfStackElements;
}
}