I have a preference for the code to be in Java if possible. Q1 - 25 pts) The del
ID: 3871097 • Letter: I
Question
I have a preference for the code to be in Java if possible.
Explanation / Answer
Q1:-
// A complete working Java program to demonstrate deletion in singly
// linked list
import java.util.Scanner;
class LinkedList
{
Node head; // head of list
/* Linked list Node*/
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
/* Given a key, deletes the all occurrence of key in linked list */
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
// recursively call delete again to delete all other occurance
deleteNode(key);
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;
// recursively call delete again to delete all other occurance
deleteNode(key);
}
/* Inserts a new Node at front of the list. */
public void push(int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
/* This function prints contents of linked list starting from
the given node */
public void printList()
{
Node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data+" ");
tnode = tnode.next;
}
System.out.println("");
}
/* Drier program to test above functions. Ideally this function
should be in a separate user class. It is kept here to keep
code compact */
public static void main(String[] args)
{
LinkedList llist = new LinkedList();
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of elements you want to insert : ");
int N;
N = sc.nextInt();
System.out.println(String.valueOf(N));
for(int i = 0 ; i < N; i++){
int M;
System.out.print("Enter element at " + i + " : ");
M = sc.nextInt();
llist.push(M);
System.out.println(String.valueOf(M));
}
System.out.println(" Content of list ( before delete )");
llist.printList();
System.out.print("Enter the number you want to delete : ");
int D;
D = sc.nextInt();
System.out.println(String.valueOf(D));
llist.deleteNode(D); // Delete node at position 4
System.out.println(" Content of list ( after delete ) :");
llist.printList();
}
}
INPUT:-
10
12
14
14
15
15
18
17
15
19
14
15
Output:-
Enter the number of elements you want to insert : 10
Enter element at 0 : 12
Enter element at 1 : 14
Enter element at 2 : 14
Enter element at 3 : 15
Enter element at 4 : 15
Enter element at 5 : 18
Enter element at 6 : 17
Enter element at 7 : 15
Enter element at 8 : 19
Enter element at 9 : 14
Content of list ( before delete )
14 19 15 17 18 15 15 14 14 12
Enter the number you want to delete : 15
Content of list ( after delete ) :
14 19 17 18 14 14 12
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.