package linkedLists; /** Singly linked list with head and tail, storing strings*/ public class SLinkedList2 { private Node head; //head node of the list private Node last; //tail of the list /** Default constructor that creates an empty list */ public SLinkedList2(){ head=null; last=null; } /** /**is the list empty?*/ public boolean isEmpty(){ return(head==null); } /**Add a new node n to tail of list*/ public void addLast(Node n){ if (isEmpty()) {//have empty list head=n; last=n;} else { last.setNext(n); last=n; } } public void addLastValue(String s){ Node temp = new Node(s,null); addLast(temp); } /**add a new node at front of list */ public void addFirst(Node n){ if (isEmpty()){ head=n;last=n; } else{ n.setNext(head); head=n;} } /** add node to front of list containing a given value */ public void addValue(String s){ Node temp = new Node(s,null); addFirst(temp); } public void removeFirst(){ head=head.getNext(); } /** String representation of list */ public String toString(){ Node temp=head; String myString=""; while(!(temp==null)){ myString=myString + temp.getElement()+" "; temp=temp.getNext(); } return myString; } }