-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.txt
More file actions
47 lines (44 loc) · 1.18 KB
/
stack.txt
File metadata and controls
47 lines (44 loc) · 1.18 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
/** Implementing Stack in java
* Stack: A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation).
* Many stacks also support a read ahead (a 'peek' operation), which reads data without removing it.
* A stack is a LIFO-queue, meaning that the last data to be inserted will be the first data to be removed.
*/
public class MyStack {
private int maxSize;
private long[] stackArray;
private int top;
public MyStack(int s) {
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
public void push(long j) {
stackArray[++top] = j;
}
public long pop() {
return stackArray[top--];
}
public long peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
public static void main(String[] args) {
MyStack theStack = new MyStack(10);
theStack.push(10);
theStack.push(20);
theStack.push(30);
theStack.push(40);
theStack.push(50);
while (!theStack.isEmpty()) {
long value = theStack.pop();
System.out.print(value);
System.out.print(" ");
}
System.out.println("");
}
}