Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.92 KB | None | 0 0
  1.  
  2.  * 13.10.2017
  3.  * Linked List in Java
  4.  * Author: underscores
  5.  */
  6.  
  7.  
  8.  /*
  9.   * (23:48:58) Sindre: so tell that guy to stick it and do it properly with submodules
  10.   */
  11.  
  12.   import java.util.Random;
  13.   import java.util.concurrent.TimeUnit;
  14.  
  15. public class LinkedList {
  16.  
  17.     private Node head;
  18.    
  19.     public LinkedList () {
  20.    
  21.         this.head = null;
  22.        
  23.     }
  24.    
  25.     public int pop() {
  26.    
  27.         int value;
  28.        
  29.         if(head.hasNext()) {
  30.             value = head.getValue();
  31.             head = head.getNext();
  32.             return value;
  33.         }
  34.        
  35.         else if (head != null) {
  36.             value = head.getValue();
  37.             head = null;
  38.             return value;
  39.         }
  40.        
  41.         else {
  42.             System.out.println("Error");
  43.             return 0;
  44.         }
  45.        
  46.     }
  47.    
  48.     public void iter() {
  49.    
  50.         Node cur = head;
  51.         int val;
  52.        
  53.         if(head == null) {
  54.             System.out.println("Nothing to show, list is empty");
  55.             return;
  56.         }
  57.        
  58.         do {
  59.             val = cur.getValue();
  60.             cur = cur.getNext();
  61.             System.out.println("Value :" + val);
  62.         } while(cur != null);
  63.        
  64.     }
  65.    
  66.     public void push(int n) {
  67.    
  68.         Node newNode = new Node(n);
  69.        
  70.         newNode.setNext(head);
  71.         head = newNode;
  72.        
  73.     }
  74.  
  75.     public void add(int n) {
  76.    
  77.         Node newNode = new Node(n);
  78.         Node current = head;
  79.        
  80.         if(head == null) {
  81.             head = newNode;
  82.         }
  83.        
  84.         else {
  85.        
  86.         while(current.hasNext()) {
  87.             current = current.getNext();
  88.         }
  89.        
  90.         current.setNext(newNode);
  91.        
  92.         }
  93.  
  94.     }
  95.    
  96.     public void bubble() {
  97.    
  98.         Node sorted = null;
  99.        
  100.         while(sorted != head.getNext()) {
  101.    
  102.             Node prev = null;
  103.             Node temp = head;
  104.            
  105.             while(sorted != temp.getNext()) {
  106.  
  107.                 if(temp.getValue() > temp.getNext().getValue()) {
  108.                     if(prev == null) {
  109.                         head = temp.getNext();
  110.                         temp.setNext(temp.getNext().getNext());
  111.                         head.setNext(temp);
  112.                        
  113.                     }
  114.                     else{
  115.                         prev.setNext(temp.getNext());
  116.                         temp.setNext(prev.getNext().getNext());
  117.                         prev.getNext().setNext(temp);
  118.                         prev = prev.getNext();
  119.                     }
  120.                    
  121.                 }
  122.                
  123.                 else {
  124.                     prev = temp;
  125.                     temp = temp.getNext();
  126.                    
  127.                 }
  128.             }
  129.            
  130.             sorted = temp;
  131.         }
  132.     }
  133.  
  134.     public static void main(String [] args) {
  135.    
  136.         LinkedList l = new LinkedList();
  137.         Random randomGenerator = new Random();
  138.         int randomint;
  139.         for(int i = 1; i<=200000; i++) {
  140.             randomint = randomGenerator.nextInt(200000);
  141.             l.add(randomint);
  142.         }
  143.         System.out.println("Bubble started");
  144.         long startTime = System.nanoTime();
  145.         l.bubble();
  146.         l.iter();
  147.         long difference = System.nanoTime() - startTime;
  148.         System.out.println("Execution time: " + String.format("%d min, %d sec", TimeUnit.NANOSECONDS.toHours(difference), TimeUnit.NANOSECONDS.toSeconds(difference) - TimeUnit.MINUTES.toSeconds(TimeUnit.NANOSECONDS.toMinutes(difference))));
  149.         System.out.println("Bubble ended");
  150.        
  151.     }
  152.    
  153. }
  154.  
  155. class Node {
  156.  
  157.     private int value;
  158.     Node next;
  159.    
  160.     public Node(int n) {
  161.    
  162.         this.value = n;
  163.        
  164.     }
  165.  
  166.     public int getValue() {
  167.    
  168.         return value;
  169.        
  170.     }
  171.  
  172.     public void setNext(Node n) {
  173.    
  174.         this.next = n;
  175.        
  176.     }
  177.  
  178.     public Node getNext() {
  179.    
  180.         return next;
  181.        
  182.     }
  183.  
  184.     public boolean hasNext() {
  185.    
  186.         return next != null;
  187.    
  188.     }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement