2014-03-18

posted Mar 19, 2014, 8:31 AM by Samuel Konstantinovich   [ updated Mar 19, 2014, 8:31 AM ]
Code From Class:

public class Node{
    private String data;
    private Node next;
    
    public Node(String data){
this.data=data;
next=null;
    }
    public void setNext(Node next){
this.next=next;
    }
    public Node getNext(){
return next;
    }
    public String getData(){
return data;
    }
    public String toString(){
        //THIS IS A TERRIBLE METHOD, FIX IT TO NOT INCLUDE NEXT
return "Data: "+data+"\tNext: "+next;
    }
}

public class MyLinkedList{
    private Node head;
    public MyLinkedList(){
head=null;
    }
    public void add(String s){
Node tmp = new Node(s);
/*
head=tmp;//this does not add to the front what does it do?
*/
tmp.setNext(head);
head=tmp;
    }
    public String toString(){
String s="";
//how do we advance through the list?
    }
}


public class Driver{
    public static void main(String [] args){
Node n1;

n1 = new Node("Tommy");
System.out.println(n1);//n1.next is null

Node n2 = new Node("Gina");
System.out.println(n2);//n2.next is null

n1.setNext(n2);
//What will this do?
//System.out.println(n1);


n2.setNext(new Node("BonJovi"));

//What will this do?
//System.out.println(n1);
//how do we delete the node after n1?
//can we do it without knowing that n2 is the next node?


//lets pretend we didn't remove n2, 
//and see what this would do:
Node last = n1.getNext().getNext();
last.setNext(n1);
    }
}

Comments