Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GamerList {
- private Node head;
- public GamerList() {
- this.head = null;
- }
- public void insert(String name, int score) {
- Node newNode = new Node(name, score);
- int index = 0;
- Node ref = head;
- Node prev = head;
- while (ref != null && score < ref.score)
- {
- ref = ref.next;
- prev = ref;
- index++;
- }
- /*
- if (index == 0)
- insertFirst(newNode);
- else if (index == getSize())
- insertLast(newNode);
- else {
- insertAt(name, score, index);
- }
- */
- insertAt(name, score, index);
- if (getSize() > 10) {
- Node ref2 = head;
- int count = 0;
- while (count < 9) {
- ref2 = ref2.next;
- count++;
- }
- ref2.next = null;
- }
- }
- public void insertAt(String n, int s, int pos)
- {
- Node newNode = new Node(n, s);
- if (pos == 0)
- {
- insertFirst(newNode);
- return;
- }
- if (pos == getSize())
- {
- insertLast(newNode);
- return;
- }
- int start = 0;
- Node ref = head;
- Node prev = head;
- while (start <= pos)
- {
- prev = ref;
- ref = ref.next;
- start++;
- }
- //Node newNode = new Node(n, s);
- newNode.next = ref;
- prev.next = newNode;
- }
- public void insertFirst(Node node)
- {
- node.next = head;
- head = node;
- }
- public void insertLast(Node node) {
- if (head == null)
- head = node;
- else if (head.next == null)
- head.next = node;
- else {
- Node ref = head;
- while (ref.next != null) {
- ref = ref.next;
- }
- ref.next = node;
- }
- }
- public void printList() {
- if (head == null)
- System.out.println("List is empty");
- Node ref = head;
- while (ref != null) {
- System.out.println(ref.name + ": " + ref.score);
- ref = ref.next;
- }
- }
- public int getSize() {
- Node ref = head;
- int count = 0;
- while (ref != null) {
- ref = ref.next;
- count++;
- }
- return count;
- }
- class Node {
- private String name;
- private int score;
- private Node next;
- public Node(String name, int score) {
- this.name = name;
- this.score = score;
- this.next = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment