-
Notifications
You must be signed in to change notification settings - Fork 0
/
Steque.java
55 lines (55 loc) · 853 Bytes
/
Steque.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
public class Steque<T>{
private int N;
DNode first, last;
private class DNode{
DNode next, prev;
T item;
DNode(T item){
this.item = item;
}
}
public void push(T item){
if(N == 0){
first = new DNode(item);
last = first;
}
else{
DNode newLast = new DNode(item);
last.next = newLast;
newLast.prev = last;
last = newLast;
}
N++;
}
public T pop(){
T ret;
if(N == 1){
ret = last.item;
first = last = null;
N--;
}
else if(N > 1){
ret = last.item;
last = last.prev;
last.next = null;
N--;
}
else{
throw new IllegalStateException();
}
return ret;
}
public void enqueue(T item){
if(N == 0){
first = new DNode(item);
last = first;
}
else{
DNode newFirst = new DNode(item);
newFirst.next = first;
first.prev = newFirst;
first = newFirst;
}
N++;
}
}