Question

In: Computer Science

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) {
this.name = n;
}
public void add(String n) {
MyLinkedList i = this;
while (i.next != null) {
i = i.next;
}
i.next = new MyLinkedList(n);
}
public int length() {
int ret_val = 1;
MyLinkedList i = this;
while (i.next != null) {
i = i.next;
ret_val++;
}
return ret_val;
}
public void print() {
// ????
}
public MyLinkedList itemAt(int index) {
int counter = 1;
MyLinkedList i = this;
while (i.next != null && counter < index) {
i = i.next;
counter++;
}
return i;
}
public static void main(String[] args) {
MyLinkedList student_list = new MyLinkedList("12");
student_list.add("7");
student_list.add("20");
student_list.add("40");
student_list.add("9");
System.out.println(student_list.length());
System.out.println(student_list.itemAt(3));
System.out.println();
}
}

Solutions

Expert Solution

Please find the answer for the above given question..

ANSWER :

OUTPUT :

CODE :   I have highlighted the code below which i have added.

class MyLinkedList {

private MyLinkedList next;

private String name;

public MyLinkedList(String n) {

this.name = n;

}

public void add(String n) {

MyLinkedList i = this;

while (i.next != null) {

i = i.next;

}

i.next = new MyLinkedList(n);

}

public int length() {

int ret_val = 1;

MyLinkedList i = this;

while (i.next != null) {

i = i.next;

ret_val++;

}

return ret_val;

}

public MyLinkedList itemAt(int index) {

int counter = 1;

MyLinkedList i = this;

while (i.next != null && counter < index) {

i = i.next;

counter++;

}

return i;

}

public void print() {

int counter =0;

MyLinkedList i = this;

System.out.println("Index\tElement");

while(i.next != null){

System.out.println(Integer.toString(counter)+"\t\t"+i.name);

i= i.next;

counter++;

}

System.out.println(Integer.toString(counter)+"\t\t"+i.name);

}

public static void main(String[] args) {

MyLinkedList student_list = new MyLinkedList("12");

student_list.add("7");

student_list.add("20");

student_list.add("40");

student_list.add("9");

System.out.println(student_list.length());

System.out.println(student_list.itemAt(3).name);

System.out.println();

student_list.print();

}

}

Thanks..


Related Solutions

java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
Add code (see below for details) to the methods "set" and "get" in the following class,...
Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index] =...
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 =...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following methods to the LinkedStacked class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the LinkedStacked, not by calling the previously defined public methods of the class. - String toString()—creates and returns a string that correctly represents the current stack....
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
In the following Java class, what would the following createFromString(String string) and saveToString() methods return? /**...
In the following Java class, what would the following createFromString(String string) and saveToString() methods return? /** * This class represents a DVD player to be rented. * * @author Franklin University * @version 2.0 */ public class DVDPlayer extends AbstractItem { /** * Constructor for objects of class DVDPlayer. */ public DVDPlayer() { // No code needed } /** * Creates a DVDPlayer from a string in the format * id:desc:weeklyRate:rented * @param string The string * @return the new...
PYTHON - please finish the methods below that are apart of a linkedlist class #return the...
PYTHON - please finish the methods below that are apart of a linkedlist class #return the data value at index(position) in the list. values remain unchanged def __getpos__(self, position): #do not change, checks for valid index if self.size == 0: raise IndexError elif position is None: return self.pop(self.size - 1) elif type(position) != int: raise TypeError elif position < 0 or position >= self.size: raise IndexError #replace the data value at requested position(index). return nothing def __setpos__(self,position,value): #do not change,...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
A. Add code (see below for details) to the methods "set" and "get" in the following...
A. Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT