The Java program listed below is one problem. Within the program posted below mo
ID: 661826 • Letter: T
Question
The Java program listed below is one problem. Within the program posted below modify the class LinkedList given in the lecture by adding the functions listed below for Exercise 2. In each case, the appropriate error message should be generated if an invalid condition occurs. For example, an error message should be generated when trying to replace the item at a given location in the list and the location is out of range. Create a main class to test your LinkedList class. Details for each method listed: String toString(): Modify the display method to override the toString method of the object class. This method returns a string representation of the linked list elements. int getLength(): Create this method to return the number of items in the list (accessor method). void clear(): Create this method to remove all of the items from the list. After this operation, the length of the list is zero. void addEnd(int item): Create this method to add the item to the end of the list. void replace(int location, int item): Create this method to replace the item in the list at the position specified by location. The item should be replaced with the item. int get(int location): Create a method that returns the element at location.
/******************************
* Week 2 lab - exercise 1: *
* a simple LinkedList class *
*******************************/
public class Main
{
public static void main(String args[])
{
LinkedList intList = new LinkedList();
System.out.print("List of numbers before list creation: ");
for (int i =0; i < 10; i++)
{
int info = (int)(Math.random()*10);
System.out.print(info + " ");
intList.add(info);
}
System.out.print(" List of numbers after list creation: ");
intList.display();
}
}
/******************************
* Week 2 lab - exercise 1: *
* a simple LinkedList class *
*******************************/
/**
* Class implementing a linked list.
*/
public class LinkedList
{
private Node first; //dummy header node
/**
* Initializes the list to empty creating a dummy header node.
*/
public LinkedList()
{
first = new Node();
}
/**
* Determines whether the list is empty
*
* @return true if the list is empty, false otherwise
*/
public boolean isEmpty()
{
return (first.getNext() == null);
}
/**
* Prints the list elements.
*/
public void display()
{
Node current = first.getNext();
while (current != null)
{
System.out.print(current.getInfo() + " ");
current = current.getNext();
}
System.out.println();
}
/**
* Adds the element x to the beginning of the list.
*
* @param x element to be added to the list
*/
public void add(int x)
{
Node p = new Node();
p.setInfo(x);
p.setNext(first.getNext());
first.setNext(p);
}
/**
* Deletes an item from the list. Only the first occurrence of the item in
* the list will be removed.
*
* @param x element to be removed.
*/
public void remove(int x)
{
Node old = first.getNext(),
p = first;
//Finding the reference to the node before the one to be deleted
boolean found = false;
while (old != null && !found)
{
if (old.getInfo() == x)
{
found = true;
} else
{
p = old;
old = p.getNext();
}
}
//if x is in the list, remove it.
if (found)
{
p.setNext(old.getNext());
}
}
}
/******************************
* Week 2 lab - exercise 1: *
* a simple LinkedList class *
*******************************/
/**
* Linked list node.
*/
public class Node
{
private int info; //element stored in this node
private Node next; //link to next node
/**
* Initializes this node setting info to 0 and next to null
*/
public Node()
{
info = 0;
next = null;
}
/**
* Sets the value for this node
*
* @param i the desired value for this node
*/
public void setInfo(int i)
{
info = i;
}
/**
* Sets the link to the next node
*
* @param l node reference
*/
public void setNext(Node l)
{
next = l;
}
/**
* Returns the value in this node
*
* @return the value in this node
*/
public int getInfo()
{
return info;
}
/**
* Returns the link to the next node
*
* @return link to the next node
*/
public Node getNext()
{
return next;
}
}
Explanation / Answer
here ae methods
@Override
public String toString() {
Iterator<E> iterator = iterator();
if (! iterator.hasNext())
return "[]";
StringBuilder stringbuilder = new StringBuilder();
stringbuilder.append('[');
for (;;) {
E c = iterator.next();
stringbuilder.append(c);
if (! iterator.hasNext())
return stringbuilder.append(']').toString();
stringbuilder.append(", ");
}
}
//deleting all elements at a once
public void clear()
{
this.clear();
}
//adding new node at end of our linked list
public void addEnd(int value) //declare private Node end;
{
Node newNode = new Node(value,null);
if(first == null)
{
first = nptr;
last = first;
}
else
{
last.setNext(newNode);
last = newNode;
}
}
public void replace(int location , int item) //to place certain element at particular position
{
Node nodepointer = new Node(item, null);
Node pointer = first;
location = location - 1 ;
for (int i = 1; i < Listsize; i++)
{
if (i == location)
{
Node temp = pointer.getLink() ;
pointer.setLink(nodepointer);
nodepointer.setLink(temp);
break;
}
pointer = pointer.getLink();
}
Listsize++;
}
public int get(int location){//thisi is to get element at position
return this.get(index);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.