forked from biblelamp/JavaExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.java
More file actions
57 lines (49 loc) · 1.75 KB
/
Copy pathstack.java
File metadata and controls
57 lines (49 loc) · 1.75 KB
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
/**
* Book: Data Structures and Algorithms in Java, by Robert LaFore
* Chapter 4:
* stack.java
* demonstrates stacks
* to compile this code: javac stack.java
* to run this program: java StackApp
*/
class StackX {
private int maxSize; // size of stack array
private long[] stackArray;
private int top; // top of stack
public StackX(int s) { // constructor
maxSize = s; // set array size
stackArray = new long[maxSize]; // create array
top = -1; // no items yet
}
public void push(long j) { // put item on top of stack
stackArray[++top] = j; // increment top, insert item
}
public long pop() { // take item from top of stack
return stackArray[top--]; // access item, decrement top
}
public long peek() { // peek at top of stack
return stackArray[top];
}
public boolean isEmpty() { // true if stack is empty
return (top == -1);
}
public boolean isFull() { // true if stack is full
return (top == maxSize-1);
}
} // end class StackX
class StackApp {
public static void main(String[] args) {
StackX theStack = new StackX(10); // make new stack
theStack.push(20); // push items onto stack
theStack.push(40);
theStack.push(60);
theStack.push(80);
theStack.push(100);
while (!theStack.isEmpty()) { // until it's empty,
long value = theStack.pop(); // delete item from stack
System.out.print(value); // display it
System.out.print(" ");
}
System.out.println("");
}
}