Commit 17dd9f14 authored by michael lundquist's avatar michael lundquist
Browse files

Adding all the files from class that are used in the assignment. Also moving...

Adding all the files from class that are used in the assignment. Also moving the tests to the test folder
parent fda0750a
Pipeline #3976 failed with stage
in 1 minute and 16 seconds
......@@ -17,7 +17,10 @@ public final class App {
/**
*
* (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 DoublyLinkedListand 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.
* (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.
*/
......
public class DoublyLNode {
private String data;
private DoublyLNode next;
private DoublyLNode previous;
public DoublyLNode (String data, DoublyLNode previous , DoublyLNode next){
this.data = data;
this.next = next;
this.previous = previous;
}
public void setPrevious(DoublyLNode previous){
this.previous = previous;
}
public void setNext(DoublyLNode next){
this.next = next;
}
public boolean hasNext() { return (next==null ? false : true); }
public boolean hasPrevious() { return (previous==null ? false : true); }
public String getData() { return data;}
public DoublyLNode getNext() {return next; }
public DoublyLNode getPrevious() { return previous; }
}
\ No newline at end of file
public class DoublyLinkedList {
private DoublyLNode head;
private DoublyLNode tail;
private int size;
public DoublyLinkedList(){
head= null;
tail = null;
size=0;
}
public boolean isEmpty(){ return (head==null) ? true : false; }
public void addToHead(DoublyLNode newNode){
if(head==null) {
head = newNode;
tail= head;
size++;
}
else{
newNode.setPrevious(null);
newNode.setNext(head);
head.setPrevious(newNode);
head= newNode;
size++;
}
}
public void addToTail(DoublyLNode newNode){
if(tail==null) {
tail = newNode;
head=tail;
size++;
}
else{
tail.setNext(newNode);
newNode.setPrevious(tail);
tail = newNode;
size++;
}
}
public boolean remove(String query){
boolean flag=false;
if(size==0) return flag;
DoublyLNode current = head;
do{
if(current.getData().equals(query)){
//remove current
DoublyLNode prev = current.getPrevious();
DoublyLNode next =current.getNext();
prev.setNext(next);
next.setPrevious(prev);
current.setNext(null);
current.setPrevious(null);
current=null;
current = next;
flag=true;
}
else{
//go to the next element
current = current.getNext();
}
}while(current!=null);
return flag;
}
public String display(){
String out="";
DoublyLNode current =head;
if (current ==null) return null;
do{
out+=current.getData()+",";
current=current.getNext();
}while(current!=null);
return out;
}
}
\ No newline at end of file
public class LNode {
Integer data;
LNode link;
public LNode(Integer data , LNode link){
this.data = data;
this.link = link;
}
public Integer getData() {
return this.data;
}
public LNode getNext(){
return this.link;
}
public void setNext(LNode link){
this.link = link;
}
public void setData(Integer data){
this.data = data;
}
public boolean hasNext(){
if (link ==null ) return false;
else return true;
}
}
\ No newline at end of file
package com.ds.fourthassignment;
//import java.util.LinkedList;
public class TestTLinkedList {
/**
* @param args
*/
public static void main(String[] args) {
TNode n1 = new TNode(new Scientist("C"), null);
TNode n2 = new TNode(new Scientist("A"), null);
TNode n3 = new TNode(new Scientist("B"), null);
TNode n4 = new TNode(new Scientist("J"), null);
TLinkedList list = new TLinkedList();
list.addSorted(n1);
list.addSorted(n2);
list.addSorted(n3);
list.addSorted(n4);
while(!list.isEmpty()){
System.out.println(list.remove());
}
}
}
\ No newline at end of file
import javax.swing.JOptionPane;
public class Test_Circular_LinkedList {
public static void main(String args[]){
CircularLinkedList clist = new CircularLinkedList();
String menu = "1- Add to head\n2-Add to tail\n3- Display values\n4- Exit.";
int choice;
do{
choice =Integer.parseInt(JOptionPane.showInputDialog(menu));
switch(choice){
case 1:
clist.addToHead(new LNode(Integer.parseInt(JOptionPane.showInputDialog("Enter an integer value")), null));
break;
case 2:
clist.addToTail(new LNode(Integer.parseInt(JOptionPane.showInputDialog("Enter an integer value")), null));
break;
case 3:
JOptionPane.showMessageDialog(null, clist.display());
break;
case 4:
System.exit(0);
}
}while(choice!=4);
}
}
\ No newline at end of file
public class Test_DoublyLinkedList {
public static void main(String[] args){
DoublyLinkedList dlist = new DoublyLinkedList();
dlist.addToHead(new DoublyLNode("1", null, null));
dlist.addToHead(new DoublyLNode("2", null, null));
dlist.addToHead(new DoublyLNode("3", null, null));
dlist.addToHead(new DoublyLNode("4", null, null));
System.out.println(dlist.display());
dlist.remove("3");
System.out.println(dlist.display());
}
}
\ 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