forked from Apress/learn-java-for-android-dev-14
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
44 lines (40 loc) · 884 Bytes
/
Copy pathStack.java
File metadata and controls
44 lines (40 loc) · 884 Bytes
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
public class Stack
{
private Object[] elements;
private int top;
public Stack(int size)
{
elements = new Object[size];
top = -1; // indicate that stack is empty
}
public void push(Object o)
{
if (top + 1 == elements.length)
{
System.out.println("stack is full");
return;
}
elements[++top] = o;
}
public Object pop()
{
if (top == -1)
{
System.out.println("stack is empty");
return null;
}
Object element = elements[top--];
// elements[top + 1] = null;
return element;
}
public static void main(String[] args)
{
Stack stack = new Stack(2);
stack.push("A");
stack.push("B");
stack.push("C");
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}