forked from HuLiSyspharm/NetDecoder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Queue.java
64 lines (55 loc) · 1.32 KB
/
Queue.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package netdecoder;
/**
*
* @author edroaldo
*/
public class Queue<Item> {
private Node first;
private Node last;
private int N;
private class Node {
Item item;
Node next;
}
public boolean isEmpty(){
return first == null;
}
public int size(){
return N;
}
public void enqueue(Item item){
Node oldlast = last;
last = new Node();
last.item = item;
last.next = null;
if(isEmpty()){
first = last;
}else{
oldlast.next = last;
}
N++;
}
public Item dequeue(){
Item item = first.item;
first = first.next;
if(isEmpty()){
last = null;
}
N--;
return item;
}
public static void main(String args[]){
Queue<String> queue = new Queue<String>();
queue.enqueue("Edroaldo");
queue.enqueue("Angel");
System.out.println(queue.size());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.size());
}
}