-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackLink
More file actions
69 lines (60 loc) · 1.4 KB
/
StackLink
File metadata and controls
69 lines (60 loc) · 1.4 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
58
59
60
61
62
63
64
65
66
67
68
69
public class StackLink {
public static void main(String[] args){
StackL<Integer> stack= new StackL<>();
System.out.println(stack.isempty());
stack.push(9);
stack.push(5);
stack.push(8);
System.out.println(stack.peek());
System.out.println(stack.length());
System.out.println(stack.pop());
}
}
class StackL<T>{
//节点
private class Node{
T e;
Node next;
public Node(){}
public Node(T e,Node next){
this.e = e;
this.next = next;
}
}
private Node top = null; //栈顶元素
private int size; //当前栈的大小
//入栈
public boolean push(T e){
top = new Node(e,top);
size++;
return true;
}
//判断是否为空栈
public boolean isempty(){
return size == 0;
}
//查看元素但不删除
public T peek(){
if(isempty()){
throw new RuntimeException("空栈异常!");
}else{
return top.e;
}
}
//出栈
public T pop(){
if(isempty()){
throw new RuntimeException("空栈异常!");
}else{
Node value = top;
top = top.next;
value.next=null;
size--;
return value.e;
}
}
//当前栈的大小
public int length(){
return size;
}
}