Algorithm-Assignment2-Deque&RandomizedQueue

Specification

The concept was easy.

Deque: Queue with support for Adding/Removing items from either the front or the end.

Randomized Queue: Pop random item when Deque.

Deque.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
152
import edu.princeton.cs.algs4.StdOut;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Deque<Item> implements Iterable<Item> {
private Node first;
private Node last;
private int size;

private class Node {
Item item;
Node previous;
Node next;
}

public Deque() {
first = null;
last = null;
size = 0;
}

public boolean isEmpty() {
return (first == null && last == null);
}

public int size() {
return this.size;
}

public void addFirst(Item item) {
if (item == null)
throw new IllegalArgumentException();
Node oldFirst = first;
first = new Node();
first.item = item;
first.previous = null;
first.next = oldFirst;
if (oldFirst == null && last == null) last = first;
else {
oldFirst.previous = first;
}
size++;
}

public void addLast(Item item) {
if (item == null)
throw new IllegalArgumentException();
Node oldLast = last;
last = new Node();
last.item = item;
last.next = null;
if (first == null && oldLast == null) first = last;
else {
oldLast.next = last;
last.previous = oldLast;
}
size++;
}

public Item removeFirst() {
if (first == null)
throw new NoSuchElementException();
Item item = first.item;
first = first.next;
if (first == null) last = null;
else {
first.previous = null;
}
size--;
return item;
}

public Item removeLast() {
if (last == null)
throw new NoSuchElementException();
Item item = last.item;
last = last.previous;
if (last == null) first = null;
else last.next = null;
if (isEmpty()) first = null;
size--;
return item;
}

public Iterator<Item> iterator() {
return new DequeIterator();
}

private class DequeIterator implements Iterator<Item> {
private Node current = first;

@Override
public boolean hasNext() {
return current != null;
}

@Override
public void remove() {
throw new UnsupportedOperationException();
}

@Override
public Item next() {
if (current == null) {
throw new NoSuchElementException();
}
Item item = current.item;
current = current.next;
return item;
}

}

public static void main(String[] args) {
Deque<Integer> deque = new Deque<>();
StdOut.println("DequeIsEmpty: " + deque.isEmpty());
StdOut.println("Deque's Size: " + deque.size());

Integer a = 1;
deque.addFirst(1);
deque.addFirst(2);
deque.removeFirst();

deque.addLast(1);
deque.addLast(2);
deque.removeFirst();

deque.addFirst(1);
deque.removeFirst();

deque.addLast(1);
deque.addFirst(a);
a++;
deque.addLast(a);
for (Integer i = 1; i < 10; i++) {
deque.addFirst(i);
}

Iterator<Integer> it = deque.iterator();
while (it.hasNext()) {
Integer i = it.next();
StdOut.println("it: " + i);
}

StdOut.println("Size: " + deque.size());
StdOut.println("Remove First: " + deque.removeFirst());
StdOut.println("Remove Last: " + deque.removeLast());
StdOut.println("Size: " + deque.size());
}

}

RandomizedQueue.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class RandomizedQueue<Item> implements Iterable<Item> {
private Node first;
private Node last;
private int size;

// construct an empty randomized queue
public RandomizedQueue() {
first = null;
last = null;
size = 0;
}

private class Node {
Item item;
Node prev;
Node next;
}
// is the randomized queue empty?
public boolean isEmpty() {
return (first == null && last == null);
}

// return the number of items on the randomized queue
public int size() {
return this.size;
}

// add the item
public void enqueue(Item item) {
if (item == null) {
throw new IllegalArgumentException();
}
Node oldLast = last;
last = new Node();
last.item = item;
last.next = null;
if (first == null && oldLast == null) first = last;
else {
oldLast.next = last;
last.prev = oldLast;
}
size++;
}

// remove and return a random item
public Item dequeue() {
Item item;
int id = StdRandom.uniform(size);
Node current = first;
if (current == null) {
throw new NoSuchElementException();
}

for (int i = 0; i < id; i++) {
if (current.next != null)
current = current.next;
}

item = current.item;
// Remove current

Node toDelete = current;
if (current == first && first.next == null) {
first = null;
last = null;
} else if (current == first) {
current = current.next;
first = first.next;
first.prev = null;
} else {
current = current.prev;
if (toDelete.next == null) {
current.next = null;
} else {
current.next = toDelete.next;
current.next.prev = toDelete.prev;
}
}

size--;
return item;
}

// return a random item (but do not remove it)
public Item sample() {
Item item;
int id = StdRandom.uniform(size);
Node current = first;
if (current == null) {
throw new NoSuchElementException();
}

for (int i = 0; i < id; i++) {
current = current.next;
}
item = current.item;
return item;
}

// return an independent iterator over items in random order
public Iterator<Item> iterator() {
return new RandomizedQueueIterator();
}

private class RandomizedQueueIterator implements Iterator<Item> {
private Node current = first;

@Override
public boolean hasNext() {
return current != null;
}

@Override
public void remove() {
throw new UnsupportedOperationException();
}

@Override
public Item next() {
if (current == null) {
throw new NoSuchElementException();
}

Item item = current.item;
current = current.next;
return item;
}
}
// unit testing (required)
public static void main(String[] args) {
RandomizedQueue<Integer> rq = new RandomizedQueue<>();
StdOut.println("RandomizedQueue is Empty: " + rq.isEmpty());
StdOut.println("size: " + rq.size());

rq.enqueue(283);
StdOut.println(rq.sample());

rq.enqueue(119);

StdOut.println(rq.dequeue());
StdOut.println(rq.dequeue());

for (Integer i = 0; i < 5; i++){
rq.enqueue(i);
}

StdOut.println("size after enqueue: " + rq.size());
StdOut.println("Deque: " + rq.dequeue());
StdOut.println("Sample: " + rq.sample());

Iterator<Integer> jt = rq.iterator();
while (jt.hasNext()) {
Integer i = jt.next();
StdOut.println("it: " + i);
}

}
}