Java AssignmentUsing the ListNode.java file below Write method.pdf

ambersushil 21 views 1 slides Mar 30, 2023
Slide 1
Slide 1 of 1
Slide 1
1

About This Presentation

Java Assignment
Using the ListNode.java file below
Write methods called min and max that return the smallest and largest values in the linked list.
These methods will be added to your ListNode class. For example if a variable called list stores
{11, -7, 3, 42, 0, 14], the call of list.min() should r...


Slide Content

Java Assignment
Using the ListNode.java file below

Write methods called min and max that return the smallest and largest values in the linked list.
These methods will be added to your ListNode class. For example if a variable called list stores
{11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return
42. If the list is empty, return -1. Print the returned value.
Write a method called insertNode that inserts a new node anywhere in your linked list. Display
your linked list with new value.
Write a program to test these methods to ensure accuracy and successful implementation. Upload
your modified version of the ListNode java file only.
Note: Original code should still be included. You will make additions and slight modifications
where necessary. Throw exceptions where you think is necessary. // Listlode is a class for storing
a single node of a linked // list. This node class is for a list of integer values. public class Listlode
\{ public int data; //data stored in this node public Listliode front;//points to head node public
Listliode next; /f link to next node in the list // post: constructs a node with data and null link
public Listlode() \{ 3. // post: constructs a node with given data and null link: public Listlode(int
data) \{ this(data, null); 3 // post: constructs a node with given data and given link. public
Listlode(int data, Listliode next) \{ this.data = data; this. next = next; 3 public String toString() \{
if (front = null) : \} else \{ String result = " ["+ front data; Listliode current = front. next; while
(current != null) \{ result +=m,m+ current data, current = current. next; 3 result += " ]n - return
result; 3. }// post: appends the given value to the end of the list public void add(int value) \&
puolic void aod if (front = nult) \{. front = new Listlode (value); 3 else \{ Listlode current =
front; whistiode current = front; while (current next I= null) \{ current = current. next; 3 current.
next = new Listliode (value ); 3. 3 J// post : returns the position of the first occurrence of the
given /l value (-1 if not found ) public void remove (int index) \{ if ( index =){ \} else \{ Listiode
current = nodeAt ( index - 1); current. next = current. next. next, 3 3 3// pre : <=i< size() // post:
returns a reference to the node at the given index public Listlode nodeAt (int index) : List Node
current = front; for (int i=0;i< index; i++) \{ current = current. next. \}. return current; 3
Tags