Skip to content

Commit

Permalink
fix the bugs in proj1a
Browse files Browse the repository at this point in the history
  • Loading branch information
shuitatata committed Sep 7, 2023
1 parent 8ad0e15 commit ef92e20
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 121 deletions.
26 changes: 16 additions & 10 deletions proj1a/ArrayDeque.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import java.io.ObjectStreamException;

public class ArrayDeque<T> {
public T[] items;
public int nextFirst;
public int nextLast;
public int size;
public int capacity;
private T[] items;
private int nextFirst;
private int nextLast;
private int size;
private int capacity;

/**
* Create an empty deque;
Expand All @@ -14,6 +12,8 @@ public ArrayDeque() {
size = 0;
capacity = 8;
items = (T[]) new Object[capacity];
nextFirst = prev(0);
nextLast = 0;
}


Expand Down Expand Up @@ -109,9 +109,15 @@ public T removeLast() {
}

public T get(int index) {
if ((index + nextFirst) % capacity <= nextLast) {
return items[(index + nextFirst) % capacity];
int Last = nextLast - 1;
if (Last <= nextFirst) {
Last += capacity;
}
if (index + nextFirst + 1 <= Last) {
return items[(index + nextFirst + 1) % capacity];
}
return null;
}
}


}
56 changes: 56 additions & 0 deletions proj1a/ArrayDequeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public static void printTestStatus(boolean passed) {
}
}

public static boolean checkNum(int expected, int actual) {
if (expected != actual) {
System.out.println("Num() returned " + actual + ",but expected: " + expected);
return false;
}
return true;
}

/**
* Adds a few things to the list, checking isEmpty() and size() are correct,
* finally printing the results.
Expand Down Expand Up @@ -85,9 +93,57 @@ public static void addRemoveTest() {

}

public static void addFirstRemoveLastTest() {
System.out.println("Running addFirst/removeLast test.");
ArrayDeque<Integer> ad1 = new ArrayDeque<Integer>();

ad1.addFirst(1);
ad1.addFirst(2);
ad1.printDeque();

int remove1 = ad1.removeLast();


ad1.addFirst(3);
ad1.addFirst(4);
int remove2 = ad1.removeLast();

ad1.printDeque();

boolean passed = checkNum(1, remove1) &&
checkNum(2, remove2) &&
checkNum(ad1.get(0), 4) &&
checkNum(ad1.get(1), 3);

printTestStatus(passed);
}

public static void addLastRemoveFirstTest() {
System.out.println("Running addLast/removeFirst test.");
ArrayDeque<Integer> ad1 = new ArrayDeque<Integer>();

ad1.addLast(1);
ad1.addLast(2);
int remove1 = ad1.removeFirst();
ad1.addLast(3);
ad1.addLast(4);
int remove2 = ad1.removeFirst();

ad1.printDeque();

boolean passed = checkNum(1, remove1) &&
checkNum(2, remove2) &&
checkNum(ad1.get(0), 3) &&
checkNum(ad1.get(1), 4);

printTestStatus(passed);
}

public static void main(String[] args) {
System.out.println("Running tests.\n");
addIsEmptySizeTest();
addRemoveTest();
addFirstRemoveLastTest();
addLastRemoveFirstTest();
}
}
54 changes: 33 additions & 21 deletions proj1a/LinkedListDeque.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
public class LinkedListDeque <T> {
public class LinkedListDeque<T> {
private int size;
private TNode sentinel;

public class TNode {
public TNode prev;
public TNode next;
private TNode prev;
private TNode next;
T item;

public TNode(T item, TNode prev, TNode next) {
this.item = item;
this.prev = prev;
this. next = next;
this.next = next;
}
}

Expand All @@ -27,40 +27,46 @@ public LinkedListDeque(LinkedListDeque other) {
sentinel = new TNode(null, null, null);
sentinel.prev = sentinel;
sentinel.next = sentinel;
TNode node = other.sentinel.next;
while(node != other.sentinel) {
TNode node = other.sentinel.next;
while (node != other.sentinel) {
this.addFirst(node.item);
node = node.next;
}
}

/**
* Add an item of type T to the beginning of the deque.
*
* @param item The element that is waiting to be added.
*/
public void addFirst(T item) {
TNode node = new TNode(item, sentinel.prev, sentinel.next);
TNode node = new TNode(item, sentinel, sentinel.next);
sentinel.next.prev = node;
sentinel.next = node;
if(this.isEmpty()) {
if (this.isEmpty()) {
sentinel.prev = node;
}
size++;
}

/**
* Add an item of type T to the end of the deque;
*
* @param item The element that is waiting to be added.
*/
public void addLast(T item) {
TNode node = new TNode(item, sentinel.prev.prev, sentinel);
TNode node = new TNode(item, sentinel.prev, sentinel);
sentinel.prev.next = node;
sentinel.prev = node;
if(this.isEmpty()) {
if (this.isEmpty()) {
sentinel.next = node;
}
size++;
}

/**
* Return whether the list is empty
*
* @return A boolean value representing whether the deque is empty.
*/
public boolean isEmpty() {
Expand All @@ -69,6 +75,7 @@ public boolean isEmpty() {

/**
* Return the number of items in the deque
*
* @return An integer value representing the size of the deque.
*/
public int size() {
Expand All @@ -80,7 +87,7 @@ public int size() {
*/
public void printDeque() {
TNode node = sentinel.next;
while(node != sentinel) {
while (node != sentinel) {
System.out.print(node.item + " ");
node = node.next;
}
Expand All @@ -89,64 +96,69 @@ public void printDeque() {

/**
* Removes and returns the item at the front of the deque.
*
* @return The item at the front of the deque. If no such item exists, return null.
*/
public T removeFirst() {
if(this.isEmpty()) {
if (this.isEmpty()) {
return null;
}
T result = sentinel.next.item;
sentinel.next.next = sentinel.next;
sentinel.next = sentinel.next.next;
sentinel.next.prev = sentinel;
size--;
return result;
}

/**
* Removes and returns the item at the back of the deque.
*
* @return The item at the back of the deque. If no such item exists, returns null.
*/
public T removeLast() {
if(this.isEmpty()) {
if (this.isEmpty()) {
return null;
}
T result = sentinel.prev.item;
sentinel.prev.prev = sentinel.prev;
sentinel.prev = sentinel.prev.prev;
sentinel.prev.next = sentinel;
size--;
return result;
}

/**
* Gets the item at the given index.
*
* @param index An integer.
* @return The item at the given index. If no such item exists, returns null.
*/
public T get(int index) {
if(index >= size) {
if (index >= size) {
return null;
}
TNode node = sentinel.next;
while(index > 0 ) {
while (index > 0) {
node = node.next;
index--;
}
return node.item;
}

public T getRecursiveHelper(TNode begin, int index) {
if(index==0){
if (index == 0) {
return begin.item;
}
return this.getRecursiveHelper(begin.next, index-1);
return this.getRecursiveHelper(begin.next, index - 1);
}

/**
* Gets the
*/
public T getRecursive(int index) {
if(index >= size) {
if (index >= size) {
return null;
}
return getRecursiveHelper(sentinel.next, index);
}
}
}

Loading

0 comments on commit ef92e20

Please sign in to comment.