Nick-O-Rama

GamerList2

Apr 22nd, 2015
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 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.         int index = 0;
  13.         Node ref = head;
  14.         Node prev = head;
  15.         while (ref != null && score < ref.score)
  16.         {
  17.             ref = ref.next;
  18.             prev = ref;
  19.             index++;
  20.         }
  21.         /*
  22.         if (index == 0)
  23.             insertFirst(newNode);
  24.         else if (index == getSize())
  25.             insertLast(newNode);
  26.         else {
  27.             insertAt(name, score, index);
  28.         }
  29.         */
  30.         insertAt(name, score, index);
  31.        
  32.         if (getSize() > 10) {
  33.             Node ref2 = head;
  34.             int count = 0;
  35.             while (count < 9) {
  36.                 ref2 = ref2.next;
  37.                 count++;
  38.             }
  39.             ref2.next = null;
  40.         }
  41.        
  42.     }
  43.    
  44.     public void insertAt(String n, int s, int pos)
  45.     {
  46.         Node newNode = new Node(n, s);
  47.         if (pos == 0)
  48.         {
  49.             insertFirst(newNode);
  50.             return;
  51.         }
  52.         if (pos == getSize())
  53.         {
  54.             insertLast(newNode);
  55.             return;
  56.         }
  57.         int start = 0;
  58.         Node ref = head;
  59.         Node prev = head;
  60.         while (start <= pos)
  61.         {
  62.             prev = ref;
  63.             ref = ref.next;
  64.             start++;
  65.         }
  66.         //Node newNode = new Node(n, s);
  67.         newNode.next = ref;
  68.         prev.next = newNode;
  69.     }
  70.    
  71.     public void insertFirst(Node node)
  72.     {
  73.         node.next = head;
  74.         head = node;
  75.     }
  76.  
  77.     public void insertLast(Node node) {
  78.         if (head == null)
  79.             head = node;
  80.         else if (head.next == null)
  81.             head.next = node;
  82.         else {
  83.             Node ref = head;
  84.             while (ref.next != null) {
  85.                 ref = ref.next;
  86.             }
  87.             ref.next = node;
  88.         }
  89.     }
  90.    
  91.     public void printList() {
  92.         if (head == null)
  93.             System.out.println("List is empty");
  94.         Node ref = head;
  95.         while (ref != null) {
  96.             System.out.println(ref.name + ": " + ref.score);
  97.             ref = ref.next;
  98.         }
  99.     }
  100.  
  101.     public int getSize() {
  102.         Node ref = head;
  103.         int count = 0;
  104.         while (ref != null) {
  105.             ref = ref.next;
  106.             count++;
  107.         }
  108.         return count;
  109.     }
  110.  
  111.     class Node {
  112.         private String name;
  113.         private int score;
  114.         private Node next;
  115.  
  116.         public Node(String name, int score) {
  117.             this.name = name;
  118.             this.score = score;
  119.             this.next = null;
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment