question archive Please make search method to find the the highest value in the linked list and then output the highest value Go through the double-ended doubly-linked list to find the largest int value held in any of the links
Subject:Computer SciencePrice:3.86 Bought9
Please make search method to find the the highest value in the linked list and then output the highest value
Go through the double-ended doubly-linked list to find the largest int value held in any of the links. If the doubly-linked list is empty or defective in any way, return 0.
I have most of the code written.
Please write the search() method which reports to the main method.
There is no input
Use java programming language
package linkedList;
import java.util.*;
public class Main {
public static void main(String args[] ) throws Exception {
Scanner myscanner = new Scanner(System.in);
int num = Integer.parseInt(myscanner.nextLine());
Link[] array = new Link[num];
for(int i=0;i
array[i]=new Link(Integer.parseInt(myscanner.nextLine()));
}
while(myscanner.hasNext()){
int select=myscanner.nextInt();
int previous=myscanner.nextInt();
int next=myscanner.nextInt();
if(previous!=-1){
array[select].previous=array[previous];
}
if(next!=-1){
array[select].next=array[next];
}
}
LinkedList mylist = new LinkedList();
if(num>0){
mylist.first=array[0];
mylist.last=array[num-1];
}
System.out.println(search(mylist));
}
//don't edit the code above, only edit the search method below
public static int search(LinkedList mylist){
/* return the highest value held in any of the links, 0 if defective or empty*/
return 0;
}
//don't edit the code below, only edit the search method above
}
class Link{
public int data;
public Link next;
public Link previous;
public Link(int input){
data=input;
next=null;
previous=null;
}
}
class LinkedList {
public Link first;
public Link last;
public LinkedList( ){
first=null;
last=null;
}
public boolean isEmpty( ){
return (first==null);
}
public void insertHead(Link insert){
if(isEmpty()){
first=insert;
last=insert;
}else{
first.previous=insert;
insert.next=first;
first=insert;
}
}
}
required search method would be
public static int search(LinkedList mylist) { /* return the highest value held in any of the links, 0 if defective or empty */ int max = Integer.MIN_VALUE; //if list empty if(mylist.isEmpty()) return 0; Link curr = mylist.first; while(curr!= mylist.last) { //if defective if(curr.data==-1) return 0; if(curr.data>max) max = curr.data; curr = curr.next; } if(curr.data>max) max = curr.data; return max; }
Complete code would be
Step-by-step explanation
Main.java
package linkedList; import java.util.*; public class Main { public static void main(String args[]) throws Exception { Scanner myscanner = new Scanner(System.in); int num = Integer.parseInt(myscanner.nextLine()); Link[] array = new Link[num]; for (int i = 0; i < num; i++) { array[i] = new Link(Integer.parseInt(myscanner.nextLine())); } System.out.println("end link"); int i =0; while (myscanner.hasNext()) { System.out.println("link : "+i++); int select = myscanner.nextInt(); int previous = myscanner.nextInt(); int next = myscanner.nextInt(); if (previous != -1) { array[select].previous = array[previous]; } if (next != -1) { array[select].next = array[next]; } } LinkedList mylist = new LinkedList(); if (num > 0) { mylist.first = array[0]; mylist.last = array[num - 1]; } System.out.println(search(mylist)); } //don't edit the code above, only edit the search method below public static int search(LinkedList mylist) { /* return the highest value held in any of the links, 0 if defective or empty */ int max = Integer.MIN_VALUE; //if list empty if(mylist.isEmpty()) return 0; Link curr = mylist.first; while(curr!= mylist.last) { //if defective if(curr.data==-1) return 0; if(curr.data>max) max = curr.data; curr = curr.next; } if(curr.data>max) max = curr.data; return max; } //don't edit the code below, only edit the search method above } class Link { public int data; public Link next; public Link previous; public Link(int input) { data = input; next = null; previous = null; } } class LinkedList { public Link first; public Link last; public LinkedList() { first = null; last = null; } public boolean isEmpty() { return (first == null); } public void insertHead(Link insert) { if (isEmpty()) { first = insert; last = insert; } else { first.previous = insert; insert.next = first; first = insert; } } }