classNode
{public intval;publicNode next;public Node(intval)
{this.val =val;
}
}classListHead
{public intcount ;publicNode next;publicListHead()
{this.count = 0;this.next = null;
}
}classList
{publicListHead head;privateNode current;publicList()
{this.head = newListHead();this.current = null;
}public void addNew(intval)
{
Node newNode= newNode(val);if(this.head.next == null)this.head.next =newNode;else
this.current.next =newNode;this.current =newNode;
}public boolean contains(intval)
{for(Node current = this.head.next; current != null; current =current.next)
{if(current.val ==val)return true;
}return false;
}public void replace(int val,intnewVal)
{for(Node current = this.head.next; current != null; current =current.next)
{if(current.val ==val)
{
current.val=newVal;return;
}
}
}public void replaceAll(int val, intnewVal)
{for(Node current = this.head.next; current != null; current =current.next)
{if(current.val ==val)
current.val=newVal;
}
}public void remove(intval)
{
Node theOneToBeRemoved= null;if(this.head.next != null && this.head.next.val ==val)
{
theOneToBeRemoved= this.head.next;this.head.next =theOneToBeRemoved.next;
theOneToBeRemoved.next= null;return;
}for(Node current = this.head.next; current != null; current =current.next)
{if(current.next != null && current.next.val ==val )
{
theOneToBeRemoved=current.next;
current.next=theOneToBeRemoved.next;
theOneToBeRemoved.next= null;return;
}
}
}public void removeAll(intval)
{
Node theOneToBeRemoved= null;while(this.head.next != null && this.head.next.val ==val)
{
theOneToBeRemoved= this.head.next;this.head.next =theOneToBeRemoved.next;
theOneToBeRemoved.next= null;
}for(Node current = this.head.next; current != null; current =current.next)
{while(current.next != null && current.next.val ==val)
{
theOneToBeRemoved=current.next;
current.next=theOneToBeRemoved.next;
theOneToBeRemoved.next= null;
}
}
}public voidclear()
{
Node theOneToBeRemoved= null;while(this.head.next != null)
{
theOneToBeRemoved= this.head.next;this.head.next =theOneToBeRemoved.next;
theOneToBeRemoved.next= null;
}
}public voidprintList()
{for(Node current = this.head.next; current != null; current =current.next)
{
System.out.print(current.val + " ");
}
System.out.println("\n=================================");
}
}public classHello
{public static voidmain(String[] args)
{
List myList= newList();
myList.addNew(1);
myList.addNew(1);
myList.addNew(1);
myList.addNew(1);
myList.addNew(1);
myList.addNew(2);
myList.addNew(1);
myList.addNew(2);
myList.addNew(1);
myList.addNew(2);
myList.addNew(7);
myList.addNew(7);
myList.addNew(8);
myList.addNew(2);
myList.printList();
myList.replaceAll(2,9);
myList.removeAll(7);
myList.printList();
}
}