Java: An Introduction to Problem Solving and Programming (7th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
7th Edition
ISBN: 9780133766264
Author: Walter Savitch
Publisher: PEARSON
bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 12, Problem 10E

Explanation of Solution

Modified code for “StringLinkedList” class:

The modified code for “StringLinkedList” class is given below:

Filename: “ListNode.java”

//Define "ListNode" class

public class ListNode

{

/* This class is same as the code in Listing 12.4 of textbook */

}

Filename: “StringLinkedList.java”

/* This class is same as the code in Listing 12.5 of textbook */

//Define "StringLinkedList" class

public class StringLinkedList

{

  private ListNode head;

  /* Create constructor for "StringLinkedList" class*/

  public StringLinkedList()

  {

  head = null;

  }

  /* Displays the data on the list.*/

  public void showList()

  {

  ListNode position = head;

  while (position != null)

  {

  System.out.println(position.getData());

  position = position.getLink();

  }

  }

  /* Returns the number of nodes on the list.*/

  public int length()

  {

  int count = 0;

  ListNode position = head;

  while (position != null)

  {

  count++;

  position = position.getLink();

  }

  return count;

  }

/* Adds a node containing the data addData at the start of the list.*/

  public void addANodeToStart(String addData)

  {

  head = new ListNode(addData, head);

  }

/* Method definition for add a node at the end of list */

public void addANodeToEnd(String addData)

{

  /* If head is null or empty, then */

  if(head == null)

  //Add node to the front

  addANodeToStart(addData);

  //Otherwise

  else

  {

  /* Assign "s" to "head" */

  ListNode s = head;

  //Compute the last node in the list

  while(s.getLink() != null)

  {

  s = s.getLink();

  }

  // Add in the node

  ListNode c = new ListNode(addData, null);

  s.setLink(c);

  }

  }

  /* Deletes the first node on the list.*/

  public void deleteHeadNode()

  {

  if (head != null)

  head = head.getLink();

  else

  {

System.out.println("Deleting from an empty list.");

System.exit(0);

}

}

/* Method definition for deletes the last node on the list */

public void deleteLastNode()

  {

  /* If head is null or empty, then */

  if (head == null)

  {

  //Display given message

System.out.println("Deleting from an empty list.");

System.exit(0);

}

  /* If "head.getLink()" is null, then */

  else if (head.getLink() == null)

  {

/* Delete first node in the list by calling method "deleteHeadNode" */

deleteHeadNode();

}

  //Otherwise

  else

  {

  //Set "s" to "head"

  ListNode s = head;

  //Look for the node before the final node

  while(s.getLink().getLink() != null)

  s = s.getLink();

  //Unlink the last node

  s.setLink(null);

  }

  }

  /* Sees whether target is on the list.*/

  public boolean onList(String target)

  {

  return find(target) != null;

  }

  // Returns a reference to the first node containing the

// target data. If target is not on the list, returns null.

private ListNode find(String target)

{

boolean found = false;

ListNode position = head;

while ((position != null) && !found)

{

  String dataAtPosition = position.getData();

  if (dataAtPosition.equals(target))

  found = true;

  else

  position = position.getLink();

  }

  return position;

  }

}

Modified Filename: “StringLinkedListDemo.java”

//Define "StringLinkedListDemo" class

public class Main

{

  //Define main function

  public static void main(String[] args)

  {

//Create object "list" from "StringLinkedList" class

  StringLinkedList list = new StringLinkedList();

/* Add three node to list by calling the method "addANodeToStart" */

list...

Blurred answer
Students have asked these similar questions
2. Perform resource allocation for the following project. Resource limits are 6 labors and 2 helpers. Legend: Activity Dur Resources G H 2 3 2L 1H 2L OH A 1 3L 1H + B D F J K 3 4 6 2 4 4L 2H 3L OH 4L 1H 2L 2H 4L 2H C E 2 2 I 1 2L 1H 3L 1H 5L 1H
Need Java method please. Thank you.
Need Java method please. Thank you.

Chapter 12 Solutions

Java: An Introduction to Problem Solving and Programming (7th Edition)

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Calculating total series of numbers: In order to calculate the total of the given numbers, it is necessary to h...

Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)

Fill in the blanks.

Modern Database Management

The Speed of Sound Program Plan: Import the required packages. Declare the class “Main”. Declare the “main ()” ...

Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)

By listing the types of measurements that form the basis of traditional plane surveying.

Elementary Surveying: An Introduction To Geomatics (15th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:9781337508841
Author:Carey
Publisher:Cengage
Text book image
CMPTR
Computer Science
ISBN:9781337681872
Author:PINARD
Publisher:Cengage
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7); Author: CS Dojo;https://www.youtube.com/watch?v=D6xkbGLQesk;License: Standard YouTube License, CC-BY