user2495 :
- 나는 두 가지 방법 분, 노드와 연결리스트의 노드의 최소 값의 최대 값을 찾을 수 최대를 기록 할.
- 예를 들면, 변수라고 ABC 상점은 {1, 78, -13, 42, 0, 14} 다음 abc.min ()를 호출한다 -9 abc.max () (78)를 리턴할지.
목록이 비어있는 경우는 -1에 돌아옵니다. 반환 값을 인쇄합니다.
정수의 배열을 입력하여 내가 링크 된 목록의 최대 및 최소 값을 계산하는 방법이 저를 도와주세요
```package demo; public class MaximumMinimum { class Node{ int data; Node next; public Node(int data) { this.data = data; this.next = null; } } //Represent the head and tail of the singly linked list public Node head = null; public Node tail = null; //addNode() will add a new node to the list public void addNode(int data) { //Create a new node Node newNode = new Node(data); //Checks if the list is empty if(head == null) { //If list is empty, both head and tail will point to new node head = newNode; tail = newNode; } else { //newNode will be added after tail such that tail's next will point to newNode tail.next = newNode; //newNode will become new tail of the list tail = newNode; } } //minNode() will find out the minimum value node in the list public void minNode() { Node current = head; int min; if(head == null) { System.out.println("List is empty"); } else { //Initializing min with head node data min = head.data; while(current != null){ //If current node's data is smaller than min //Then, replace value of min with current node's data if(min > current.data) { min = current.data; } current= current.next; } System.out.println("Minimum value node in the list: "+ min); } } //maxNode() will find out the maximum value node in the list public void maxNode() { Node current = head; int max; if(head == null) { System.out.println("List is empty"); } else { //Initializing max with head node data max = head.data; while(current != null){ //If current node's data is greater than max //Then, replace value of max with current node's data if(max < current.data) { max = current.data; } current = current.next; } System.out.println("Maximum value node in the list: "+ max); } } public static void main(String[] args) { MaximumMinimum sList = new MaximumMinimum(); //Adds data to the list sList.addNode(5); sList.addNode(8); sList.addNode(1); sList.addNode(6); //Display the minimum value node in the list sList.minNode(); *//Display the maximum value node in the list * sList.maxNode(); } } ```
Aashish Pawar :
코드는 올바른 방식으로 실행 주어진 당신.
당신은 또한 사용할 수 있습니다
MaximumMinimum sList = new MaximumMinimum();
List<Integer> list = new ArrayList<>();
Node head = sList.head;
while(head != null){
list.add(head.data);
head= head.next;
}
//no recommended if you want to design your own method
System.out.println(list);
System.out.println(Collections.max(list));
System.out.println(Collections.min(list));
정수형 어레이를 입력
public void stores(int[] array)
{
for(int element:array)
{
this.addNode(element);
}
}
다음 주에 실행하는 경우
int[] elements = {1, 78, -9, 42 , 0, 14};
sList.stores(elements);
sList.maxNode(); //78
sList.minNode();//-9
또한 당신은 사용할 수 있습니다 Arrays.stream(array_name).forEach(e->sList.add(e))
당신은 자바 8 방법으로 그것을 수행하려는 경우.