简体   繁体   中英

Smallest element in a Linked List in Java

I am trying to create a linked list of 10 unordered integers, output the list, then find the smallest element in the list and output it. This is what I have. It finds and output an element but not the smallest in the list. Please help.

import java.util.LinkedList;
import java.util.Random;

public class intergers
{

    public static void main(String[] args)
    {
        LinkedList <Integer> integers = new LinkedList<Integer>();

        //Generates an unordered list of ten integers.
        Random Doge = new Random();


        //Using a for loop.

        for(int count =0; count<10; count++)
        {
            int integer = Doge.nextInt(10);
            integers.add(integer);
        }
        if(integers.getLast() !=0)
        {
            integers.removeLast();
            integers.add(0);
        }

        System.out.println(integers); //Prints out that list.
        int Oedipus; 
        for(Oedipus =0; Oedipus <integers.size()-1; Oedipus++)
        {
            if(integers.get(Oedipus) < integers.get(Oedipus++))
            {
                //int smallest = integers.get(Oedipus);
                int smallest = integers.get(Oedipus);
            }

            if(integers.get(Oedipus) > integers.get(Oedipus++))
            {
                //int smallest = integers.get(Oedipus);
                //System.out.println("Smallest " + integers.get(Oedipus));
            }
            int smallest = integers.size();
        }
        int smallest = integers.get(Oedipus);
        System.out.println("The smallest element is: " + smallest);

    }
}

您是否尝试过使用Collections.min()方法?

This line

System.out.println(integers); //Prints out that list.

does not do what the comment says it does. Add a loop to go through the items, and print each item individually to see what's in the list.

Your lines that deal with smallest , eg this one

int smallest = integers.get(Oedipus);

are incorrect for two reasons: they declare a new smallest every time in a nested scope, and they do not initialize smallest to begin with.

To fix this, declare smallest outside the loop, initialize it with the initial element of the list, and then iterate elements starting from 1 , comparing it to smallest . Alternatively, store the index , not the value, of the smallest element in the smallest . Initialize it with zero, and set it to Oedipus if you find an element that is smaller than the one at integers.get(smallest) .

You just need to iterate through the list and try to find the minimum element.

int minInt = Integer.MAX_VALUE
for(int i = 0; i < Integers.size(), ++i){
    if(Integers.get(i) < minInt)
        minInt = Integers.get(i)
}

Edit: The reason I stored the value rather than the index is because random access of linked lists is slow. If it was an array then random access would be fast.

In addition to removing the unnecessary declarations of smallest , you are also resetting smallest to the size of the LinkedList every time that you iterate through with your int smallest = integers.size(); code. You should remove this along with the code at the end that states:

int smallest = integers.get(Oedipus);
System.out.println("The smallest element is: " + smallest);

All this does is it redefines smallest as the last element.

Your approach is somewhat strange. Also you should consider the scope of variables. the "smallest" variables that you have through your code are different.

Analyze the following redefined function

public static void main(String[] args) {
    // TODO code application logic here
    LinkedList <Integer> integers = new LinkedList<Integer>();

    //Generates an unordered list of ten integers.
    Random Doge = new Random();

    //Using a for loop.
    for(int count =0; count<10; count++)
    {
        int integer = Doge.nextInt(10);
        integers.add(integer);
    }

    int smallest = integers.get(0);
    System.out.println(integers); //Prints out that list.
    for( int Oedipus =1; Oedipus <integers.size()-1; Oedipus++)
    {
        if(integers.get(Oedipus) < smallest )
        {
            //int smallest = integers.get(Oedipus);
            smallest = integers.get(Oedipus);
        }
    }
    System.out.println("The smallest element is: " + smallest);     
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM