-
Notifications
You must be signed in to change notification settings - Fork 1
/
VertexStack.java
110 lines (93 loc) · 2.23 KB
/
VertexStack.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
package rsn170330.lp4;
/**
* CS 5V81: Implementation of Data Structures and Algorithms
* Extension of Long Project LP4: PERT, Enumeration of topological orders
*
* Team: LP101
* @author Rahul Nalawade (rsn170330)
* @author Prateek Sarna (pxs180012)
* @author Bhavish Khanna Narayanan (bxn170002)
*/
/**
* Stack Implementation for Enumeration of paths, avoiding built-in stack to
* save from running out of space because of dynamic object creation.
*/
public class VertexStack<T> {
private int capacity;
private int top;
private T[] stack;
// Default Constructor
public VertexStack() {
capacity = 64; // default size = 64*
top = -1;
stack = (T[]) new Object[capacity];
// To avoid "generic array cannot be created" error, declare the array
// to be Object[] and type-cast where needed to avoid type warnings.
}
// Constructor for stack size
public VertexStack(int c) {
capacity = c;
top = -1;
stack = (T[]) new Object[capacity];
}
/**
* Add a new element at top of the stack.
* @param item the element to be pushed onto this stack.
* @return the pushed element
*/
public T push(T item) {
if (top < (capacity - 1)) {
stack[++top] = item;
return item;
}
return null; // Stack overflow
}
/**
* Removes the element at the top of this stack.
* @return removed element
*/
public T pop() {
if (-1 < top) {
T e = stack[top--];
return e;
}
return null; // Empty stack
}
/**
* Looks at the element at the top of this stack
* without removing it from the stack.
* @return top element
*/
public T peek() {
if (top == -1) {
return null;
}
return stack[top];
}
/**
* Tests if this stack is empty.
* @return true iff this stack contains no items, false otherwise.
*/
public boolean empty() {
return top == -1;
}
/**
* Returns the number of elements in the stack.
* @return the number of elements in the stack
*/
public int size() {
return top + 1;
}
/**
* Fills user supplied array with the elements of the stack, in same order
* @param a the supplied array reference
*/
public void toArray(T[] a) {
int l = size();
for (int i=0; i<l; i++) {
a[i] = (T) stack[i];
}
}
public static void main(String args[]) {
}
}