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
Write Pseudocode to remove the node at position 1 in a doubly-linked list. Assume position follows...
Write Pseudocode to remove the node at position 1 in a doubly-linked list. Assume position follows classic indexing from 0 to item_count - 1, and there is a node at position 2.
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined...
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined on page 257. The function takes as input a pointer to the head of a linked list consisting of numbers. The function should remove the second item in the list. If the list had only one item, the function should delete that item. If the list was empty, then let the list remain empty. In all cases return the new head of the list...
a.   Write the separate function to delete the head node and tail node of a singly...
a.   Write the separate function to delete the head node and tail node of a singly linked list in java programming language? b.   Write the separate function to delete the head node and tail node of a doubly linked list in java programming language? c.   Write the best and worst case of the following sort algorithms. i.   Selection sort ii.   Bubble sort iii.   Insertion sort iv.   Merge sort
Write C language code for a function that takes a doubly linked list as a parameter...
Write C language code for a function that takes a doubly linked list as a parameter and deletes all the nodes in even positions from the first to the last after displaying the content of each node to the console.
Write a method that inserts a node in a sorted linked list. HINTS: Traverse the list...
Write a method that inserts a node in a sorted linked list. HINTS: Traverse the list and find a position for the element and insert it.
(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++
public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node...
public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node next; } public boolean isEmpty() { return (first == null); } public void push(String item) { // Insert a new node at the beginning of the list. Node oldFirst = first; first = new Node(); first.item = item; first.next = oldFirst; } public String pop() { // Remove the first node from the list and return item. String item = first.item; first = first.next;...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
Write a function that prints every other element in a linked list. Assume that it is...
Write a function that prints every other element in a linked list. Assume that it is a member function of a doubly linked list. This means you will need to use pointer operations.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT