Advertisement
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);
- if (head == null) {
- head = newNode;
- }
- else if (head.score <= newNode.score) {
- newNode.next = head;
- head = newNode;
- }
- else
- insertLast(newNode);
- }
- 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 sort() {
- for (Node ref = head; ref != null; ref = ref.next) {
- for (Node ref2 = ref.next; ref2 != null; ref2 = ref2.next) {
- if (ref.score < ref2.score) {
- int tempScore = ref.score;
- String tempName = ref.name;
- ref.score = ref2.score;
- ref.name = ref2.name;
- ref2.score = tempScore;
- ref2.name = tempName;
- }
- }
- }
- if (getSize() > 10) {
- Node ref = head;
- int count = 0;
- while (count < 9)
- {
- ref = ref.next;
- count++;
- }
- ref.next = null;
- }
- }
- 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
Advertisement