SHARE
TWEET

Untitled

a guest Dec 12th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Set;
  2. import java.util.HashSet;
  3.  
  4. class List {
  5.     Node first;
  6.  
  7.     List () {
  8.         this.first = null;
  9.     }
  10.  
  11.     public void insert (Node n) {
  12.         n.next = this.first;
  13.         this.first = n;
  14.     }
  15.  
  16.     public void print () {
  17.         Node temp = new Node();
  18.         temp.next = this.first;
  19.         while (temp.next != null) {
  20.             System.out.println(temp.next.str);
  21.             temp = temp.next;
  22.         }
  23.     }
  24.  
  25.     public Node find (String s) {
  26.         Node temp = new Node();
  27.         temp.next = this.first;
  28.         while (temp.next != null) {
  29.             if (temp.next.str == s) return temp.next;
  30.             temp = temp.next;
  31.         }
  32.         return null;
  33.     }
  34.  
  35.     public void remove (String s) {
  36.         Node temp = new Node();
  37.         temp.next = this.first;
  38.         Boolean run = true;
  39.         while (temp.next != null && run) {
  40.             if (temp.next.str == s) {
  41.                 temp.next = temp.next.next;
  42.                 run = false;
  43.             }
  44.             temp = temp.next;
  45.         }
  46.     }
  47.    
  48.     public void removeAll () {
  49.         this.first = new Node ();
  50.     }
  51.  
  52.     public void removeRepeats () {
  53.         Node run = new Node();
  54.         run.next = this.first;
  55.         Set<String> repeats = new HashSet();
  56.         while (run.next != null) {
  57.             if (repeats.contains(run.next.str)) run.next = run.next.next;
  58.             else {
  59.                 repeats.add(run.next.str);
  60.                 run = run.next;
  61.             }
  62.         }
  63.     }
  64.  
  65.     public static List merge (List l1, List l2) {
  66.         List merged = new List ();
  67.         Node temp = l1.first;
  68.         while (temp.next != null) {
  69.             temp = temp.next;
  70.         }
  71.         temp.next = l2.first;
  72.         merged.first = l1.first;
  73.         return merged;
  74.     }
  75.  
  76.     public static void main (String[] args) {
  77.         /*
  78.         List myList = new List ();
  79.         myList.insert(new Node ("1"));
  80.         myList.insert(new Node ("1"));
  81.         myList.insert(new Node ("2"));
  82.         myList.insert(new Node ("2"));
  83.         myList.insert(new Node ("3"));
  84.         myList.insert(new Node ("4"));
  85.         myList.print();
  86.         System.out.println(myList.find("2").str);
  87.         myList.remove("3");
  88.         myList.print();
  89.         myList.removeRepeats();
  90.         myList.print();
  91.         myList.removeAll();
  92.         myList.print(); */
  93.         List l1 = new List ();
  94.         List l2 = new List ();
  95.         l1.insert(new Node ("1"));
  96.         l1.insert(new Node ("2"));
  97.         l1.insert(new Node ("3"));
  98.         l2.insert(new Node ("4"));
  99.         l2.insert(new Node ("5"));
  100.         l2.insert(new Node ("6"));
  101.         List l3 = List.merge(l1, l2);
  102.         l3.print();
  103.     }
  104.  
  105. }
  106.  
  107. class Node {
  108.     String str;
  109.     Node next;
  110.  
  111.     Node () {
  112.         this.next = null;
  113.     }
  114.  
  115.     Node (String s) {
  116.         this.str = s;
  117.         this.next = null;
  118.     }
  119. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top