Question

Create a method for a singlyLinked class public void remove( ) removes a randomly selected entry from the bag I don...

Create a method for a singlyLinked class

  • public void remove( )
    • removes a randomly selected entry from the bag

I don't know how to make it random I just know how to remove the first on the list this is what I have

/**
*
* @return Removes and returns the first node of the bag
*/
@Override
public E remove()
{
if(bagHead == null)
{
return null;
}
E element = bagHead.getElement();
bagHead = bagHead.getNext();
count--;
return element;
}

It needs to be random

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I don't know Java so I would not be able to help you with code, but I just want to help with the idea of solving your question and also I will provide the algorithm to do that so that it can be easily convertable to Java.

If you want to remove a random element then first generate random number in the range 0 to size of linked list. maintain two pointers previous and current, use loop to traverse to that randomly generated number node. now you can easily delete because you have previous pointer. set the current pointer's next to previous pointer's next and set current pointer's next to null and finally reduce the size because we have deleted the node. I have provided the visual representation of the removal process below.

prev current STEP-1 current prev STEP-2 current prev STEP-3 NULL

ALGORITHM:

####################################################

int random = some random number between (0 , length)

set current = head or start, set prev = head or start

for i from 0 to random:

prev = current

current = current -> next element

//comment: now your current is at random number position and we have to delete it

prev -> next element = current -> next element

current -> next element = NULL

//comment: now that element is deleted, let's reduce the length of linked list

length = length - 1

##############################################################################

MORE HELP: to generate random number you can use  java.util.Random class in Java. Now that everything is answered it should not difficult to code in Java. If you have liked my explanation please like. If you have any doubts please comment.

Add a comment
Know the answer?
Add Answer to:
Create a method for a singlyLinked class public void remove( ) removes a randomly selected entry from the bag I don...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a method for a singlyLinked class public void remove( ) removes a randomly selected entry...

    Create a method for a singlyLinked class public void remove( ) removes a randomly selected entry from the bag I don't know how to make it random I just know how to remove the first on the list this is what I have /** * * @return Removes and returns the first node of the bag */ @Override public E remove() { if(bagHead == null) { return null; } E element = bagHead.getElement(); bagHead = bagHead.getNext(); count--; return element; }...

  • instructions: Write a non-member method named bagScaler, which removes from a Bag all elements that occur...

    instructions: Write a non-member method named bagScaler, which removes from a Bag all elements that occur less than N times . The method returns the number of copies removed. For example, if B = {Bob, Joe, Bob, Ned, Bob, Ned, Kim}, then calling bagScaler(B, 3) removes Joe, Ned, and Kim making B = {Bob, Bob, Bob}, and returns 4 because it removed 2 copies of Ned, one of Joe, and one of Kim. Note: There's no iterator, but there is...

  • In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying...

    In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying nodes referenced by positions p and q to be exchanged for each other. Relink the existing nodes, do not create any new nodes. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- package lists; import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedPositionalList implements PositionalList { //---------------- nested Node class ---------------- /** * Node of a doubly linked list, which stores a reference to its * element and to both the previous and next...

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element...

    Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...

  • Write a method with signature "concatenate(LinkedQueue<E> Q2)" for the LinkedQueue<E> class that takes all elements of...

    Write a method with signature "concatenate(LinkedQueue<E> Q2)" for the LinkedQueue<E> class that takes all elements of Q2 and appends them to the end of the original queue. The operation should run in O(1) time and should result in Q2 being an empty queue. Write the necessary code to test the method.You may just modify the SinglyLinkedList class to add necessary support. LinkedQueue Class public class LinkedQueue<E> implements Queue<E> { /** The primary storage for elements of the queue */ private...

  • Q1: You can find a file that defines the CircularlyLinked List class similar to what we...

    Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the file and work on it. Your task is to: 1. Complete the missing methods in the file as discussed in the class. Search for the comment/" MISSING / in the file to see the methods that need to be completed. 2. Add the following methods to the class a. public Node getMin 1. Task: find the node with...

  • //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args )...

    //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args ) { Scanner in = new Scanner( System.in ); LinkedList teamList = new LinkedList(); final int TEAM_SIZE = Integer.valueOf(in.nextLine()); for (int i=0; i<TEAM_SIZE; i++) { String newTeamMember = in.nextLine(); teamList.append(newTeamMember); } while (in.hasNext()) { String removeMember = in.nextLine(); teamList.remove(removeMember); }    System.out.println("FINAL TEAM:"); System.out.println(teamList); in.close(); System.out.print("END OF OUTPUT"); } } =========================================================================================== //PoD import java.util.NoSuchElementException; /** * A listnked list is a sequence of nodes with...

  • public class LinkedNodesExamQuestion { /*    >>>>>>>>>>>>>>>>>> INSTRUCTIONS <<<<<<<<<<<<<<<< 1. Read through this entire class to...

    public class LinkedNodesExamQuestion { /*    >>>>>>>>>>>>>>>>>> INSTRUCTIONS <<<<<<<<<<<<<<<< 1. Read through this entire class to make sure you understand the context. A class named SinglyLinked is provided for you below. It provides an add method for adding new nodes on the front of the chain; this method should not be changed. It also provides a max method that you must complete. Note that the data type allowed in a Node is constrained to be a class that implements the...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that...

    Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in the list) and am having some difficulty. I can't seem to figure out why this swap method is throwing an error. Code: (SWAPPING METHOD AND TEST LINE BOLDED) package linkedlists; public class SinglyLinkedList<E> implements Cloneable { //---------------- nested Node class ---------------- /** * Node of a singly linked list, which stores a reference to its...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT