Q2. (13 points) Design LinkedList Consider the following main_class) public stat
ID: 3703758 • Letter: Q
Question
Q2. (13 points) Design LinkedList Consider the following main_class) public static void main(String) arga) LS theList new LS ) theList.insertFirst ll, 1.99, 2.99) thelist.insertFirat (22, 2,99, 3.99) thelist.insertFirat (33, 3.99, 4.33) theList displayList while Ithelist.isEmpty) Link atinktheList.deleteFirst) System.out.print ("the following node was Deleted:") alink.displaytink0: System.out print(" cheList.displayList) 1. Code the node (4.5p) Va. Parameters (1.5p) b. Constructor (1.5p) c. Display it on a format as below (Fig.1) (1.5p) 2. Code the LinkedList ) (7.5) Va. Define the head (1.5p) b. Write a method to Check if the list is empty (1.5) Vc. InsertFirst method (1.5) d. DeleteFirst method (1.5) e. DisplayList method (1.5) f. Create a counter to follow how many node in the list ## Create a method to find and print the node with the highest integer (2p) our list:133:3.99: 4.33 122: 2.99: 3.99H11: 1.99: 2.99 The number of node(s) in the list:3 Fig.1Explanation / Answer
class Link
{
double x;
double y;
double z;
// point to next node
Link next;
// constructor
Link()
{
this.x = 0.0;
this.y = 0.0;
this.z = 0.0;
this.next = null;
}
// constructor
Link(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
this.next = null;
}
public void setNext(Link next)
{
this.next = next;
}
public Link getNext()
{
return this.next;
}
public void displayLink()
{
System.out.print("{" + String.valueOf(x) + ": " + String.valueOf(y) + ": " + String.valueOf(z) + "}")
}
}
// In the program it is named as LS
class LS
{
// point to the head of the list
Link head;
// constructor
LS()
{
// create an empty list
head = null;
}
// return true if list is empty
public boolean isEmpty()
{
return this.head == null;
}
public void insertFirst(double x, double y, double z)
{
// create a new node
Link new_node = new Link(x, y, z);
// if list is empty
if( isEmpty() )
// set new node as the new head
this.head = new_node;
else
{
// make new node point to head
new_node->setNext( this.head );
// set new node as the new head
this.head = new_node;
}
}
public void deleteFirst(double x, double y, double z)
{
// if list is already empty
if( isEmpty() )
return;
// move head to second node
this.head = this.head.getNext();
}
public void dislayList()
{
// point trav to the head of the list
Link trav = this.head;
// loop untill list ends
while( trav != null )
{
// display the node
trav.displayLink()
// move to next node
trav = trav.getNext();
}
}
// return the number of nodes in the list
public int count()
{
// point trav to the head of the list
Link trav = this.head;
int c = 0;
// loop untill list ends
while( trav != null )
{
c++;
// move to next node
trav = trav.getNext();
}
return c;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.