-
Notifications
You must be signed in to change notification settings - Fork 2
/
DLLDeque.java
151 lines (130 loc) · 3.6 KB
/
DLLDeque.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
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
/* Team JDK -- Judy Liu, David Frid, Karina Ionkina
APCS2 pd05
Lab 02 -- Synecdoche
2017- 04- 03 */
/*------------------------Deque Class------------------------
** Implementation to a double ended queue. It uses a
** doubly linked nodes as its container
-----------------------------------------------------------*/
public class DLLDeque<T>{
private DLLNode<T> _front;
private DLLNode<T> _end;
private int _size;
public DLLDeque(){
_size = 0;
_front = null;
_end = null;
}
//~~~~~~~~~~~~~ methods for Front of Deque ~~~~~~~~~~~~
/*------------------------addFront-----------------------
**Adds element to the front by linking a new node to the
**front of the list.
-------------------------------------------------------*/
public void addFirst(T x){
if(isEmpty()){
_front = new DLLNode<T>(x, null, null);
_end = _front;
}
else {
DLLNode<T> bob = new DLLNode<T>(x, null, _front);
_front = bob;
}
_size++;
}
/*------------------------------removeFront------------------
**Removes element from the front by setting the _front to
**_front.getNext() and setting the previous of the node
** ahead to null
-------------------------------------------------------*/
public T removeFirst(){
if(isEmpty()){
throw new IndexOutOfBoundsException("Emmmpppptttyyyy");
}
T ret = _front.getCargo();
_front = _front.getNext();
// _front.setPrev(null); // why does this have to be removed?
_size-- ;
return ret;
}
public T peekFirst(){
if(isEmpty()){
return null;
}
return _front.getCargo();
}
//~~~~~~~~~~~~ methods for the End of Deque ~~~~~~~~~~~
/*------------------------addEnd-------------------------
**Adds element to the end by linking a last node to
** a new node whose cargo is x.
-------------------------------------------------------*/
public void addLast(T x){
if (isEmpty()){
_end = new DLLNode<T>(x,null, null);
_front = _end;
}
else{
DLLNode<T> bob = new DLLNode<T>(x, _end,null);
_end = bob;
_end.getPrev().setNext(_end);
}
_size ++;
}
/*---------------------removeEnd------------------------
** The second to last element's getNext is set to null
** and _end points to that element.
-------------------------------------------------------*/
public T removeLast(){
if(isEmpty()){
throw new IndexOutOfBoundsException("Emmmpppptttyyyy");
}
else{
T ret = _end.getCargo();
_end = _end.getPrev();
_end.setNext(null);
_size --;
return ret;
}
}
public T peekLast(){
if (isEmpty()){
return null;
}
return _end.getCargo();
}
public int size(){
return _size;
}
public boolean isEmpty(){
return _front == null;
}
public String toString(){
String retStr = "";
DLLNode node = _front;
while(node != null){
retStr += node.toString() + " ";
node = node.getNext();
}
return retStr;
}
public static void main(String[] args){
/* DLLDeque<String> test = new DLLDeque<String>();
test.addEnd("boop");
System.out.println(test.peekEnd());
test.addFront("hello4");
System.out.println(test);
test.addFront("hello3");
//System.out.println(
System.out.println(test);
//test.removeFront();
test.addEnd("hello5");
System.out.println(test);
test.addEnd("oops");
System.out.println(test);
test.addFront("hello2");
System.out.println(test);
test.addFront("hello1");
test.removeEnd();
System.out.println(test.peekEnd());
} */
}
}