Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Finding min in linked list

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Finding min in linked list

    There is something wrong with my min() method, I got max to work though.

    public double maximum()
    	{
    		DoubleNode compare = head;
    		if(size() == 1){
    			return compare.getItem();
    		}else if(size() >= 1){
    			while(compare.next() != null){
    				if(compare.next().getItem() > compare.getItem() || compare.next().getItem() == compare.getItem()){
    				compare = compare.next();
    			}
    		}
    		return compare.getItem();
    		}else{
    			return Double.NaN;
    		}
    	}
     
    	public double minimum()
    	{
    		DoubleNode compare = head;
    		Double i = 0.0;
    		if(size() == 1){
    			return compare.getItem();
    		}else if(size() >= 1){
    			while(compare.next() != null){
    				if(compare.next().getItem() < compare.getItem() || compare.next().getItem() == compare.getItem()){
    					compare = compare.next();
    				}
    			}
    		return compare.getItem();
    		}else{
    			return Double.NaN;
    		}
    	}


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Finding min in linked list

    as i studied your code acc to me your max method will also not work for every case.

    if(size() == 1){
    return compare.getItem();
    }else if(size() >= 1){
    while(compare.next() != null){
    if(compare.next().getItem() > compare.getItem() || compare.next().getItem() == compare.getItem()){
    compare = compare.next();
    }
    }
    if(size() == 1){
    }else if(size() >= 1){
    why you are writing size()>=1

    your above condition is already checking for size==1.

    while(compare.next() != null){
    if(compare.next().getItem() > compare.getItem() || compare.next().getItem() == compare.getItem()){
    compare = compare.next();
    }
    whether it will move further if the element at next is smaller than the current.


    try this input for your max function

    2,23,34,32,11


    Hope you will got the right path , where i want to direct you.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. [SOLVED] Linked List Help
    By lieles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 4th, 2011, 10:32 PM
  2. Linked List problem, please help.
    By Axeander in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 21st, 2010, 05:01 PM
  3. Help with linked list
    By joecool594 in forum Collections and Generics
    Replies: 3
    Last Post: November 28th, 2010, 12:33 PM
  4. Simple linked list
    By Koren3 in forum Collections and Generics
    Replies: 10
    Last Post: November 2nd, 2009, 03:33 AM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM