Question

In: Computer Science

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;
}

// Deletes LAST occurrence of given item from a linked list,
// given a front pointer to the first node in the list.
// Returns pointer to first node of the updated linked list.
// Input list could be empty.
// Throws NoSuchElementException if item is not in the linked
// list.

public static Node deleteLastOccurrence(Node front, int item)
throws NoSuchElementException {

// COMPLETE THIS METHOD

}

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks


//required method
public static Node deleteLastOccurrence(Node front, int item)
                throws NoSuchElementException {
        // initializing a boolean flag to false
        boolean found = false;
        // two Node references, one to store previous node of current node under
        // process, and other is to store previous of the target node if found.
        Node prev = null, prevTarget = null;
        // looping through each node
        for (Node n = front; n != null; n = n.next) {
                // checking if this node contains item
                if (n.data == item) {
                        // setting current prev node as prevTarget
                        prevTarget = prev;
                        // setting flag to true
                        found = true;
                        // useful tip: add a break; statement here if you want to remove
                        // the first occurrence instead.
                }
                // setting n as new prev
                prev = n;
        }
        // now if found is false throwing NoSuchElementException
        if (!found) {
                throw new NoSuchElementException();
        }
        // otherwise if prevTarget is null, that means we have to remove front
        // node
        if (prevTarget == null) {
                front = front.next; // advancing front
        } else {
                // otherwise removing node between prevTarget and
                // prevTarget.next.next
                prevTarget.next = prevTarget.next.next;
        }
        // returning front node
        return front;
}

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:...
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:...
C PROGRAMMING Create the int delete(int key) function so that it deletes the LAST occurrence of...
C PROGRAMMING Create the int delete(int key) function so that it deletes the LAST occurrence of a given number in the linked list Make sure the parameter for this delete function is (int key). Also, use these variables and global Nodes BELOW as this is a DOUBLY LINKED LIST!!! #include #include typedef struct node {             int data;             struct node *next;             struct node *prev; } Node; Node *head; Node *tail; ----------------------- So, the function has to look like...
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...
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)
unix Delete the second character from every line in a file Delete the last word from...
unix Delete the second character from every line in a file Delete the last word from every line in a file. Swap the first and second letter of every line in a file. Swap the first and last characters of every line in a file.
I'm working on a to-do list program in Python 2. I'm trying to delete an item...
I'm working on a to-do list program in Python 2. I'm trying to delete an item from the list and I'm not sure what I'm missing to do that. I haven't been able to get it to delete by string or by index number. Also, I'm trying to get the menu to run again after the user completes the add/delete/etc options. Do I need to put menu() menu_option = int(input("Welcome to your To-Do List. Please choose and option to continue:...
IN JAVA: Delete an item from a circular queue and return the index of the next...
IN JAVA: Delete an item from a circular queue and return the index of the next item
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]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT