-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path堆栈.txt
85 lines (70 loc) · 1.35 KB
/
堆栈.txt
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
package ali.interview;
public class ListStack {
class Node{
private Object data;
private Node next;
public Node() {
this.data = null;
this.next = null;
}
public Node(Object data) {
this.data = data;
this.next = null;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
private Node top;
public Node getTop() {
return top;
}
public void setTop(Node top) {
this.top = top;
}
public ListStack() {
top = new Node();
}
public boolean isNull() {
if(top.getNext() == null)
return true;
return false;
}
public void push(Node node) {
if(this.isNull()) {
top.setNext(node);
}else {
node.setNext(top.getNext());
top.setNext(node);
}
}
public Object pop() {
if (this.isNull()) {
return null;
}else {
Node n = top.getNext();
top.setNext(n.getNext());
return n.getData();
}
}
public static void main(String[] args) {
ListStack ls = new ListStack();
System.out.println(ls.isNull());
ls.push(ls.new Node(5));
ls.push(ls.new Node('g'));
ls.push(ls.new Node(7));
System.out.println(ls.pop());
System.out.println(ls.pop());
System.out.println(ls.pop());
System.out.println(ls.pop());
}
}