Advertisement
Nick-O-Rama

GamerList

Apr 17th, 2015
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1.  
  2. public class GamerList {
  3.    
  4.     private Node head;
  5.    
  6.     public GamerList() {
  7.         this.head = null;
  8.     }
  9.    
  10.     public void insert(String name, int score) {
  11.         Node newNode = new Node(name, score);
  12.         if (head == null) {
  13.             head = newNode;
  14.         }
  15.         else if (head.score <= newNode.score) {
  16.             newNode.next = head;
  17.             head = newNode;
  18.         }
  19.         else
  20.             insertLast(newNode);
  21.            
  22.     }
  23.  
  24.     public void insertLast(Node node) {
  25.         if (head == null)
  26.             head = node;
  27.         else if (head.next == null)
  28.             head.next = node;
  29.         else {
  30.             Node ref = head;
  31.             while (ref.next != null) {
  32.                 ref = ref.next;
  33.             }
  34.             ref.next = node;
  35.         }
  36.     }
  37.    
  38.     public void sort() {
  39.         for (Node ref = head; ref != null; ref = ref.next) {
  40.             for (Node ref2 = ref.next; ref2 != null; ref2 = ref2.next) {
  41.                 if (ref.score < ref2.score) {
  42.                     int tempScore = ref.score;
  43.                     String tempName = ref.name;
  44.                     ref.score = ref2.score;
  45.                     ref.name = ref2.name;
  46.                     ref2.score = tempScore;
  47.                     ref2.name = tempName;
  48.                 }
  49.             }
  50.         }
  51.         if (getSize() > 10) {
  52.             Node ref = head;
  53.             int count = 0;
  54.             while (count < 9)
  55.             {
  56.                 ref = ref.next;
  57.                 count++;
  58.             }
  59.             ref.next = null;
  60.         }
  61.     }
  62.  
  63.     public void printList() {
  64.         if (head == null)
  65.             System.out.println("List is empty");
  66.         Node ref = head;
  67.         while (ref != null) {
  68.             System.out.println(ref.name + ": " + ref.score);
  69.             ref = ref.next;
  70.         }
  71.     }  
  72.    
  73.     public int getSize() {
  74.         Node ref = head;
  75.         int count = 0;
  76.         while (ref != null) {
  77.             ref = ref.next;
  78.             count++;
  79.         }
  80.         return count;
  81.     }  
  82.  
  83.     class Node {
  84.         private String name;
  85.         private int score;
  86.         private Node next;
  87.        
  88.         public Node(String name, int score) {
  89.             this.name = name;
  90.             this.score = score;
  91.             this.next = null;
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement