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

1st Class:: public class Link { public int iData; public double dData; public Li

ID: 3725394 • Letter: 1

Question

1st Class::

public class Link {

public int iData;

public double dData;

public Link next;

public Link(int id, double dd)

{

iData=id;

dData=dd;

}

public void displayLink()

{

System.out.print("{" +iData +"," + dData +"}");

}

}

---------------------------------------------------------------

2nd Class::

public class LinkList {

private Link first;

////////////////////////////////////////////

public LinkList() //constructor

{

first= null;

}

////////////////////////////////////////////

public boolean isEmpty()

{

return(first==null);

}

///***Write here the insertFirst Method ***//

/////** write here the find Method ***/////////

///***Write here the deletFirst Method ***//

//////write here the delete Method any position**/////

///***Write here the display Method ***//

}

--------------------------------------------------

3rd Class(main)::

public class LinkListApp {

public static void main(String[] args)

{

LinkList theList = new LinkList(); // create list

theList.insertFirst(22,2.99);

theList.insertFirst(44, 4.99);

theList.insertFirst(66,6.99);

theList.displayList();

////////////////////////////

Link f= theList.find(44);

if(f != null)

System.out.println("Found link with key" + f.iData);

else

System.out.println("Can't find link");

////////////////////

Link d= theList.delete(66);

if(d != null)

System.out.println("Deleted link with key" + d.iData);

else

System.out.println("Can't delete link");

///////////////////////

theList.displayList();

}// end main

}//end class

-------------------------------

The question is ::

Update the code to  perform the followings:

Take the inputs from user to enter data of linklist

insertFirst Method ()

deleteFirts Method ()

find Method () // search key taken from user

delete any position Method() // delete spicific key taken from user

Explanation / Answer

Link Class:

public class Link {

public int iData;

public double dData;

public Link next;

public Link(int id, double dd)

{

iData=id;

dData=dd;

}

public void displayLink()

{

System.out.print("{" +iData +"," + dData +"}");

}

}


LinkList Class:


public class LinkList {

private Link first;
private Link prev;

////////////////////////////////////////////

public LinkList()      //constructor

{

first= null; //first is acting as a head node of a link list
prev=null;

}

////////////////////////////////////////////

public boolean isEmpty()

{

return(first==null);

}

///***Write here the insertFirst Method ***//
void insertFirst(int i,double d)
{
   if(first==null)
   {
       //System.out.println("ff");
       Link list=new Link(i,d);
       list.next=null;
       prev=list;
       first=prev;
   }
   else
   {
   Link list=new Link(i,d);
   list.next=null;
   prev.next=list;
   prev=list;
   }
  
}

/////** write here the find Method ***/////////
Link find(int i)
{
   Link list=first;
   while(list!=null)
   {
       if(list.iData==i)
       {
           return list;
       }
       list=list.next;
   }
   return null;
}

///***Write here the deletFirst Method ***//
Link deleteFirst()
{
   if (first == null)
        return null;
   Link temp = first;

        // If first needs to be removed
      
            first = temp.next;   // Change first
            return temp;
      
}

//////write here the delete Method any position**/////
Link delete(int index)
{
   if (first == null)
        return null;

    // Store first node
   Link temp = first;

    // Find previous node of the node to be deleted
    for (int i=0; temp!=null && i<index-1; i++)
        temp = temp.next;

    // If position is more than number of ndoes
    if (temp == null || temp.next == null)
        return null;

    // Node temp->next is the node to be deleted
    // Store pointer to the next of node to be deleted
    Link next = temp.next.next;
    Link list=temp.next;
    temp.next = next; // Unlink the deleted node from list
    return list;
  
}

///***Write here the display Method ***//
public void displayList()
{
    Link node = first;
    if (first == null)
        System.out.println("Empty string");
    else
    {
      
  
    while (node != null)
    {
        System.out.print(node.iData+" "+node.dData+" ");
        node = node.next;
    }
    System.out.println(" ");
    }
}

}

LinkListApp Class:

import java.util.Scanner;

public class LinkListApp {

public static void main(String[] args)

{
   Scanner sc=new Scanner(System.in);

LinkList theList = new LinkList(); // create list
while(true)
{
  

System.out.println("Enter 1 for inserting into link list");
System.out.println("Enter 2 for displaying the link list");
System.out.println("Enter 3 for deleteing from the first node");
System.out.println("Enter 4 for deleteing from any given index");
System.out.println("Enter 5 for finding an element");
System.out.println("Enter 6 to quit the program");

   int option;
   option=sc.nextInt();
   switch(option)
   {
   case 1:
   {
       System.out.println("Enter two values to insert in link list"); //Enter 2 number 1)Integer 2)Double

      System.out.println("Enter Integer");
       int i=sc.nextInt();

System.out.println("Enter Double");
       double dou=sc.nextDouble();
       theList.insertFirst(i,dou);
       break;
     
   }
   case 2:
   {
       System.out.println("Displaying the link list");
       theList.displayList();
       break;
   }
   case 3:
   {
       System.out.println("Deleting the first node from link list");
       Link d1= theList.deleteFirst();
       if(d1 != null)
       System.out.println("Deleted link with key " + d1.iData);
       else
       System.out.println("Can't delete link");
       break;
   }
   case 4:
   {
       System.out.println("Enter the index to delete from link list"); //
       int index=sc.nextInt();
       Link d= theList.delete(index);
       if(d != null)
       System.out.println("Deleted link with key " + d.iData);
       else
       System.out.println("Can't delete link");
       break;
     
   }
   case 5:
   {
       System.out.println("Enter the element to find in link list");
       int ele=sc.nextInt();
       Link f= theList.find(ele);
       if(f != null)
       System.out.println("Found link with key " + f.iData);
       else
       System.out.println("Can't find link");
       break;
     
   }
   case 6:
   {
       break;
   }
   default:
   {
       break;
   }
   
   }
    System.out.println("do you want to continue (y/n)"); //Enter y for continuing and n for ending the code
    String str=sc.next();
    if(str.equals("y")||str.equals("Y"))
    {
       continue;
    }
    else
    {
       break;
    }
}

}// end main

}//end class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote