Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am looking for Mk_Shiva32 for help with this problem, as this user was the only one to follow the correct formar in t previous

I am looking for Mk_Shiva32 for help with this problem, as this user was the only one to follow the correct formar in t previous question I had asked.

further below is the actual problem.

*For the entirety of this question you are not allowed to use any data structure * other than a custom built linked list. This should consist ONLY of a node * class that will be given. You should not be creating a secondary class to handle the data * structure. * * Failure to do so will result in no credit.

the node class

public class Node { private E item; private Node next;

public Node(E item, Node next) { //0, null this.item = item; this.next = next; }

public E getItem() { return item; }

public void setItem(E item) { this.item = item; }

public Node getNext() { return next; }

public void setNext(Node next) { this.next = next; }

}

the problem: THIS IS WHERE I AM STUCK, PLEASE READ THE PROBLEM.

/** * Problem: Using the Linked List from problem A (PROBLEM A IS BELOW THIS CODE) , print the contents of the Linked List in ascending order, UNLESS the list contains the number 5. If it * contains the number 5, print the list in descending order. DO NOT MAKE ANY OTHER METHODS OR CLASSES AND DO NOT CHANGE ANY OF THE METHOD HEADERS OR CLASSES

public Node sortIntegers(Node linkedList) { return null; // Return the start node of your linked list as well as printing it. The "null" can be changed to //"start" }

PROBLEM A:

public Node addInts() { Scanner userInput = new Scanner(System.in);

Node start, tail = new Node<>(0, null); start = null; System.out.println("Enter numbers and enter STOP to finish"); while(true) {

if(userInput.hasNextInt()) { int numInput = userInput.nextInt(); if (start == null) { start = new Node<>(numInput, null); tail = start; } else { Node next = new Node<>(numInput, null); tail.setNext(next); tail = tail.getNext(); } } else if(userInput.hasNext("STOP") || userInput.hasNext("stop")) { break; } else { userInput.next(); System.out.println("Not a valid input, please enter another integer or type STOP to print off linked list."); } } Node p = start; while (p != null) { System.out.print(p.getItem()); if (p.getNext() != null) System.out.print(", "); p = p.getNext(); } System.out.println(); return start; }

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

Labor and Employment Law Text and Cases

Authors: David Twomey

15th edition

1133188281, 978-1133711841, 1133711847, 978-1285247632, 978-1133188285

More Books

Students also viewed these Programming questions