package linkedLists; public class IntList { /** Singly linked list with just a head, storing Integers*/ //contains recursive methods private IntNode head; //head node of the list /** Default constructor that creates an empty list */ public IntList(){ head=null; } //note new constructor here, to help with recursion /**constructor to create a list with head node n*/ public IntList(IntNode n){ head=n; } /**return the head of the list*/ public IntNode getHead(){ return head; } /**set the head of the list*/ public void setHead(IntNode n){ head=n; } /**is the list empty?*/ public boolean isEmpty(){ return(head==null); } /**add a new node at front of list */ public void addFirst(IntNode n){ n.setNext(head); head=n; } /** remove node from front of list */ public void removeFirst(){ if (!isEmpty()) head=head.getNext(); else System.out.println("Can't remove from list, list is empty"); } /** add node to front of list containing a given value */ public void addValue(Integer i){ IntNode temp = new IntNode(i,null); addFirst(temp); } /** is Node containing i in list? */ public boolean inList(int i){ if (isEmpty()||head.getElement()>i) return false; else{ IntList tempList; if(head.getElement()==i) return true; else {tempList=new IntList(head.getNext()); return tempList.inList(i);} } } /** Add new value in non-decreasing order into list */ public void addValueInOrder(Integer i){ if (isEmpty()||head.getElement()>i) addValue(i); else{IntNode temp; IntList tempList; if (head.getNext()==null){temp=new IntNode(i,null);head.setNext(temp);} else{tempList=new IntList(head.getNext());tempList.addValueInOrder(i);head.setNext(tempList.getHead()); } } } /**remove all nodes containing element with a given value */ public void removeValue(int i){ if (!(isEmpty())&&(head.getElement()<=i)){ IntList tempList; if(head.getElement()==i) { head=head.getNext(); if(head!=null) this.removeValue(i); } else{ if(head.getNext()!=null){ tempList=new IntList(head.getNext()); tempList.removeValue(i); head.setNext(tempList.getHead()); } } } } /**output positive elements of list*/ public void writePos(){ if (!isEmpty()){ if(head.getElement()>0) System.out.print(head.getElement()+" "); IntList myList=new IntList(head.getNext()); myList.writePos(); } } /**delete the first node containing 0 from list * and return the head of the list */ public IntNode delete0(){ if(!isEmpty()){ if (head.getElement()==0) {head=head.getNext();return head;} else{ IntList myList=new IntList(head.getNext()); head.setNext(myList.delete0()); return head; } } else return null; } /**delete all nodes containing the value v from list * and return the head of the list */ public IntNode delete(int v){ if(!isEmpty()){ if (head.getElement()==v){ head=head.getNext(); return delete(v);} else{ IntList myList=new IntList(head.getNext()); head.setNext(myList.delete(v)); return head; } } else return null; } /**return size of list*/ public int size(){ if(isEmpty()) return 0; IntList tempList=new IntList(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 IntList tempList=new IntList(head.getNext()); return(head.getElement()+ tempList.sum()); } /** String representation of list, recursive */ public String toString(){ IntList myList; if (isEmpty()) return ""; else{myList=new IntList(head.getNext()); return (head.getElement()+" " + myList.toString()); } } }