1

so im writing a function that will find the second largest key in an un-ordered symbol table using a linked list implementation, the code I have so far isnt working right and was wondering if someone had any tips thanks!

public Key secondLargestKey () {
          if(first == null) return null;
          if(size()<=1)  return null;
          Node secondMax=null;
          Node Max=first;
           for (Node pointer=first.next;pointer.next!=null;pointer=pointer.next) {
              if(Max.key.compareTo(pointer.key)<=0) {
                 secondMax=Max;
                 Max=pointer.next; 
               }
              else {
                 secondMax=Max.next;
                 Max=pointer; 
              }

              }   
            return Max.key;
        }`

Output:

secondLargestKeyTest: Correct  String  Answer: null
secondLargestKeyTest: Correct  String A Answer: null
secondLargestKeyTest: *Error*  String AB Expected A Actual: B
secondLargestKeyTest: Correct  String ABC Actual: B
secondLargestKeyTest: Correct  String ABABABC Actual: B
secondLargestKeyTest: *Error*  String ZAYBXC Expected Y Actual: Z
Sam M
  • 13
  • 4
  • When `pointer` is larger than `Max`, previous `Max` becomes `secondMax` and `pointer` becomes new `Max`; `Max.next` and `pointer.next` are of no consequence. And at the end of it all, `secondMax` is the desired result. – Kevin Anderson Sep 22 '18 at 01:23
  • thanks for the reply, is this what you mean with max becoming second max and pointer becoming new max? if(Max.key.compareTo(pointer.key)<=0) { secondMax=Max; Max=pointer; } else { Max=secondMax; pointer=Max; } } return secondMax.key; } – Sam M Sep 22 '18 at 01:37
  • I led you slightly astray, it's a little more complicated. If `pointer.key` > `secondMax.key` AND also `pointer.key` > `Max.key`, then `secondMax` becomes old `Max` and `Max` becomes `pointer`; but if if `pointer.key` > `secondMax` only, `secondMax` becomes `pointer` and `Max` stays the same. – Kevin Anderson Sep 22 '18 at 02:12
  • when I use this code i am getting a null pointer exception if(first == null) return null; if(size()<=1) return null; Node secondMax=null; Node Max=first; for (Node pointer=first.next;pointer.next!=null;pointer=pointer.next) { if(pointer.key.compareTo(secondMax.key)>0 && pointer.key.compareTo(Max.key)>0) { secondMax=Max; Max=pointer; } else if(pointer.key.compareTo(secondMax.key)>0) { secondMax=pointer; } } return secondMax.key; } – Sam M Sep 22 '18 at 02:35
  • Well, in order for the algorithm to work, you can't avoid starting off with at least `secondMax == null` so your code needs to be prepared to deal with that. For example, instead of just `if (secondMax.key.compareTo(pointer.key) <= 0)...`, you'd write `if (secondMax==null || secondMax.key.compareTo(pointer.key) <= 0)...` – Kevin Anderson Sep 22 '18 at 03:17
  • great thanks for the help – Sam M Sep 22 '18 at 04:14

1 Answers1

1

Your code is close to being correct. The terminating condition in your for loop needs to check that pointer!=null, not pointer.next!=null. Also, if pointer.key is less than Max, you then need to compare it to secondMax, and accept it if it's greater, or secondMax is null (i.e. not yet set)

Here's some code for reference:

static <E extends Comparable<E>> E secondMax(Node<E> head)
{
  if(head == null) return null;

  E max2 = null;
  E max1 = head.key;
  for(Node<E> curr=head.next; curr!=null; curr=curr.next)
  {
    if(curr.key.compareTo(max1) >= 0)
    {
      max2 = max1;
      max1 = curr.key;
    }
    else if(max2 == null || curr.key.compareTo(max2) > 0)
    {
      max2 = curr.key;
    }
  }
  return max2;
}

static class Node<E>
{
  E key;
  Node<E> next;
  public Node(E k)
  {
    key = k;
  }
}
RaffleBuffle
  • 5,396
  • 1
  • 9
  • 16