!!! Your classes and functions belonging to them, should be named exactly as lis
ID: 3544165 • Letter: #
Question
!!! Your classes and functions belonging to them, should be named exactly as listed below !!!Write your own implementation of single-linked list, without using any class in the Java Collection hierarchy. Each node in the list should contain an int value and a reference to the next node, and at any time, the int values in the list should keep sorted (initially in increasing order). The list should support the following operations, with the bold word as method name:add a new value (and return true) if it is not already in the list, otherwise do nothing and return falsesearch for a given value and return a Boolean to indicate if it is in the listdelete a given value (and return true) if it is already in the list, otherwise do nothing and return falseremove the first node and return its value if the list is not empty, otherwise throw an exceptionreport the current size of the listmerge this list with another one of the same classreverse the order of the values in the listoverride the toString methodDesign a user interface for the program (GUI is optional), and use proper examples to test it.Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
class EmptyListException extends Exception
{
EmptyListException()
{
System.out.println("The list is empty");
}
}
class Link {
public int data1;
public Link nextLink;
//Link constructor
public Link(int d1) {
data1 = d1;
}
//Print Link data
public String toString() {
System.out.println( "{" + data1 + "} ");
return "{" + data1 + "} ";
}
}
class LinkList {
private Link first;
//LinkList constructor
public LinkList() {
first = null;
}
//Returns true if list is empty
public boolean isEmpty() {
return first == null;
}
public boolean search(int d)
{
Link currentLink = first;
while(currentLink != null)
{
if(currentLink.data1==d)
return true;
currentLink = currentLink.nextLink;
}
return false;
}
//Inserts a new Link at the first of the list
public boolean add(int d1) {
boolean flag = search(d1);
if(!flag)
{
Link link = new Link(d1);
link.nextLink = first;
first = link;
return true;
}
return false;
}
//Deletes the link at the first of the list
public Link delete(Link L,int d) {
boolean flag = search(d);
if(flag)
{
Link prev;
if(L.data1==d)
{
prev = L.nextLink;
first = prev;
return first;
}
L.nextLink = delete(L.nextLink,d);
return first;
}
return first;
}
public int remove() {
Link temp = first;
if(temp!=null){
first = first.nextLink;
return temp.data1;}
else
{try{
throw new EmptyListException();}catch(Exception e){}
return -999;
}
}
//Prints list data
public void printList() {
Link currentLink = first;
System.out.print("List: ");
while(currentLink != null) {
currentLink.toString();
currentLink = currentLink.nextLink;
}
System.out.println("");
}
public Link merge(Link other)
{
Link currentLink = first;
Link prev =null;
if(currentLink==null)
{
first =other;
return first;
}
while(currentLink != null) {
prev = currentLink;
currentLink = currentLink.nextLink;
}
prev.nextLink = other;
return first;
}
public void reverse()
{
ArrayList<Integer> I = new ArrayList<Integer>();
Link currentLink = first;
while(currentLink != null) {
I.add(currentLink.data1);
currentLink = currentLink.nextLink;
}
ListIterator L = I.listIterator(I.size());
first = null;
while(L.hasPrevious())
{
Integer x= (Integer)L.previous();
add(x.intValue());
}
}
}
class LinkListTest {
public static void main(String[] args) {
LinkList list = new LinkList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.printList();
while(!list.isEmpty()) {
System.out.print("deleted: "+list.remove());
System.out.println("");
}
list.printList();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.