In: Computer Science
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list. So if the item is 7 and the list is [1,3,7,4,7,3,7,2], the result is [1,3,7,4,7,3,2]
2- Use LinkList. Write removeAll(int
n). Deletes all occurrences of an item n from a linked
list. So if the item is 7 and the list1 is [1,3,7,4,7,3,2] , then
list1.removeAll(7) then list1 becomes [1,3,4,3,2].
Demonstrate by displaying the list contents before
and after calling the above methods. Eg:
lst1
[1,3,7,4,7,3,7,2]
lst1.removelast(7)
[1,3,7,4,7,3,2]
lst1.removeall(7)
[1,3,4,3,2]
------------------------------------------------------------------------------------------------------------------------------------------------------------------
// linkList.java
// demonstrates linked list
// to run this program: C>java LinkListApp
////////////////////////////////////////////////////////////////
class Link
{
public int iData; // data item
public double dData; // data item
public Link next; // next link in list
//
-------------------------------------------------------------
public Link(int id, double dd) // constructor
{
iData = id; // initialize data
dData = dd; // ('next' is automatically
} // set to null)
//
-------------------------------------------------------------
public void displayLink() // display ourself
{
System.out.print("{" + iData + ", " + dData + "} ");
}
} // end class Link
////////////////////////////////////////////////////////////////
class LinkList
{
private Link first; // ref to first link on list
//
-------------------------------------------------------------
public LinkList() // constructor
{
first = null; // no links on list yet
}
//
-------------------------------------------------------------
public boolean isEmpty() // true if list is empty
{
return (first==null);
}
//
-------------------------------------------------------------
// insert at start of list
public void insertFirst(int id, double dd)
{ // make new link
Link newLink = new Link(id, dd);
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
//
-------------------------------------------------------------
public Link deleteFirst() // delete first item
{ // (assumes list not empty)
Link temp = first; // save reference to link
first = first.next; // delete it: first-->old next
return temp; // return deleted link
}
//
-------------------------------------------------------------
public void displayList()
{
System.out.print("List (first-->last): ");
Link current = first; // start at beginning of list
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}
//
-------------------------------------------------------------
} // end class LinkList
////////////////////////////////////////////////////////////////
class LinkListApp
{
public static void main(String[] args)
{
LinkList theList = new LinkList(); // make new list
theList.insertFirst(22, 2.99); // insert four items
theList.insertFirst(44, 4.99);
theList.insertFirst(66, 6.99);
theList.insertFirst(88, 8.99);
theList.displayList(); // display list
while( !theList.isEmpty() ) // until it's empty,
{
Link aLink = theList.deleteFirst(); // delete link
System.out.print("Deleted "); // display it
aLink.displayLink();
System.out.println("");
}
theList.displayList(); // display list
} // end main()
} // end class LinkListApp
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Class LinkListAppTest.Java is just for testing. Please do not edit in any form. class LinkListAppTest { public static void main(String[] args) // NEW MAIN { LinkList lst1 = new LinkList(); // Since the available add method is a pre-add to the first we go in reverse order lst1.insertFirst(8,2); // last digit lst1.insertFirst(7,7); // entering next to last lst1.insertFirst(6,3); // until we get to the the beginning lst1.insertFirst(5,7); lst1.insertFirst(4,4); lst1.insertFirst(3,7); lst1.insertFirst(2,3); lst1.insertFirst(1,1); System.out.println("lst1"); // list the name of the linked-list lst1.displayList(); // pre-print the entered list unaltered System.out.println("lst1.removeLast(7)"); // list the action to be taken lst1.removeLast(7); // complete the action to remove the last dData == 7; lst1.displayList(); // print list post removal of the last dData == 7; System.out.println("1st1.removeAll(7)"); // list the action to be taken lst1.removeAll(7); // complete the action to remove all remaining dData == 7; lst1.displayList(); // print list post removal of all remaining dData == 7; } // end main() } // end class
////////////////////////////////////////////////////////////////
class Link
{
public int iData; // data item
public double dData; // data item
public Link next; // next link in list
//
-------------------------------------------------------------
public Link(int id, double dd) // constructor
{
iData = id; // initialize data
dData = dd; // ('next' is automatically
} // set to null)
//
-------------------------------------------------------------
public void displayLink() // display ourself
{
System.out.print("{" + iData + ", " + dData + "} ");
}
} // end class Link
////////////////////////////////////////////////////////////////
class LinkList
{
private Link first; // ref to first link on list
//
-------------------------------------------------------------
public LinkList() // constructor
{
first = null; // no links on list yet
}
//
-------------------------------------------------------------
public boolean isEmpty() // true if list is empty
{
return (first==null);
}
//
-------------------------------------------------------------
// insert at start of list
public void insertFirst(int id, double dd)
{ // make new link
Link newLink = new Link(id, dd);
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
//
-------------------------------------------------------------
public Link deleteFirst() // delete first item
{ // (assumes list not empty)
Link temp = first; // save reference to link
first = first.next; // delete it: first-->old next
return temp; // return deleted link
}
//
-------------------------------------------------------------
public void displayList()
{
System.out.print("List (first-->last): ");
Link current = first; // start at beginning of list
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}
//
-------------------------------------------------------------
// method to remove the last node with dData = n
public void removeLast(int n)
{
Link curr = first; // set curr to
first
Link pre = null; // set pre to node
previous to curr
Link currN = null; // node to
contain the last node with dData = 7
Link preN = null; // node previous
to currN
// loop over the list
while(curr != null)
{
if(curr.dData ==
n) // curr's dData = n
{
// set preN and currN to pre and curr
respectively
preN = pre;
currN = curr;
}
pre =
curr;
curr =
curr.next;
}
// if n was found in the list
if(currN != null)
{
if(preN == null)
// currN is the first node
first = first.next;
else // currN is
not the first node
preN.next = currN.next;
}
}
// method to remove all nodes with dData = n
public void removeAll(int n)
{
Link curr = first; // set curr to
first
Link pre = null; // node previous
to curr
// loop over the list
while(curr != null)
{
if(curr.dData ==
n) // curr's dData = n
{
if(pre == null) // curr is the first node
first = first.next; // set
first to next of first
else // curr is not first node
pre.next = curr.next; // set
next of pre to curr
}else // curr's
dData != n
pre = curr; // set pre to curr
curr =
curr.next; // set curr to next of curr
}
}
} // end class LinkList
////////////////////////////////////////////////////////////////
class LinkListAppTest { public static void main(String[] args) // NEW MAIN { LinkList lst1 = new LinkList(); // Since the available add method is a pre-add to the first we go in reverse order lst1.insertFirst(8,2); // last digit lst1.insertFirst(7,7); // entering next to last lst1.insertFirst(6,3); // until we get to the the beginning lst1.insertFirst(5,7); lst1.insertFirst(4,4); lst1.insertFirst(3,7); lst1.insertFirst(2,3); lst1.insertFirst(1,1); System.out.println("lst1"); // list the name of the linked-list lst1.displayList(); // pre-print the entered list unaltered System.out.println("lst1.removeLast(7)"); // list the action to be taken lst1.removeLast(7); // complete the action to remove the last dData == 7; lst1.displayList(); // print list post removal of the last dData == 7; System.out.println("1st1.removeAll(7)"); // list the action to be taken lst1.removeAll(7); // complete the action to remove all remaining dData == 7; lst1.displayList(); // print list post removal of all remaining dData == 7; } // end main() } // end class
Output: