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. .
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...
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 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++
Write a class file called lastname_digits.java. In it, write a method to compute and return the...
Write a class file called lastname_digits.java. In it, write a method to compute and return the total instances where the sum of digits of the array equals to the target. Write a demo file called lastname_digits_demo.java. Create an array of random 10 integers between 301 and 999. Also create an integer variable called target that is equal to a value between 5 and 10. Print the answer within this file. Example: If five of the 20 integers are equal to...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT