Question

In: Computer Science

For a LinkedList in Java how do I - Add a method named replace() that takes...

For a LinkedList in Java how do I

- Add a method named replace() that takes in two parameters (the data object to replaced, followed by the one to be inserted) that will remove the first occurrence of the item to be replaced and add the second parameter to the list. The method returns a boolean - true if the item to be replaced was found, false if not

- Add an instance method named showList() (no parameters or return value) that will extract all of the values from the list and display them in order, one per line, followed by another line displaying how many values are currently in the list. If the list is empty, display the message “The list is currently empty” instead

- Overload showList() with an integer parameter which represents how many items to display on each line. The method needs to newline after the last item no matter how many items were displayed (i.e. don’t leave the cursor hanging.)

Solutions

Expert Solution

//Linked list class

class LinkedList

{

//Node class

static class Node

{

int data;

Node next;

Node(int d)

{

data = d;

next = null;

}

}

Node head; //Head of list

//Start with the empty list

static LinkedList llist = new LinkedList();

//Inserts a new node at the front of the list

public void push(int new_data)

{

//Allocate new node and putting data

Node new_node = new Node(new_data);

//Make next of new node as head

new_node.next = head;

//Move the head to point to new Node

head = new_node;

}

//Checks whether the value x is present in linked list and

// replaces it with y

public boolean replace( int x, int y)

{

Node current = llist.head; //Initialize current

while (current != null)

{

if (current.data == x){

current.data = y ;

return true; //data found

}

current = current.next;

}

return false; //data not found

}

public void showList()

{

Node n = llist.head;

while (n != null) {

System.out.println(n.data);

n = n.next;

}

}

public void showList(int N)

{

int i = 1;

Node n = llist.head;

while (n != null) {

System.out.print(n.data + " ");

n = n.next;

if (i == N){

i = 0;

System.out.println();

}

++i;

}

System.out.println("\n");

}

//Driver function to test the above functions

public static void main(String args[])

{  

/*Use push() to construct below list

14->21->11->30->10 */

llist.push(10);

llist.push(10);

llist.push(11);

llist.push(21);

llist.push(14);

llist.push(19);

llist.push(39);

llist.push(18);

llist.push(25);

llist.push(1);

llist.showList();

if (llist.replace( 10, 31))

System.out.println("\nYes\n");

else

System.out.println("\nNo\n");

llist.showList();

llist.showList(3);

}

}

Screen shot :


Related Solutions

Write a Java program/method that takes a LinkedList and returns a new LinkedList with the integer...
Write a Java program/method that takes a LinkedList and returns a new LinkedList with the integer values squared and reversed. Example: if the LinkedList has (9, 5,4,6), then the returned list will have (36, 16,25,81). What is the time-complexity of your code? You must use listIterator for full credit. public LinkedList getReverseSquaredList (LinkedList list) { }
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with the higher value data. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head; while (tempPtr != null) { tempPtr = tempPtr.nextNode; result =...
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps...
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps the elements at those two positions. The method should not traverse the list twice to find the elements, and it should not create or destroy any nodes.
In java write a method that will take an array and change it into a linkedlist...
In java write a method that will take an array and change it into a linkedlist and then display it in the main method
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
for java!! Make a public class Value that provides a static method named test. test takes...
for java!! Make a public class Value that provides a static method named test. test takes a single int argument and returns an anonymous object that implements the following Absolute interface: public interface Absolute { int same(); int opposite(); } -------------------------------------------------------------------------- The returned object should implement same so that it returns the passed int as it is, and opposite so that it returns the passed int as the int * -1. For example Absolute first = Value.test(-90) Absolute second =...
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
How do I add a method to make sure that the user inputs in uppercase only...
How do I add a method to make sure that the user inputs in uppercase only and anything entered in lower case throws an error as well as to make sure when they are halving the questioned number they don't enter any decimals? import java.util.*; public class TestCode { public static void main(String[] args) { String choice = "YES"; Random random = new Random(); Scanner scanner = new Scanner(System.in); ArrayList data = new ArrayList(); int count = 0,correct=0; while (!choice.equals("NO"))...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters it by the requested price range that a property has. So if someone wants a property between the value of 10(min) and 20(max) it would show all members of the array that meet those conditions.. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT