Question
import java.util.*; // implementing the dynamic List ADT using Linked List class Node{ private int data; private Node nextNodePtr; public Node(){} public void setData(int d){
import java.util.*;
// implementing the dynamic List ADT using Linked List
class Node{ private int data; private Node nextNodePtr; public Node(){} public void setData(int d){ data = d; } public int getData(){ return data; } public void setNextNodePtr(Node nodePtr){ nextNodePtr = nodePtr; } public Node getNextNodePtr(){ return nextNodePtr; } }
class List{
private Node headPtr; public List(){ headPtr = new Node(); headPtr.setNextNodePtr(null); } public Node getHeadPtr(){ return headPtr; } public boolean isEmpty(){ if (headPtr.getNextNodePtr() == null) return true; return false; } public void insert(int data){ Node currentNodePtr = headPtr.getNextNodePtr(); Node prevNodePtr = headPtr; while (currentNodePtr != null){ prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr.getNextNodePtr(); } Node newNodePtr = new Node(); newNodePtr.setData(data); newNodePtr.setNextNodePtr(null); prevNodePtr.setNextNodePtr(newNodePtr); } public void insertAtIndex(int insertIndex, int data){ Node currentNodePtr = headPtr.getNextNodePtr(); Node prevNodePtr = headPtr; int index = 0; while (currentNodePtr != null){ if (index == insertIndex) break; prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr.getNextNodePtr(); index++; } Node newNodePtr = new Node(); newNodePtr.setData(data); newNodePtr.setNextNodePtr(currentNodePtr); prevNodePtr.setNextNodePtr(newNodePtr); } public int read(int readIndex){ Node currentNodePtr = headPtr.getNextNodePtr(); Node prevNodePtr = headPtr; int index = 0; while (currentNodePtr != null){ if (index == readIndex) return currentNodePtr.getData(); prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr.getNextNodePtr(); index++; } return -1; // an invalid value indicating // index is out of range } public void modifyElement(int modifyIndex, int data){ Node currentNodePtr = headPtr.getNextNodePtr(); Node prevNodePtr = headPtr; int index = 0; while (currentNodePtr != null){ if (index == modifyIndex){ currentNodePtr.setData(data); return; } prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr.getNextNodePtr(); index++; } } public void deleteElementAtIndex(int deleteIndex){ Node currentNodePtr = headPtr.getNextNodePtr(); Node prevNodePtr = headPtr; Node nextNodePtr = headPtr; int index = 0; while (currentNodePtr != null){ if (index == deleteIndex){ nextNodePtr = currentNodePtr.getNextNodePtr(); break; } prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr.getNextNodePtr(); index++; } prevNodePtr.setNextNodePtr(nextNodePtr); }
public void deleteElement(int deleteData){ // The code given here deletes only the first occurrence of 'deleteData' // Modify the deleteElement member function to delete all occurrences of 'deleteData' Node currentNodePtr = headPtr.getNextNodePtr(); Node prevNodePtr = headPtr; Node nextNodePtr = headPtr; while (currentNodePtr != null){ if (currentNodePtr.getData() == deleteData){ nextNodePtr = currentNodePtr.getNextNodePtr(); prevNodePtr.setNextNodePtr(nextNodePtr); break; } else{
prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr.getNextNodePtr(); } } } public void IterativePrint(){ Node currentNodePtr = headPtr.getNextNodePtr(); while (currentNodePtr != null){ System.out.print(currentNodePtr.getData()+" "); currentNodePtr = currentNodePtr.getNextNodePtr(); } System.out.println(); } }
class SinglyLinkedList_ContinuousDelete{ public static void main(String[] args){
Scanner input = new Scanner(System.in); int listSize; System.out.print("Enter the number of elements you want to insert: "); listSize = input.nextInt(); List integerList = new List(); // Create an empty list for (int i = 0; i
// to delete all occurrences of a particular data int deleteData; System.out.print("Enter the data to delete: "); deleteData = input.nextInt(); integerList.deleteElement(deleteData); // modify the deleteElement member function to delete // all occurrences of 'deleteData' System.out.print("Contents of the List (after delete): "); integerList.IterativePrint();
}
} ***PLEASE INCLUDE SCREENSHOT OF EXECUTION
h1nh dtgi it setn g t. w IS ltte att ne ceL eeelsrac ta 2 filge et enddf 2 eetr)sn nrht i 14 quae asu taes ed ll fon u one e5 12445587594 e d ta ...-sd s 01234567890L f Efhne in at y te eep inni d at i e n r el t( Teil-i n t a h of neeeeeeeeeefdf lep In mr le n el edon eeeeeeeeeeetet tttttttttttntn Qb to fu ce de of A oc EEEEEEEEEEECEC
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started