forked from sxia1/Hydration
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQQKachoo.java
More file actions
155 lines (133 loc) · 3.94 KB
/
Copy pathQQKachoo.java
File metadata and controls
155 lines (133 loc) · 3.94 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//Team Hydration
//Roster: Zane Wang, Sophia Xia, Maggie Zhao
//APCS2 pd1
//L02 -- All Hands on Deque! (Not Schenectady; rather, synecdoche.)
//2018-04-18
import java.lang.Iterable;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class QQKachoo<D> implements Deque<D>{
//INSTANCE VARIABLES =============================================
private DLLNode<D> _head, _tail;
private int _size;
//METHODS =========================================================
//Constructor
public void QQKachoo() {
_head = new DLLNode(null,null,null);
_tail = new DLLNode(null,null,null);
_size = 0;
}
//Returns the number of elements in this deque.
public int size(){
return _size;
}
//Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque). Throws a NoSuchElementException if the deque is empty.
// * Throw exception.
public D element(){
return _head.getCargo();
}
//Retrieves, but does not remove, the first element of this deque.
public D getFirst(){
return _head.getCargo();
}
//Retrieves, but does not remove, the last element of this deque.
public D getLast(){
return _tail.getCargo();
}
public D peek() {
return peekFirst();
}
public D peekFirst() {
if (_size == 0)
return null;
return _head.getCargo();
}
public D peekLast() {
if (_size == 0)
return null;
return _tail.getCargo();
}
//Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to
public boolean add(D d){
addLast(d);
return true;
}
//Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions
public void addFirst(D d){
DLLNode<D> newNode= new DLLNode<D>(d, null, _head);
if (size() == 0)
_head = _tail = newNode;
else {
newNode.setNext(_head);
_head.setPrev(newNode);
_head = newNode;
}
_size ++;
}
//Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions.
public void addLast(D d){
DLLNode<D> newNode = new DLLNode<D>(d, _tail, null);
if (size() == 0)
_head = _tail = newNode;
else {
_tail.setNext(newNode);
newNode.setPrev(_tail);
_tail = newNode;
}
_size++;
}
//Remove Methods
public D remove(){
return removeFirst();
}
// throws an exception if empty
public D removeFirst(){
if (_size == 0)
throw new NoSuchElementException();
D temp = _head.getCargo();
if (_size > 1)
_head = _head.getNext();
_head.setPrev(null);
_size--;
return temp;
}
// throws an exeption if empty
public D removeLast(){
if (_size == 0)
throw new NoSuchElementException();
D temp = _tail.getCargo();
_tail = _tail.getPrev();
_tail.setNext(null);
_size--;
return temp;
}
/*
public Iterator<D> iterator() {
Iterator<D> it = new Iterator(_head);
return it;
}
public Iterator<D> descendingIterator() {
DLLNode<D> help = _tail;
DLLNode<D> temp = new DLLNode(_tail.getCargo(),null,null);
DLLNode<D> retItr = temp;
for (int i = 0; i < _size - 1; i++) {
help = help.getPrev();
retItr.setNext(new DLLNode(help.getCargo(),retItr,null));
retItr = retItr.getNext();
}
Iterator<D> it = new Iterator(temp);
return it;
}
*/
public static void main(String[] args){
QQKachoo deck = new QQKachoo();
deck.addFirst("boo");
deck.addLast("hoo");
deck.addFirst("who");
System.out.println(deck.peekFirst());//who
System.out.println(deck.removeFirst());//who
System.out.println(deck.removeFirst());//boo
System.out.println(deck.peekFirst());//hoo
System.out.println(deck.removeFirst());//hoo
}
}