Create an application that can use Singly Linked List with iterator with java Th
ID: 3738371 • Letter: C
Question
Create an application that can use Singly Linked List with iterator with java
The Candidate data will keep the save information
For Singly Linked List with Iterator:
-INSERT: Allow users to insert Candidate or Candidate with title until users want to stop. For each Candidate or Candidate with experience: display message to ask users to enter all the information that needs to create an object then insert it to the Singly Linked List. When users finish inserting all nodes, display all the Singly Linked list to verify all are inserted
-FETCH: -Fetch and display two first Candidate or Candidate with experience on the list
-UPDATE: -Add “A” in front of each ID of all Candidate or Candidate with experience in the Linked List with iterator The display all the Singly Linked List with iterator to see the change
-DELETE -Delete the node at third location on the Singly Linked List with iterator -Display the Singly Linked List with iterator to see the change
Explanation / Answer
As per your requirement the below one is solution please follow it
package com.samples;
import java.util.Scanner;
class Test
{
String Title;
Test ref=null;
public static Test refval=null;
public static void add(String val)
{
if(refval==null)
{
Test Temp=new Test();
Temp.Title=val;
refval=Temp;
Temp.ref=null;
}
else
{
Test tem=refval;
while(tem.ref!=null)
{
tem=tem.ref;
}
Test obj=new Test();
obj.Title=val;
tem.ref=obj;
obj.ref=null;
}
}
public static void Show()
{
Test tem=refval;
if(tem==null)
{
System.out.println("No Data to view");
}
else
while(tem!=null)
{
System.out.println(tem.Title);
tem=tem.ref;
}
}
public static void addFirst(String val)
{
if(refval==null)
{
Test Temp=new Test();
Temp.Title=val;
refval=Temp;
Temp.ref=null;
}
else
{
Test temp=new Test();
temp.Title=val;
temp.ref=refval;
refval=temp;
}
}
public static void deleteFirstNode()
{
if(refval==null)
{
System.out.println("No elemet to delete. add an element then delete");
}
else
{
int heapSize = (int) Runtime.getRuntime().totalMemory();
System.out.println("Memory before deleting "+ heapSize);
refval=refval.ref;
System.out.println("memory after deleting"+Runtime.getRuntime());
}
}
}
public class LinkedList
{
public static void main(String arvg[])
{
Scanner sc=new Scanner(System.in);
String title = null;
System.out.println(" Read Candidate Data:press stop to stop the input");
do
{
title=sc.next();
Test.add(title);
}while(!(title.equals("stop")));
int option;
while(true)
{
System.out.println("Enter 1 to add data to link List ");
System.out.println("Enter 2 to view Data ");
System.out.println("enter 3 to Update title ");
System.out.println("enter 4 to delete firstval");
System.out.println("enter 5 to exit");
option=sc.nextInt();
switch(option)
{
case 1:
System.out.println("Enter val to insert into the list: ");
title=sc.next();
Test.add(title);
break;
case 2:
Test.Show();
break;
case 3:
System.out.println("Enter value ");
title=sc.next();
Test.addFirst(title);
case 4:
Test.deleteFirstNode();
break;
case 5:
System.exit(0);
default:
System.out.println("Wrong User choice going to end");
System.exit(0);
}
}
}
}
Sample Output:-
-------------------------
Read Candidate Data:press stop to stop the input
venkanna
koothada
sindhu
manikam
stop
Enter 1 to add data to link List
Enter 2 to view Data
enter 3 to Update title
enter 4 to delete firstval
enter 5 to exit
2
venkanna
koothada
sindhu
manikam
stop
Enter 1 to add data to link List
Enter 2 to view Data
enter 3 to Update title
enter 4 to delete firstval
enter 5 to exit
4
Memory before deleting 64487424
memory after deletingjava.lang.Runtime@3d4eac69
Enter 1 to add data to link List
Enter 2 to view Data
enter 3 to Update title
enter 4 to delete firstval
enter 5 to exit
2
koothada
sindhu
manikam
stop
Enter 1 to add data to link List
Enter 2 to view Data
enter 3 to Update title
enter 4 to delete firstval
enter 5 to exit
3
Enter value
chennaisuperking
Memory before deleting 64487424
memory after deletingjava.lang.Runtime@3d4eac69
Enter 1 to add data to link List
Enter 2 to view Data
enter 3 to Update title
enter 4 to delete firstval
enter 5 to exit
2
koothada
sindhu
manikam
stop
Enter 1 to add data to link List
Enter 2 to view Data
enter 3 to Update title
enter 4 to delete firstval
enter 5 to exit
5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.