please write comments so i can understand the code and do it by Java, please Des
ID: 3720256 • Letter: P
Question
please write comments so i can understand the code and do it by Java, please
Design and implement an animated search, insertion, and deletion in a linked list. The program will have three buttons Search, Insert and Delete. The Search button will take one input value, the value to search for in the linked list, and will output in a popup window the value and index where the value is found. The Insert button will take two inputs, the value to insert in the linked list and the index where it should be inserted in the list. The Delete button will take one input value, the index, and then deletes the specified value from the linked list.
If invalid data is entered an appropriate error message should be displayed.
Explanation / Answer
class LinkedList
{
Node head; // head of list
/* Linked list Node. This inner class is made static so that
main() can access it */
static class Node {
int data;
Node next;
Node(int d) { data = d; next=null; } // Constructor
}
/* This function prints contents of linked list starting from head */
public int Search(int Data)
{
int count = 1;
Node n = head;
while (n != null)
{
if(n.data == Data);
return count;
count++;
n = n.next;
}
}
public void insertAfter(int new_data)
{
Node temp = head
while( temp != null)
temp = temp.next;
prev_node = temp;
/* 2. Allocate the Node &
3. Put in the data*/
Node new_node = new Node(new_data);
/* 4. Make next of new Node as next of prev_node */
new_node.next = prev_node.next;
/* 5. make next of prev_node as new_node */
prev_node.next = new_node;
}
/* Given a key, deletes the first occurrence of key in linked list */
public void deleteNode(int key)
{
// Store head node
Node temp = head, prev = null;
// If head node itself holds the key to be deleted
if (temp != null && temp.data == key)
{
head = temp.next; // Changed head
return;
}
// Search for the key to be deleted, keep track of the
// previous node as we need to change temp.next
while (temp != null && temp.data != key)
{
prev = temp;
temp = temp.next;
}
// If key was not present in linked list
if (temp == null) return;
// Unlink the node from linked list
prev.next = temp.next;
}
/* method to create a simple linked list with 3 nodes*/
public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList llist = new LinkedList();
while(1)
{
System.out.print("***** Link List: ");
System.out.print("1. Search ");
System.out.print("2. Insert");
System.out.print("3. Delete");
System.out.print("4. Exit ");
System.out.print("Input Your choice: ");
Scanner input = new Scanner(System.in);
int Val = input.nextInt();
if(Val == 1)
{
System.out.print("Input Node Value to search");
Scanner input = new Scanner(System.in);
int Val = input.nextInt();
int Loc = Search(Val);
System.out.print("Location",Loc);
System.out.print("Value",Val);
}
else if(Val == 2)
{
System.out.print("Input Node Value to Insert");
Scanner input = new Scanner(System.in);
int Val = input.nextInt();
insertAfter(Val);
}
else if(Val == 3)
{
System.out.print("Input Node Value to Delete");
Scanner input = new Scanner(System.in);
int Val = input.nextInt();
deleteNode(Val);
}
else if(Val == 4)
{
return;
}
else
System.out.print("Invalid input ");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.