Advertisement
Nick-O-Rama

BirdList

Apr 27th, 2015
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. public class BirdList {
  2.  
  3.     private Node head;
  4.    
  5.     public BirdList()
  6.     {
  7.         head = null;
  8.     }
  9.    
  10.     // --- INSERT METHODS --- //
  11.     public void insert(String n)
  12.     {
  13.         if (head == null) {
  14.             insertFirst(n);
  15.             return;
  16.         }
  17.         if (find(n)) {
  18.             int pos = findIndex(n);
  19.             int start = 0;
  20.             Node ref = head;
  21.             while (start < pos) {
  22.                 ref = ref.next;
  23.                 start++;
  24.             }
  25.             ref.frequency++;
  26.         }
  27.         else insertLast(n);
  28.     }
  29.    
  30.     public void insertFirst(String n)
  31.     {
  32.         Node newNode = new Node(n);
  33.         newNode.next = head;
  34.         head = newNode;
  35.     }
  36.    
  37.     public void insertLast(String n)
  38.     {
  39.         Node newNode = new Node(n);
  40.         if (head == null)
  41.             head = newNode;
  42.         else if (head.next == null)
  43.             head.next = newNode;
  44.         else {
  45.             Node ref = head;
  46.             while (ref.next != null)
  47.             {
  48.                 ref = ref.next;
  49.             }
  50.             ref.next = newNode;
  51.         }
  52.     }
  53.    
  54.     // --- Size Method --- //
  55.     public int getSize()
  56.     {
  57.         Node ref = head;
  58.         int size = 0;
  59.         while (ref != null)
  60.         {
  61.             size++;
  62.             ref = ref.next;
  63.         }
  64.         return size;
  65.     }
  66.    
  67.     // --- FIND METHODS --- //
  68.     public boolean find(String n) // Returns true if name is found in list
  69.     {
  70.         Node ref = head;
  71.         while (ref != null)
  72.         {
  73.             if (ref.name.equals(n))
  74.                 return true;
  75.             ref = ref.next;
  76.         }
  77.         return false;
  78.     }
  79.    
  80.     public int findIndex(String n)
  81.     {
  82.         int index = -1;
  83.         if (!find(n))
  84.             return index;
  85.         else {
  86.             Node ref = head;
  87.             index = 0;
  88.             while (ref != null) {
  89.  
  90.                 if (ref.name.equals(n))
  91.                     break;
  92.                 index ++;
  93.                 ref = ref.next;
  94.             }
  95.             return index;
  96.         }
  97.     }
  98.    
  99.     // --- SORT METHODS --- //
  100.     public void sort()
  101.     {
  102.         for (Node ref = head; ref != null; ref = ref.next) {
  103.             for (Node ref2 = ref.next; ref2 != null; ref2 = ref2.next) {
  104.                 if (ref.frequency < ref2.frequency) {
  105.                     String tempName = ref.name;
  106.                     int tempFreq = ref.frequency;
  107.                     ref.name = ref2.name;
  108.                     ref.frequency = ref2.frequency;
  109.                     ref2.name = tempName;
  110.                     ref2.frequency = tempFreq;
  111.                 }
  112.             }
  113.         }
  114.     }
  115.     public void remove(String n)
  116.     {
  117.         if (find(n))
  118.         {
  119.             Node ref = head;
  120.             Node prev = head;
  121.             while (!ref.name.equals(n)) {
  122.                 prev = ref;
  123.                 ref = ref.next;
  124.             }
  125.             if (ref == head)
  126.                 head = head.next;
  127.             prev.next = ref.next;
  128.         }
  129.     }
  130.    
  131.    
  132.    
  133.     // --- PRINT METHODS --- ///
  134.     public void printList()
  135.     {
  136.         Node ref = head;
  137.         while (ref != null)
  138.         {
  139.             System.out.println(ref.name + ": " + ref.frequency);
  140.             ref = ref.next;
  141.         }
  142.     }
  143.    
  144.     class Node {
  145.         private String name;
  146.         private int frequency;
  147.         private Node next;
  148.  
  149.         public Node(String n) {
  150.             this.name = n;
  151.             this.frequency = 1;
  152.             next = null;
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement