Fatty Low
Fatty Low

Reputation: 109

Double linked list return top 10 scores in JAVA

I am looking for some help. I have total 11 scores and I need to return the top 10 scores only. I'm new to JAVA. If anyone can help point out my mistakes, thank you very much!

Below is the scores I want to add:

list.add(1);

list.add(2);

list.add(3);

list.add(4);

list.add(5);

list.add(6);

list.add(7);

list.add(8);

list.add(9);

list.add(10);

list.add(11);

list.show();

Below is my double linked list class:

    class DoubleLinkedList{   
        
        public int size() {

            int count = 0;
            Node p = head;

            while(p != null) {

                count++;
                p = p.next;
            }

            return count;
        }
        

        class Node{  
            int data;  
            Node previous;  
            Node next;  
       
            public Node(int data) {  
                this.data = data;  
            }  
        }  

        Node head, tail = null;  
       
  
        public void add(int data) {  

            Node newNode = new Node(data);  
       

            if(head == null) {  
                head = tail = newNode;  
                //head's previous will be null  
                head.previous = null;  
                //tail's next will be null  
                tail.next = null;  
            }
            else {  
 
                tail.next = newNode;  

                newNode.previous = tail;  
 
                tail = newNode;  

                tail.next = null;  
            } 
            
            if(size() > 10) {

                Node node1 = head;

                for (int i = 0; i < 9; i++) {
                    node1 = node1.next;
                }
                node1.next = null;
            }
            
        }  
       
        public void show() {  

            Node current = head;  
            if(head == null) {  
                System.out.println("Double linked list is empty");  
                return;  
            }  
 
            while(current != null) {  
     
                System.out.println("[score=" + current.data+ "]");
                current = current.next;  
            }  
        }  
    }

My current output is:

[score=1]

[score=2]

[score=3]

[score=4]

[score=5]

[score=6]

[score=7]

[score=8]

[score=9]

[score=10]

But, my expected output is:

 [score=11]
 [score=10]
 [score=9]
 [score=8]
 [score=7]
 [score=6]
 [score=5]
 [score=4]
 [score=3]
 [score=2]

Upvotes: 1

Views: 162

Answers (1)

Kyle Millar
Kyle Millar

Reputation: 256

In your code, the add() function is appending new scores to the end of the list. When the list’s size > 10, it removes a node from the end of the list. Since you are adding the smaller scores first and the larger scores last, when you add the score 11, it is immediately removed because it is at the end of the list. That would explain why your output is numbers 1-10.

Instead, you should remove from the front of the list. However, this is going to remove the first element added to the list, not necessarily the smallest score. If the scores are not added in increasing order, this code will not work. I would suggest looking into the PriorityQueue class if you need to handle adding scores out of order.

Upvotes: 2

Related Questions