Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix style in HW2/Deque.java.

  • Loading branch information...
commit 55801b445873b2cfe3bdcf4ab7255845e752e99d 1 parent c022ed7
@JWong42 authored
Showing with 55 additions and 55 deletions.
  1. +55 −55 HW2/Deque.java
View
110 HW2/Deque.java
@@ -12,94 +12,94 @@
private Node prev;
}
- public Deque() {
- N = 0;
- pre = new Node(); // Creates the sentinel that's before the first item.
- post = new Node(); // Creates the sentinel that's after the last item.
- pre.next = post;
- post.prev = pre;
- }
+ public Deque() {
+ N = 0;
+ pre = new Node(); // Creates the sentinel that's before the first item.
+ post = new Node(); // Creates the sentinel that's after the last item.
+ pre.next = post;
+ post.prev = pre;
+ }
- // Checks if deque is empty.
+ // Checks if deque is empty.
public boolean isEmpty() {
- return N == 0;
+ return N == 0;
}
// Returns the size of deque.
public int size() {
- return N;
+ return N;
}
// Adds an item to the front of the deque.
public void addFirst(Item item) {
- if (item == null) throw new java.lang.NullPointerException("Cannot add null items.");
- Node oldFirst = pre.next;
- Node first = new Node();
- first.item = item;
- first.prev = pre;
+ if (item == null) throw new java.lang.NullPointerException("Cannot add null items.");
+ Node oldFirst = pre.next;
+ Node first = new Node();
+ first.item = item;
+ first.prev = pre;
first.next = oldFirst;
- oldFirst.prev = first;
- pre.next = first;
- N++;
+ oldFirst.prev = first;
+ pre.next = first;
+ N++;
}
// Adds an item to the back of the deque.
public void addLast(Item item) {
- if (item == null) throw new java.lang.NullPointerException("Cannot add null items");
- Node oldLast = post.prev;
- Node last = new Node();
- last.item = item;
- last.next = post;
- last.prev = oldLast;
- oldLast.next = last;
- post.prev = last;
+ if (item == null) throw new java.lang.NullPointerException("Cannot add null items");
+ Node oldLast = post.prev;
+ Node last = new Node();
+ last.item = item;
+ last.next = post;
+ last.prev = oldLast;
+ oldLast.next = last;
+ post.prev = last;
N++;
}
// Removes an item from the front of the deque.
public Item removeFirst() {
- if (isEmpty()) throw new java.util.NoSuchElementException("Deque is empty.");
- Node first = pre.next;
- Item item = first.item;
- pre.next = first.next;
- first.next.prev = pre;
- N--;
- return item;
+ if (isEmpty()) throw new java.util.NoSuchElementException("Deque is empty.");
+ Node first = pre.next;
+ Item item = first.item;
+ pre.next = first.next;
+ first.next.prev = pre;
+ N--;
+ return item;
}
// Removes an item from the back of the deque.
public Item removeLast() {
- if (isEmpty()) throw new java.util.NoSuchElementException("Deque is empty.");
- Node last = post.prev;
- Item item = last.item;
- post.prev = last.prev;
- last.prev.next = post;
- N--;
- return item;
+ if (isEmpty()) throw new java.util.NoSuchElementException("Deque is empty.");
+ Node last = post.prev;
+ Item item = last.item;
+ post.prev = last.prev;
+ last.prev.next = post;
+ N--;
+ return item;
}
// Returns an iterator object used for iteration through nodes in deque.
public Iterator<Item> iterator() {
- return new DequeIterator();
+ return new DequeIterator();
}
// Implementation of iterator object.
private class DequeIterator implements Iterator<Item> {
- private Node current = pre.next;
-
- public boolean hasNext() {
- return current != post;
- }
-
- public Item next() {
- Item item = current.item;
- current = current.next;
- return item;
- }
+ private Node current = pre.next;
+
+ public boolean hasNext() {
+ return current != post;
+ }
+
+ public Item next() {
+ Item item = current.item;
+ current = current.next;
+ return item;
+ }
- public void remove() {
- throw new java.lang.UnsupportedOperationException("Remove operation is not suppored.");
- }
+ public void remove() {
+ throw new java.lang.UnsupportedOperationException("Remove operation is not suppored.");
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.