Question

In: Computer Science

Write a remove(E val) method for DoublyLinkedList class This method remove the first occurrence of the...

Write a remove(E val) method for DoublyLinkedList class
This method remove the first occurrence of the node that contains the val.
.

Solutions

Expert Solution

Method to remove the first occurrence of node that contain val in doubly linked list is provided below.

void remove(E val) {
   // If head node is null then the list is empty
if(headNode==NULL) {
printf("Empty list");
}
   // If the list is not empty
   // If the head node contain required val
if(headNode->val == val) {
     
   // If there is more than one node
if(headNode->next != NULL) {
      
       // Update the previous link of next node of head node
       headNode->next->previous=NULL;
      
       // Now update the head node
       headNode=headNode->next;
      
}
       // If there is only one node
   else
   {
       // Make head node null
headNode = NULL;
}

}

// If the head node does not contain required value and there is only one node
else if(headNode->val != val && headNode->next == NULL) {
printf("Data not found");
}
   // Otherwise make the head node as curr node
curr = headNode;
  
   // Parse through the list until head node or required node found
while(curr->next != NULL && curr->val != val) {
   // Prev node denote the previous node of curr node
prev = curr;
curr = curr->next;
}
   // Check the curr node has required value
if(curr->val == val) {
   // Update the prev node next
prev->next = prev->next->next;
  
   // If the next of prev node is not null
   if(prev->next!=NULL)
   {
       // Update the previous link of prev node
       prev->next->previous=prev;
   }
   // Otherwise make prev as last node
   else
   {
       last=prev;
   }
  
   // free the curr node
free(curr);
}
// Otherwise val not found
else
printf("Data not found");
}


Related Solutions

Write a remove(E val) method for CircularlyLinkedList class This method remove the first occurrence of the...
Write a remove(E val) method for CircularlyLinkedList class This method remove the first occurrence of the node that contains the val.
3.) The function remove of the class arrayListType removes only the first occurrence of an element....
3.) The function remove of the class arrayListType removes only the first occurrence of an element. Add the function removeAll as an abstract function to the class arrayListType, which would remove all occurrences of a given element. Also, write the definition of the function removeAll in the class unorderedArrayListType and write a program to test this function. 4.) Add the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write...
Add to the class DoublyLinkedList a method called isSubSet ( DoublyLinkedList <Character> s1, DoublyLinkedList <Character> s2)...
Add to the class DoublyLinkedList a method called isSubSet ( DoublyLinkedList <Character> s1, DoublyLinkedList <Character> s2) that receives 2 DoublLinked lists of characters. The method will return true if s1 is subset of s2. Then don’t forget to test the method in the main method in Test class in Doubly package.
Method: DoublyLinkedList reverse(DoublyLinkedList list) Reverse() method accepts a DoublyLinkedList of Character as the argument, reverses the...
Method: DoublyLinkedList reverse(DoublyLinkedList list) Reverse() method accepts a DoublyLinkedList of Character as the argument, reverses the elements in the list, and returns the resulting list. For example: The given list is 'a' 'b' 'c' 'd' 'e' The return list should be 'e' 'd' 'c' 'b' 'a' How can we do this in Java?
implement the reverse() method that changes the ordering of the items within a doublylinkedlist class. don't...
implement the reverse() method that changes the ordering of the items within a doublylinkedlist class. don't return anything and make sure it executes without errors on lists with no items or one item example: fruits = DoublyLinkedList() fruits.append('apple') fruits.append('banana') fruits.append('cherry') for i in fruits: print(i) >> apple >> banana >> Charlie fruits.reverse() for i in fruits: print(i) >> cherry >> banana >> apple
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
write a method in java that insert First(Queue<E> s E e)that receives a queue and an...
write a method in java that insert First(Queue<E> s E e)that receives a queue and an elment, it inserts the element at the beginnings of queue
Write a member method named countMin.  This method is inside the class SingleLinkedList<E>. So it has the...
Write a member method named countMin.  This method is inside the class SingleLinkedList<E>. So it has the direct access to Node<E> class and data fields head, size. This method counts how many times the smallest data item in this SingleLinkedList<E> appears in the list.  It is assumed that the class passed to E implements the Comparable<E> interface. public int countMin() ---------------------- Write a method named endWith.  This method is OUTSIDE the class LinkedList<E>. It takes in two parameters: two LinkedList of Integers named...
anwer in kotlin fun main() { val text = "Kotlin".getFirstAndLast() val firstChar = text["first"] val lastChar...
anwer in kotlin fun main() { val text = "Kotlin".getFirstAndLast() val firstChar = text["first"] val lastChar = text["last"] // TODO 2 println() } // TODO 1 fun String.getFirstAndLast() = mapOf<String, Char>() TODO 1: Make the getFirstAndLast function the extension of the String class with the return type Map <String, Char> TODO 2: Add functions to print the values of the firstChar and lastChar variables in the console. there no more information from this task i dont know. this is the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT