Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Q1) The deleteElement(int deleteData) member function in the code for the Singly Linked Listbased implementation of a List ADT deletes the first occurrence of deleteData

Q1) The deleteElement(int deleteData) member function in the code for the Singly Linked Listbased implementation of a List ADT deletes the first occurrence of deleteData in the List. Modify this member function in such a way that all occurrences of deleteData in the List are deleted with a single call to the deleteElement function from main. After you modify the deleteElement function, run the main function (as given in the startup code for this question) by creating a List of at least 10 elements with certain elements repeating. Now ask for a value (deleteData) to delete from the user and call the deleteElement(deleteData) function to delete all occurrences of deleteData in the List. Capture the output of your program displaying the contents of the List before and after the call to the deleteElement function. A sample of the expected screenshot of the program is shown below. As noticed in the screenshot, all occurrences of deleteData '15' in the List are removed after a call to the deleteElement function. image text in transcribed

The given code is:

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

int value;

System.out.print("Enter element # "+i+" : ");

value = input.nextInt();

integerList.insertAtIndex(i, value);

}

System.out.print("Contents of the List (before delete): ");

integerList.IterativePrint();

// 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();

}

}

Enter the number of elements you want to insert: 10 Enter element # 12 Enter element # 1 14 Enter element # 2 14 Enter element # 3 15 Enter element # 4 15 Enter element # 5 : 18 Enter element # 6 : 17 Enter element # ? 15 Enter element # 8 19 Enter element # 9 14 Contents of the List: before de lete 12 14 14 15 15 18 17 15 19 14 Enter the data to delete: 15 Contents of the List after de lete); 12 14 14 18 17 19 14

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago