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
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...
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...
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.       ...
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
Write a method name maxElement, which returns the largest value in an array that is passed...
Write a method name maxElement, which returns the largest value in an array that is passed as an argument. The method must use recursion to find the largest element. Demonstrate the method in a program. Write the JAVA CODE on the paper that is provided. (Put your name on every piece of paper) Use the following array to test the method. Int [] numbers = {2,12,1999,99,100,4,7,300} PROGRAMMING LANGUAGE- JAVA. Please answer the question ASAP. Thanks in advance!