Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Scenario: We have a linked list containing some elements and we need to build a string of the form [3,6,4,2,4]. If the list is

JAVA

Scenario: We have a linked list containing some elements and we need to build a string of the form [3,6,4,2,4]. If the list is empty, it should output [].

Aim: Write code in Java for traversing the linked list.

Steps for Completion

  1. Write a toString() method in the LinkedList class as follows:
public String toString() { // ... }
  1. Use a while loop to traverse the linked list.

REQUIRED TASKS

Write the toString() method in the LinkedList class.

Use a while loop to traverse the linked list to add the nodes to print to the console. Wrap the list of nodes in opening and closing brackets separated by commas : [node1, node2, node3].

GIVEN CODE:

import java.util.Optional;

public class LinkedList { private LinkedListNode head;

public LinkedList() { head = null; }

public void addFront(V item) { this.head = new LinkedListNode<>(item, head); }

public void deleteFront() { Optional> firstNode = Optional.ofNullable(this.head); this.head = firstNode.flatMap(LinkedListNode::getNext).orElse(null); firstNode.ifPresent(n -> n.setNext(null)); }

public Optional> find(V item) { Optional> node = Optional.ofNullable(this.head); while (node.filter(n -> n.getValue() != item).isPresent()) { node = node.flatMap(LinkedListNode::getNext); } return node; }

public void addAfter(LinkedListNode aNode, V item) { aNode.setNext(new LinkedListNode<>(item, aNode.getNext().orElse(null))); }

// write your code here

public static void main(String[] args) { LinkedList list = new LinkedList(); list.addFront("Isabel"); list.addFront("Ruth"); list.addFront("Karl"); list.addFront("John"); System.out.println(list.find("Isabel")); System.out.println(list.find("Ruth")); System.out.println(list.find("Karl")); System.out.println(list.find("John")); System.out.println(list.find("James"));

list.deleteFront(); System.out.println(list.find("John")); list.addFront("Oliver"); System.out.println(list.find("Ruth")); list.addAfter(list.find("Ruth").get(), "Sam"); System.out.println(list.toString());

LinkedListNode x = new LinkedListNode<>(5, null); } }

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 Systems An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions

Question

Outline the process of short-selling.

Answered: 1 week ago

Question

1. Identify three approaches to culture.

Answered: 1 week ago

Question

3. Identify and describe nine cultural value orientations.

Answered: 1 week ago

Question

4. Describe how cultural values influence communication.

Answered: 1 week ago