I'm a student currently at Rutgers University. I've taken Data Structures before, now I've been assigned a program that must add/multiply polynomials w/ a singly linked list. It's really simple, now my issue is that I must use a sorting technique (not required by the assignment itself just a necessary thing in order to complete it) and I chose to use merge sort as it's the most efficient (and simplest if written properly).
I can re-write it using iteration and write the getMiddleNode
method by iterating twice and just using a counter, but I don't see how that would help improve my code at all. I honestly believe this is the best implementation that anyone could write without being allowed the usage of imports.
[CODE]
/**
* Sorts the polynomial in order of degree from the <strong>highest to the lowest</strong> respectively.
* <br><br><strong>Note</strong>:<br>Utilizes merge sort technique.
*
* @param p The polynomial to be resorted.
* @return The front of the node.
*/
private void sort() {
if (poly == null)
return;
poly = sort(poly);
}
/**
* Recursively splits a node into two parts, left and right. It continues this process until
* the nodes are paired in two parts and then merges them in descending order.
*
* @param node The node to be split into left and right parts.
* @return The sorted node.
*/
private static Node sort(Node node) {
// If the linked list is empty or only has one element it is already sorted.
if (node == null || node.next == null)
return node;
// Find the middle of the linked list.
Node middle = getMiddle(node), middleNext = middle.next;
// Split the node in half.
middle.next = null;
// Merge the left and right half.
return merge(sort(node), sort(middleNext));
}
/**
* Compares the left and right side of each half.
*
* @param left
* @param right
* @return
*/
private static Node merge(Node left, Node right) {
if (left == null)
return right;
else if (right == null)
return left;
System.out.println(left.term +", "+right.term);
if (left.term.degree < right.term.degree) {
left.next = merge(left.next, right);
return left;
} else {
right.next = merge(left, right.next);
return right;
}
}
/**
* Iterates the linked list until the middle node is found.
*
* @param node The node to be iterated.
* @return The middle node of the linked list.
*/
private static Node getMiddle(Node node) {
if (node == null)
return null;
Node slow, fast;
slow = fast = node;
// Iterate two nodes concurrently
while (fast.next != null && fast.next.next != null) {
// Faster node reaches the end of the linked list two times faster than the slower node.
// Eliminates the need for a counter and re-iterating.
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
[/CODE]
This is the code that I've basically been taught in my previous data structure class. I wrote it in my notes a long time ago and used it in my program. My question is would my university consider this plagiarism. I did not think of the code whatsoever, I only wrote down documentation showing I understand how the code itself works.