Commit 46375cc3 authored by michael lundquist's avatar michael lundquist
Browse files

Adding some comments and trying to figure out bubble sort

parent 6da8ebcb
Pipeline #3992 passed with stage
in 1 minute and 24 seconds
......@@ -12,19 +12,6 @@ public final class App {
* @param args The arguments of the program.
*/
public static void main(String[] args) {
System.out.println("Hello World!");
System.out.println("Hello World!" + 1/2);
}
/**
*
* (50 points) Use the class TNode and TLinkedList discussed in the class and implement the following methods:
* a.(30 points) Search method to perform linear searchbased on the name field of a scientist object. If the query is found, remove and return the matched Scientist object. b.(20 points) Create an add method that keeps nodes sorted in descending order.
* 2-(30 points) Use the DoublyLinkedList and DoublyNodejava classesdiscussed in the class and change it to supportthedatatype Scientist provided in class. Implement the following method:
* a.Implement the method indexOfElement(Scientist s) to return the index of aninput element (based on the name of a scientist). Suppose the elements are unique. Add this method to class DoubleLinkedList.
*/
/**
* 3-(20 points) Use the CircularLinkedList java class to implement the following method and add it to the CircularLinkedListjava class(middle is neither at the beginning nor at the end):public void addToMiddle(LNode newNode) {//TODO }
*/
}
package com.ds.fourthassignment;
/**
* Code taken from class and used
* to do the assignment
*
* @author Michael Lundquist
* Email: mlundqu2@masonlive.gmu.edu
* G00737340
*/
public class CircularLinkedList {
private LNode head;
private LNode tail;
......@@ -48,7 +51,8 @@ public class CircularLinkedList {
}
/**
* middle is neither at the beginning nor at the end
* middle is neither at the beginning nor at the end.
* Note, this will add to the beginning if no other data exists yet.
*
* @param newNode the node to be added to the middle of the list
*/
......
package com.ds.fourthassignment;
/**
* This now supports the scientist type
* Code taken from class and used
* to do the assignment
* This is a doubly linked list
*
* @author Michael Lundquist
* Email: mlundqu2@masonlive.gmu.edu
* G00737340
*/
public class DoublyLNode {
......
package com.ds.fourthassignment;
/**
* Code taken from class and used
* to do the assignment
*
* @author Michael Lundquist
* Email: mlundqu2@masonlive.gmu.edu
* G00737340
*/
public class DoublyLinkedList {
......@@ -87,12 +91,9 @@ public class DoublyLinkedList {
}
/**
* TODO
* Implement the method indexOfElement(Scientist s) to return the
* index of an input element (based on the name of a scientist). Suppose
* the elements are unique. Add this method to class DoubleLinkedList
* Searches for a scientist, and returns her index if she's found
*
* @return the index of this scientist, or -1
* @return the index of the scientist being searched for, or -1
*/
public int indexOfElement(Scientist s){
int curIndex = 0;
......
package com.ds.fourthassignment;
/**
* Code taken from class and used
* to do the assignment
*
* @author Michael Lundquist
* Email: mlundqu2@masonlive.gmu.edu
* G00737340
*/
public class LNode {
......
package com.ds.fourthassignment;
/**
* Code taken from class and used
* to do the assignment
*
* @author Michael Lundquist
* Email: mlundqu2@masonlive.gmu.edu
* G00737340
*/
public class Scientist implements Comparable<Scientist>{
private String name;
public Scientist(){}
......
......@@ -40,11 +40,61 @@ public class TLinkedList {
}
/**
* TODO: check if addSorted works for:
* TODO: check if addSorted works for: (nope, it's backwards)
*
* Create an add method that keeps nodes sorted in
descending order.
* descending order.
*
* Descending sorted
*/
public void bubbleSort(boolean ascending){
boolean sorted = false;
boolean swap = false;
System.out.println("before sort: " + toString());
while(!sorted){
sorted = true;
TNode curNode = head;
TNode nextNode = null;
TNode prevNode = null;
while(curNode.hasNext()){
swap = false;
nextNode = curNode.getNext();
int comparison = curNode.getData().compareTo(nextNode.getData());
if(!ascending && comparison < 0){//descending order
swap = true;
}else if(ascending && comparison > 0){
swap = true;
}
if(swap){
sorted = false;
//if(nextNode.hasNext()){
System.out.println("next data: " + nextNode.getData().getName());
curNode.setNext(nextNode.getNext());
//System.out.println(curNode.getNext().getData().getName());
System.out.println("cur data: " + curNode.getData().getName());
nextNode.setNext(curNode);
if(prevNode != null){
prevNode.setNext(nextNode);
}
}else if(prevNode != null){
prevNode.setNext(curNode);
System.out.println("prev data: " + prevNode.getData().getName());
}
if(curNode.hasNext()){
prevNode = curNode;
curNode = curNode.getNext();
System.out.println("prev data: " + prevNode.getData().getName());
}
System.out.println(toString());
}
System.out.println(toString());
}
}
/**
*This method adds nodes to the list and keeps the list sorted.
......@@ -83,10 +133,8 @@ descending order.
size++;
return;
}
previous = current;
current = current.getNext();
}
previous.setNext(node);
size++;
......@@ -135,7 +183,35 @@ descending order.
* @return The found Scientist or null if no scientist was found
*/
public Scientist search(Scientist needle){
for(
TNode curNode = this.head, lastNode = null;
curNode != null;
lastNode = curNode, curNode = curNode.getNext()
){
Scientist foundScientist = curNode.getData();
if(needle.compareTo(foundScientist) == 0){
if(lastNode != null){
lastNode.setNext(curNode.getNext());
}else{
head = curNode.getNext();
}
return foundScientist;
}
}
return null;
}
/**
* Used for testing.
*
* @author Michael Lundquist
*/
public String toString(){
String list = "";
for(TNode node = this.head; node != null; node = node.getNext()){
list += node.getData().getName();
}
return list;
}
}
\ No newline at end of file
package com.ds.fourthassignment;
/**
* Code taken from class and used
* to do the assignment
*
* @author Michael Lundquist
* Email: mlundqu2@masonlive.gmu.edu
* G00737340
*/
public class TNode {
private Scientist data;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment