-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkQueue
More file actions
90 lines (80 loc) · 1.93 KB
/
LinkQueue
File metadata and controls
90 lines (80 loc) · 1.93 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public class LinkQueue {
public static void main(String[] args){
LinQueue<Integer> linq = new LinQueue<>();
System.out.println(linq.isempty());
linq.push(68);
linq.push(79);
linq.push(69);
linq.push(18);
linq.push(23);
System.out.println(linq.isempty());
System.out.println(linq.pop());
System.out.println(linq.pop());
System.out.println(linq.amount());
}
}
class LinQueue<T>{
//链队结点
class Node{
T data;
Node next;
public Node(T data,Node next){
this.data = data;
this.next = next;
}
}
private Node front; //队头指针
private Node rear; //队尾指针
private int size; //队列大小
//初始化
public LinQueue(){
this.front = new Node(null,null);
this.rear = new Node(null,null);
this.size = 0;
}
//初始化队列
public void initQueue(){
front.next = null;
rear.next = null;
size =0;
}
//判断队列是否为空
public boolean isempty(){
if(front.next==null){
return true;
}else{
return false;
}
}
//进队
public void push(T data){
Node node = new Node(data,null);
if(isempty()){
front.next = node;
rear.next = node;
}else{
rear.next.next = node;
rear.next = node;
}
size++;
}
//出队
public T pop(){
if(isempty()){
throw new RuntimeException("队列为空");
}else if(size == 1){
Node temp = front.next;
initQueue();
return temp.data;
}else{
Node temp = front.next;
front.next=front.next.next;
size--;
return temp.data;
}
}
//队列的大小
public int amount(){
return size;
}
}