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...
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
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
Implement a class called DoublyLinkedList. In the main function, instantiate the DoublyLinkedList class and make sure that there is a user loop and a menu so that the user can access all the list operators.
C++  Implement a class called DoublyLinkedList. In the main function, instantiate the DoublyLinkedList class and make sure that there is a user loop and a menu so that the user can access all the list operators. You should implement the following operators, and any others that you may deem best. DestroyList InitializeList GetFirst InsertFirst, InsertLast, Insert DeleteFirst, DeleteLast, Delete IsEmpty Length Print, ReversePrint
Write a class with 2 methods. The first method determines if any two sides added together...
Write a class with 2 methods. The first method determines if any two sides added together are greater than the remaining side. The second method calculates the triangle’s area. // Method 1: If sum of any two sides is greater than the remaining side, return true public static boolean isValid(double sid1, double side2, double side3) // Method 2: Returns the triangle area public static double area(double side1, double side2, double side3) Develop a test program that takes in the three...
Write a static method remove(int v, int[] in) that will return a new array of the...
Write a static method remove(int v, int[] in) that will return a new array of the integers in the given array, but with the value v removed. For example, if v is 3 and in contains 0, 1, 3, 2, 3, 0, 3, and 1, the method will return an array containing 0, 1, 2, 0, and 1. Hint: You can follow two steps to solve this problem: Create an array in the method, let say you called it result....
Write methods contains and remove for the BinarySearchTree class. Use methods find and delete to do...
Write methods contains and remove for the BinarySearchTree class. Use methods find and delete to do the work
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
Write a program to detect the deadlock occurrence and write the sequence if there is a...
Write a program to detect the deadlock occurrence and write the sequence if there is a safe state. Noted: C or C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT