package linkedLists; public class DIntList { /** Doubly linked list with just a head, storing Integers*/ //contains recursive methods private DIntNode head; //head node of the list /** Default constructor that creates an empty list */ public DIntList(){ head=null; } //note new constructor here, to help with recursion /**constructor to create a list with head node n*/ public DIntList(DIntNode n){ head=n; if (head!=null) head.setPrev(null); } /**return the head of the list*/ public DIntNode getHead(){ return head; } /**set the head of the list*/ public void setHead(DIntNode n){ head=n; } /**is the list empty?*/ public boolean isEmpty(){ return(head==null); } /**add a new node at front of list */ public void addFirst(DIntNode n){ n.setNext(head); if(head!=null) head.setPrev(n); head=n; } /** add node to front of list containing a given value */ public void addValue(int i){ DIntNode temp = new DIntNode(i,null,null); addFirst(temp); } /**output positive elements of list*/ public void writePos(){ if (!isEmpty()){ if(head.getElement()>0) System.out.print(head.getElement()+" "); DIntList myList=new DIntList(head.getNext()); myList.writePos(); } } /**delete the first node containing 0 from list * and return the head of the list */ public DIntNode delete0(){ if(!isEmpty()){ if (head.getElement()==0) {head=head.getNext();return head;} else{ DIntList myList=new DIntList(head.getNext()); DIntNode temp = myList.delete0(); head.setNext(temp); if (temp!=null) temp.setPrev(head); return head; } } else return null; } /**delete all nodes containing the value v from list * and return the head of the list */ public DIntNode delete(int v){ if(!isEmpty()){ if (head.getElement()==v){ head=head.getNext(); return delete(v);} else{ DIntList myList=new DIntList(head.getNext()); DIntNode temp=myList.delete(v); head.setNext(temp); if (temp!=null) temp.setPrev(head); return head; } } else return null; } /**return size of list*/ public int size(){ if(isEmpty()) return 0; DIntList tempList=new DIntList(head.getNext()); return(1+tempList.size()); } /**returns the sum of the elements list, recursive*/ public int sum(){ if(head==null) return 0; // note didnt use an else here as program stops on reaching return DIntList tempList=new DIntList(head.getNext()); return(head.getElement()+ tempList.sum()); } /** String representation of list, recursive */ public String toString(){ DIntList myList; if (isEmpty()) return ""; else{myList=new DIntList(head.getNext()); return (head.getElement()+" " + myList.toString()); } } }