Question

In: Computer Science

1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list....

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 

Solutions

Expert Solution

////////////////////////////////////////////////////////////////
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:


Related Solutions

1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list....
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:...
You are asked to delete the last occurrence of an item from a linked list. So,...
You are asked to delete the last occurrence of an item from a linked list. So, for instance: Input: 2 -> 3 -> 2 -> 4 Delete last occurrence of 2, result: 2 -> 3 -> 4 Implement the following method to do the deletion. You may NOT use or implement helper methods - all your code must be implemented inside the given method. You may NOT use recursion. public class Node { public int data; public Node next; }...
In C++, write a member method delete() that deletes a node from a linked list at...
In C++, write a member method delete() that deletes a node from a linked list at a random position. (It should first randomly generate that position. and then delete that node).
9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and...
9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and a PeopleNode class, complete the PeopleList class by writing findFirst() and findLast() methods. The findFirst() method should find the first occurrence of an age value in the linked list and return the corresponding node. Similarly, the findLast() method should find the last occurrence of the age value in the linked list and return the corresponding node. For both methods, if the age value is...
Suppose a linklist consists of more than 50 node, write a code to delete the tail...
Suppose a linklist consists of more than 50 node, write a code to delete the tail node (IN JAVA)
Suppose a linklist consists of more than 50 node, write a code to delete the tail...
Suppose a linklist consists of more than 50 node, write a code to delete the tail node   in java
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list with cells containing integers
Delete an item from a circular queue and return the index of the next item. (in...
Delete an item from a circular queue and return the index of the next item. (in Java)
Find the last node of a linked list of n elements whose index is a multiple...
Find the last node of a linked list of n elements whose index is a multiple of k (counted from 0). For example, if the list is 12 → 75 → 37 → 99 → 12 → 38 → 99 → 60 ↓ and k = 4, then you should return the second 12 (with index 4). Your algorithm should take O(n) time and use O(1) extra space. Implement the following method in LinkedList.java. public T lastK(int k) LinkedList.java. public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT