Question

In: Computer Science

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.

Solutions

Expert Solution

Method to remove first occurrence of node that contain val 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->link != headNode) {
       // Make the head node as current
curr = headNode;
         
       // Parse thrrough the list until we get the previous node of head node
while(curr->link!=headNode) {
curr = curr->link;
}

       // Update the curr node link to next node after head
curr->link = headNode->link;
         
       // Update head node
headNode = headNode->link;
}
       // If there is only one node
   else
   {
       // Make head node null
headNode = NULL;
}

}

// If the head node does not contain required val and there is only one node
else if(headNode->val != val && headNode->link == headNode) {
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->link != headNode && curr->val != val) {
   // Prev node denote the previous node of curr node
prev = curr;
curr = curr->link;
}
   // Check the curr node has required val
if(curr->val == val) {
   // Update the prev node link
prev->link = prev->link->link;
  
   // free the curr node
free(curr);
}
// Otherwise val not found
else
printf("Data not found");
}


Related Solutions

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. .
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 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 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++
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
Part A) Java Programming Exercise #2: 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. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
art A) Java Programming Exercise #2: 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. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the...
Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the following method: public ExtLinkedList <E> mergeThreeLists (ExtLinkedList<E> list1, ExtLinkedList<E> list2) { } that returns an ExtLinkedList<E> which is the merged version of values from thislist, followed by values from list1, and then followed by values from list2.    For example, if E is Integer type and this listhas (5,3,1), list1has (8, 10,12,14), and list2has (22,23,24,25,26) in that order, then the returned list from a call to...
Briefly write about the first in first out process costing method.
Briefly write about the first in first out process costing method.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT