Question

Write a member method remove( ) which randomly generate a position and deletes the node at...

Write a member method remove( ) which randomly generate a position and deletes the node at that position from the linked list.

Node: Please use C++ language, only need function.

Homework Answers

Answer #1

/*If you any query do comment in the comment section else like the solution*/

node *remove(node *head) {
    node *ptr = head;
    node *prev = NULL;
    int count = 0;
    int random = rand();
    while(ptr != NULL) { 
        if(count == random) {
            if(ptr == head) {
                ptr = head->next;
                head->next = NULL;
                delete(head);
                head = ptr;
                return head;
            } else {
                prev->next = ptr->next;
                delete(ptr);
                return head;
            }
        }
        count++;
        ptr = ptr->next;
    }
    return head;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(c) language What is the code that is required to define the node and the header...
(c) language What is the code that is required to define the node and the header for a linked list of the ages of your family members in decreasing order along with the name you refer to that person as (the nickname e.g. dad 59). Specify only the typedef of the node and the declaration of the header. YOU DO NOT NEED THE CODE To make the linked list.
Assume that struct Node{ int item; Node*link; }; Write function NodePtr list_search(NodePtr head, int target); The...
Assume that struct Node{ int item; Node*link; }; Write function NodePtr list_search(NodePtr head, int target); The function will search through the linked list that is pointed by head and return a NodePtr that points to the Node that contains target as its item. If there is no such a Node, NULL will be returned. c++
LANGUAGE IS C++ 1- A link based getEntry method requires how many steps to access the...
LANGUAGE IS C++ 1- A link based getEntry method requires how many steps to access the nth item in the list? a)n b)n+1 c)n^2 d)n-1 2- When calling the insert or remove methods, what is an disadvantage for the link-based implementation of the ADT List? a)harder to understand b)must shift data c)searching for that position is slower d)takes more memory 3-What is the last step in the insertion process for a linked implementation of the ADT List? a)Connect the new...
Don't forget to have the main method A. Write a class that maintains the top ten...
Don't forget to have the main method A. Write a class that maintains the top ten scores for a game application, implementing the add and remove methods but using a singly linked list instead of an array. B. Perform the previous project, but use a doubly linked list. Moreover, your implementation of remove(i) should make the fewest number of pointer hops to get to the game entry at index i.
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class...
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class and a Node class (please name your classes exactly ​as I did here). Please follow the below specifications for the two classes. Node.cpp ● This must be a generic class. ● Should contain a generic member variable that holds the nodes value. ● Should contain a next and prev Node* as denoted here. ● All member variables should be private. ● Use public and...
Exercise 1 In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method...
Exercise 1 In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. Hint: You may need to traverse the list. Exercise 2 In this exercise, you will use the DoublyLinkedList implementation of the textbook....
C++ PROGRAMMING Submit the code for each problem separately. Important: Please place all appropriate coding comments...
C++ PROGRAMMING Submit the code for each problem separately. Important: Please place all appropriate coding comments inside of the code. Problem 1    Create a simple linked list program to create a class list containing class node {             void *info;              node *next; public:              node (void *v) {info = v; next = 0; }              void put_next (node *n) {next = n;}              node *get_next ( ) {return next;}              void *get_info (...
urgent !! ASAP PLEASE DATA STRUCTURES To class SinglyLinkedList, add method afterThird(E e) which receives an...
urgent !! ASAP PLEASE DATA STRUCTURES To class SinglyLinkedList, add method afterThird(E e) which receives an element e and insert that in a new node after the third node in the list (assume the first node in the list is number 1). You should ensure that the linked list has at least three nodes, otherwise throw an exception. You should consider all possible cases.
Write a function to accept a header of a sorted linked and a name which can...
Write a function to accept a header of a sorted linked and a name which can be used as a key. If the key is found then add the record to the linked list using pointer(s). Do not write complete program. Write just one function. I just need a function that can demonstrate the prompt above.
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...