I missed one of my classes for data structures. Can anyone give me a general guide in solving this simple recursive problem using Linked lists. Where one has to find the min and max values of integers stored in a linked lists. Here is methods i need to use.

Write a class called MinMax that has two short recursive methods that find the minimum and maximum
values in a LinkedList ADT of Integer values without using any loops:

1 public int getMin(LinkedList<Integer> list) {
2 ...
3 }
4
5 public int getMax(LinkedList<Integer> list) {
6 ...
7 }

Thanks, all help appreciated.

you're not looking for help, you're looking to cheat on an assignment.
first try to write something yourself.

Hey don't worry, i solved the question. No i was not looking to cheat, i was looking for general guide to help put me in the right direction and get the question done in a more efficient manner. Isn't that what a forum is for? i can't believe the judgement i am getting because we can't talk face to face, i am sure this discussion would be dealt diffrently instead of accusing me of cheating and dismissing me for any help or friendly advice/gesture. In any case if any one is interested in the answer, this is what i got. If it can be improved in any way please let me know. Thanks James for showing me the forum guidelines rather than giving such judgment.

Worked out i had to use the getLast, getFirst and remove linkedlist methods to shrink the list to size of 1 which would give the min or max value as desired.

import java.util.*;
/** This class creates a int list and finds the min and max values of the list**/
public class Week02_ex02 {
    public static void main(String[] args) { 


        //creates a new object referenced to the Week02_ex02 Class
        Week02_ex02 Ex2 = new Week02_ex02();

        //Creates a list from the createList Method in Ex2
        LinkedList<Integer> list = Ex2.createList();
        System.out.println(list);

        //Calls the getMin method in Ex2 Class
        int min = Ex2.getMin(list);

        //Creates an identical list to the first one created to find the Max values
        LinkedList<Integer> list2 = Ex2.createList();

        //initialize max to the return method of getMax
        int max = Ex2.getMax(list2);


        //prints out the minimum value in the list
        System.out.println("the lowest value of list is "+ min);
        System.out.println("the highest value of list is "+ max);







    }
    /**This method creates a LinkedList with integer Values **/
    public LinkedList<Integer> createList() {

        LinkedList<Integer> list = new LinkedList<Integer>();
        list.add(5);
        list.add(7);
        list.add(3);
        list.add(20);
        list.add(9);

        return list;

    }
    /** This method finds the minimum integer value of the list **/
    public int getMin(LinkedList<Integer> list) {

        //The base case, if the list size = 1, recursion is complete
        if (list.size() != 1) {

            //This if statement checks if the first element of the list in smaller than
            //the last and runs different operation according to this check.
            if (list.getFirst() < list.getLast()) {


                //elements in the list will be removed if they are not smaller than the
                //first element in the list
                list.remove(list.getLast());


            }
            else {
                list.remove(list.getFirst());
            }

            getMin(list);


        }

        int min = list.getFirst();
        return min;


    }

    /**This method finds the Max integer value of the list **/
    public int getMax(LinkedList<Integer> list) {

        //The base case, if the list size = 1, recursion is complete
        if (list.size() != 1) {

            //This if statement checks if the first element of the list in bigger than
            //the last and runs different operation according to this check.
            //elements in the list will be removed if they are not bigger than the
            //first element in the list
            if (list.getFirst() > list.getLast()) {
                list.remove(list.getLast());


            }
            else {
                list.remove(list.getFirst());
            }

            getMax(list);


        }

        int max = list.getFirst();
        return max;


    }

}

Edited 4 Years Ago by hamiltino: description error

Hello hamiltino
Thanks for sharing that solution with us - it's a neat algorithm.
Please don't be upset if posts in forum seem a bit terse in their posts. We do get a lot of first time posts that say "here's my homework question - give me the answer", and they all get a similar response.
I'm certain that you will find people here very helpful and friendly if/when you hit problems with your Java, including stultuske who has helped solved 460 problems and been granted a huge 1124 positive reputation points by his peers.

This article has been dead for over six months. Start a new discussion instead.