-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDeque.java
More file actions
142 lines (125 loc) · 2.97 KB
/
Copy pathDeque.java
File metadata and controls
142 lines (125 loc) · 2.97 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
import java.lang.Iterable;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item>
{
/* Node class for LL creation */
private class Node
{
Item item;
Node prev, next;
public Node(Item item)
{
this.item = item;
this.next = null;
this.prev = null;
}
public Node(Item item, Node prev, Node next)
{
this.item = item;
this.prev = prev;
this.next = next;
}
}
private Node first, last;
private int size;
/* Constructor */
public Deque()
{
this.first = null;
this.last = null;
this.size = 0;
}
public boolean isEmpty()
{
return first == null;
}
/* Public method to get size of deck */
public int size()
{
return size;
}
/* insertion operations */
public void addFirst(Item item)
{
if (item == null) throw new NullPointerException("Cannot add null values.");
Node newNode = new Node(item, null, first);
if (isEmpty())
last = newNode;
else first.prev = newNode;
first = newNode;
size++;
}
public void addLast(Item item)
{
if (item == null) throw new NullPointerException("Cannot add null values.");
// similar to enqueue
Node oldlast = last;
last = new Node(item, oldlast, null);
if (isEmpty()) first = last;
else oldlast.next = last;
size++;
}
/* deletion operations */
public Item removeFirst()
{
if (isEmpty()) throw new NoSuchElementException("Cannot remove anything from empty deck.");
// similar to dequeue
Item item = first.item;
first = first.next;
first.prev = null;
if (isEmpty()) last = first;
size--;
return item;
}
public Item removeLast()
{
if (isEmpty()) throw new NoSuchElementException("Cannot remove anything from empty deck.");
Item item = last.item;
last = last.prev;
last.next = null;
if (last == null) first = last; // check if deck is empty
size--;
return item;
}
/*
* Simple Iteration functionality
* Supports only forward traversal through deck
*/
public Iterator<Item> iterator() { return new DeckIterator(); }
private class DeckIterator implements Iterator<Item>
{
private Node current = first;
public boolean hasNext()
{
return current != null;
}
public void remove()
{
// Not supported...nor will this ever be supported!
throw new UnsupportedOperationException("remove() not supported for iterators.");
}
public Item next()
{
// return next item in Deck
if (!hasNext()) throw new NoSuchElementException("No elements remaining to iterate over.");
Item item = current.item;
current = current.next;
return item;
}
}
/* Unit testing -> Not expected to be called by client programs */
public static void main(String[] args)
{
Deque<String> dq = new Deque<String>();
dq.addFirst("First");
dq.addLast("Last");
dq.addFirst("newFirst");
dq.addLast("newLast");
System.out.println("Removed: " + dq.removeLast());
System.out.println("Removed: " + dq.removeLast());
for(String s:dq)
System.out.print(s + "\t");
System.out.println();
}
}