Question: CSc 2720 Data Structures: Lab 5 How to Submit: Please submit your answers in icollege once you have completed. Failure to submit will result in
CSc 2720 Data Structures: Lab 5 How to Submit: Please submit your answers in icollege once you have completed. Failure to submit will result in a zero for this lab. Refresher: We have seen in class that deletion and insertion operations in a linked list necessitate a curr and prev pointers. To delete a node which curr references: prev.next curr.next; head Problem 1: Write a function deleteAt Index that takes a linked list head and an integer index as an argument, looks for the node in the linked list at index index and delete the node from the linked list (disconnect it from the list). Assumptions: Users always gives a valid index. User will never delete the head/first node in the list. i.e: the user will never enter index 0) The linked list will have at least two elements. Example: Given linked list (12,3], which looks like the following 2 Input: index 1 Output: List After Deletion: 1 3 public class Node ( int item Node next; // Node Constructor Node(int d) ( item d; next-null; Tester Class public class Tester( public static void main(String[] args) Node head = new Node(1); Node second new Node(2); Node third new Node(3); head.next second; second.next third; / The current linked list is as follows: head second third System.out.println("List Before Deletion ) printlinkedList (head) I Should be 1 2 3 // User wants to delete at index 2 deleteAtIndex (2, head); System.out.printin("List After Deletion at index 2"); printLinkedList (head); /I Should be 1 2 /I User wants to delete at index 1 deleteAtIndex (1, head); System.out.println("List After Deletion at index 1") printLinkedList (head); II Should be 2 // To pass the linked list to a function, you only need to pass the head public static void deleteAtIndex (int value, Node head) ( Node prev, curr int counter = IINSERT CODE HERE // increment count as you traverse the list // Node traversal and printing public static void printlinkedList (Node head)( for(Node cur head ; cur!.null;cur.cur.next)( # System.out.print(cur.item+) System.out.printin()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
