Commit 35c71586 authored by michael lundquist's avatar michael lundquist
Browse files

Adding a first attempt at all the methods required in this homework.

parent 1e4d219a
Pipeline #3984 failed with stage
in 1 minute and 20 seconds
......@@ -42,11 +42,28 @@ public class CircularLinkedList {
size++;
}
/**
* middle is neither at the beginning nor at the end
*
* @param newNode the node to be added to the middle of the list
*/
public void addToMiddle(LNode newNode) {
int curPos = 0;
LNode curNode = head;
if(this.size > 2){
while(curPos < this.size/2){
curPos ++;
curNode = curNode.getNext();
}
}
newNode.setNext(curNode.getNext());
curNode.setNext(newNode);
}
public String display(){
String out="";
if (head==null) return null;
LNode current = head;
LNode current = head;
do{
out+=current.getData() +",";
if (current==tail) break;
......
package com.ds.fourthassignment;
/**
* This now supports the scientist type
*/
public class DoublyLNode {
private String data;
private Scientist data;
private DoublyLNode next;
private DoublyLNode previous;
public DoublyLNode (String data, DoublyLNode previous , DoublyLNode next){
public DoublyLNode (Scientist data, DoublyLNode previous , DoublyLNode next){
this.data = data;
this.next = next;
this.previous = previous;
......@@ -24,7 +28,7 @@ public class DoublyLNode {
public boolean hasPrevious() { return (previous==null ? false : true); }
public String getData() { return data;}
public Scientist getData() { return data;}
public DoublyLNode getNext() {return next; }
public DoublyLNode getPrevious() { return previous; }
......
......@@ -82,4 +82,26 @@ public class DoublyLinkedList {
return out;
}
/**
* 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
*
* @return the index of this scientist, or -1
*/
public int indexOfElement(Scientist s){
DoublyLNode curNode = head;
int curIndex = 0;
do{
if(curNode.getData().compareTo(s) == 0){
return curIndex;
}
curNode = curNode.getNext();
curIndex ++;
} while(curNode.hasNext());
return -1;
}
}
\ No newline at end of file
......@@ -39,7 +39,12 @@ public class TLinkedList {
}
/**
* TODO: check if addSorted works for:
*
* Create an add method that keeps nodes sorted in
descending order.
*/
/**
*This method adds nodes to the list and keeps the list sorted.
......@@ -85,7 +90,6 @@ public class TLinkedList {
}
previous.setNext(node);
size++;
}
/**
......@@ -122,4 +126,16 @@ public class TLinkedList {
*/
public int getSize(){ return size; }
/**
* Search method to perform linear search based on the
* name field of a scientist object. If the query is found, remove and
* return the matched Scientist object
*
* @param needle The scientist your searching for
* @return The found Scientist or null if no scientist was found
*/
public Scientist search(Scientist needle){
return null;
}
}
\ No newline at end of file
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