aboutsummaryrefslogtreecommitdiffstats
path: root/csci1913
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--csci1913/Java/lab11_strap012.java42
-rw-r--r--csci1913/Java/project2_strap012.java45
2 files changed, 60 insertions, 27 deletions
diff --git a/csci1913/Java/lab11_strap012.java b/csci1913/Java/lab11_strap012.java
index 6b4bbb5..7eb146a 100644
--- a/csci1913/Java/lab11_strap012.java
+++ b/csci1913/Java/lab11_strap012.java
@@ -4,7 +4,7 @@ class Deque<Base> {
private Base object;
private Node left;
private Node right;
- private Node(Base o,Node l,Node r) {
+ private Node(Base o, Node l, Node r) {
object = o;
left = l; right = r;
}
@@ -13,20 +13,40 @@ class Deque<Base> {
head = new Node(null, null, null);
head.left=head; head.right=head;
}
- public void enqueueFront(Base object) {
-
- }
public void enqueueRear(Base object) {
-
+ Node tempL = head;
+ Node newNode = new Node(object, tempL.left, tempL);
+ tempL.left.right = newNode;
+ tempL.left = newNode;
}
- public Base dequeueFront() {
-
+ public void enqueueFront(Base object) {
+ Node tempR = head;
+ Node newNode = new Node(object, tempR, tempR.right);
+ tempR.right.left = newNode;
+ tempR.right = newNode;
}
public Base dequeueRear() {
-
+ if (isEmpty()) {
+ throw new IllegalStateException();
+ } else {
+ Node temp = head.left;
+ temp.left.right = head;
+ head.left = temp.left;
+ return temp.object;
+ }
+ }
+ public Base dequeueFront() {
+ if (isEmpty()) {
+ throw new IllegalStateException();
+ } else {
+ Node temp = head.right;
+ temp.right.left = head;
+ head.right = temp.right;
+ return temp.object;
+ }
}
public Boolean isEmpty() {
- return head.right==null && head.left==null;
+ return head.right==head && head.left==head;
}
}
@@ -74,8 +94,8 @@ class ObservationDeque{
deque.enqueueFront("B");
deque.enqueueFront("C");
- System.out.println(deque.dequeueFront()); // C 2
- points. System.out.println(deque.dequeueFront()); // B 2 points.
+ System.out.println(deque.dequeueFront()); // C 2 points.
+ System.out.println(deque.dequeueFront()); // B 2 points.
System.out.println(deque.dequeueFront()); // A 2 points.
System.out.println(deque.isEmpty()); // true 2 points.
diff --git a/csci1913/Java/project2_strap012.java b/csci1913/Java/project2_strap012.java
index 6c4ee34..3cbfa80 100644
--- a/csci1913/Java/project2_strap012.java
+++ b/csci1913/Java/project2_strap012.java
@@ -54,11 +54,12 @@ class Sort {
// NUMBER slots, without making new NODEs.
private static Node sortNodes(Node unsorted) {
- Node left = null, right = null, sorted = null;
+ Node left = null, right = null;
if (unsorted==null || unsorted.next==null) {
//unsorted is either empty or of length 1
return unsorted;
- } //Halving phase
+ }
+ //Halving phase
else {
int step = 1;
while (unsorted != null) {
@@ -79,34 +80,40 @@ class Sort {
//Sorting (recursive) Phase
left = sortNodes(left);
right = sortNodes(right);
- //Combining phase
- Node end = null;
+
+ //Combining phase, variables needed for Q funs
+ Node end = null, temp = null, sorted = null;
+ //Special Initial Case
if (left.number < right.number) {
- Node temp = left.next;
- sorted = left; end = left;
- left.next = null;
+ sorted = left;
+ end = left;
+ temp = left.next;
left = temp;
end.next = null;
} else {
- Node temp = right.next;
- end = right; sorted = right;
- right.next = null;
+ temp = right.next;
+ sorted = right;
+ end = right;
right = temp;
end.next = null;
}
+ //Loop for rearranging pointers. Easily the hardest part of the assignment
while (left != null && right != null) {
if (left.number < right.number) {
- Node temp = left.next;
+ end.next = left;
end = left;
- right = temp;
+ temp = left.next;
end.next = null;
- } else {
- Node temp = right.next;
+ left = temp;
+ } else { //Right is smaller or equal, put it at the end.
+ end.next = right;
end = right;
+ temp = right.next;
+ end.next = null;
right = temp;
- end.next = null;
}
- }
+ }
+
if (left == null) {
end.next=right;
} else if (right == null) {
@@ -131,5 +138,11 @@ class Sort {
writeNodes(sortNodes(makeNodes(3, 1, 4, 5, 9, 2, 6, 8, 7)));
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
+
+ writeNodes(sortNodes(makeNodes(5,5,5,1,2,3,6,90,12,1,14)));
+ // [1, 1, 2, 3, 5, 5, 5, 6, 12, 14, 90]
+
+ writeNodes(sortNodes(makeNodes(1, 100, 6, 100, 1, 15, 10000, 12, 2, 0, -15)));
+ // [-15, 0, 1, 1, 2, 6, 12, 15, 100, 100, 10000]
}
} \ No newline at end of file