user2627732
user2627732

Reputation: 1

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);

    }
}

Upvotes: 0

Views: 2696

Answers (5)

Pat
Pat

Reputation: 310

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.

Upvotes: 0

Girauder
Girauder

Reputation: 185

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);     
}

Upvotes: 0

Archibald
Archibald

Reputation: 456

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.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726929

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, e.g. 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).

Upvotes: 1

tmanion
tmanion

Reputation: 401

Have you tried using the Collections.min() method?

Upvotes: 3

Related Questions