Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the following definition for the linked list, implementthe member function

ID: 3611592 • Letter: U

Question

Using the following definition for the linked list, implementthe member functions for the linked list class ofintegers. Use the given main function to test out yourimplementation.

This is given below:

public class LinkedList

{

     private class node

     {

          intdata;

          nodenext;

     }

     private node first;//pointer to thefirst node in the list

   public LinkedList()

     { //create an empty linked list }

    

   public boolean empty()

{ // return true if the list is empty, otherwise return false}

   public void InsertInFront(int x)

{//insert a value x in front of the list}

public void Delete(ElementType x)

{//if value x is in the list, remove x }

   public void Display()

{// Display the data values in the linked list }

public int count()

{//Count and return the number of values in the linked list}

}

Use the following main function to test your implementation:

public static void main(String[] arg)

{

LinkedList X = new LinkedList();

for(int a = 1; a<10; a++)

     if((a % 2) == 0)

        X.InsertInFront(a);

X.Display();

X.Delete (2);

System.out.println (”list after deleting2:”);

X.Display();

X.Delete(6);

System.out.println (”list after deleting6:”);

X.Display()

if(!X.empty())

     System.out.println(”List is notempty.”);

System.out.println(“Number of values: ”+X.count());

}    

Explanation / Answer

{

private class node

{

int data;

node next;

}

private node first;//pointer tothe first node in the list

public LinkedList()

{ //createan empty linked list

first=null;

}

public boolean empty()

{ // returntrue if the list is empty, otherwise return false

if(first ==null)

return true;

else

return false;

}

public void InsertInFront(intx)

{//insert avalue x in front of the list

node temp;

if(first ==null)

{

first = new node();

first.data =x;

first.next = null;

}

else

{

temp = new node();

temp.data = x;

temp.next = first;

first = temp;

}

}

public void Delete(intx)

{//if valuex is in the list, remove x

node temp,p;

p=temp =first;

if(first.data == x)

{

first =first.next;

}else

while(temp!= null)

{

if(temp.data ==x)

{

p.next = temp.next;

break;

}

p=temp;

temp = temp.next;

}

}

public void Display()

{// Displaythe data values in the linked list

node temp;

temp =first;

while(temp!= null)

{

System.out.println(temp.data);

temp = temp.next;

}

}

public int count()

{//Countand return the number of values in the linked list

node temp;

temp =first;

int count=0;

while(temp!= null)

{

count++;

temp = temp.next;

}

return count;

}

public static void main(String[] arg)

{

LinkedList X = new LinkedList();

for(int a = 1; a<10; a++)

if((a % 2)== 0)

X.InsertInFront(a);

X.Display();

X.Delete (2);

System.out.println ("list after deleting2:");

X.Display();

X.Delete(6);

System.out.println ("list after deleting6:");

X.Display();

if(!X.empty())

System.out.println("List is notempty.");

System.out.println("Number of values: "+X.count());

}

}