Advertisement
Guest User

Untitled

a guest
Apr 17th, 2017
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1.  
  2.  
  3. package hwl;
  4.  
  5. /*  @author abozainih */
  6.  
  7. public class Hwl {
  8.  
  9.     public static void main(String[] args) {
  10.         LinkedList<Integer> x = new LinkedList<>();
  11.         x.addFirst(2);
  12.         x.addFirst(10);
  13.         x.addFirst(3);
  14.         x.addFirst(18);
  15.         x.addFirst(5);
  16.         x.print();
  17.         System.out.println("==============");
  18.         x.sort();
  19.         x.print();
  20.        
  21.     }
  22.    
  23. }
  24.    
  25.     class Node<E> {
  26.         E element;
  27.         Node<E> next;
  28.         public Node(E element){
  29.             this.element= element;
  30.         }
  31.     }
  32.    
  33.     class LinkedList< E extends Comparable> {
  34.         Node<E> head = null;
  35.         Node<E> tail = null;
  36.         int size = 0;
  37.        
  38.         public void addFirst(E x){
  39.             if(size == 0){
  40.                 head=new Node<E>(x);
  41.                 tail = head;
  42.                 size++;
  43.             }else {
  44.             Node<E> temp = new Node<E>(x);
  45.             temp.next = head;
  46.             head = temp;
  47.             size++;
  48.             }
  49.  
  50.         }
  51.         public void addLast(E x){
  52.             if(size == 0){
  53.                 head=new Node<E>(x);
  54.                 tail = head;
  55.                 size++;
  56.             }else {
  57.                 Node<E> temp = new Node<E>(x);
  58.                tail.next = temp;
  59.                tail = temp;
  60.                size++;
  61.             }
  62.         }
  63.         public void print(){
  64.            Node<E> current = head;
  65.            
  66.            while(current != null){
  67.                
  68.                System.out.println(current.element);
  69.                current= current.next;
  70.            }
  71.         }
  72.         public void add(int index, E x){
  73.             if(index == 0) addFirst(x);
  74.             else if(index == size-1) addLast(x);
  75.             else {
  76.                 Node<E> current = head;
  77.                 for (int i = 0; i < index; i++) {
  78.                     current = current.next;
  79.                 }
  80.                
  81.                 Node<E> temp = current.next;
  82.                 current.next = new Node<E>(x);
  83.                 current.next.next= temp;
  84.                 size++;  
  85.             }
  86.            
  87.         }
  88.        
  89.         public void sort(){
  90.             Node<E> current = head ;
  91.             Node<E> current2 = current.next;
  92.             E min = head.element;
  93.             E temp;
  94.             int pos = 0;
  95.             for (int i = 0; i < size-1; i++) {
  96.                 for (int j = 0; j < size; j++) {
  97.                     if(current2 != null){
  98.                         if(min.compareTo(current2.element) > 0){
  99.                           pos = j ;
  100.                           min = current2.element;
  101.                        
  102.                         }
  103.                         current2= current2.next;
  104.                     }
  105.                 }
  106.  
  107.                 temp = current.element;
  108.                 current.element = min;
  109.                 current = current.next;
  110.                 min = current.element;
  111.                 current2 = head;
  112.                 for (int j = 0; j <= pos; j++) {
  113.                     if(current2 !=null){
  114.                     if(j==pos){current2.element = temp;}
  115.                     current2= current2.next;
  116.                     }
  117.                 }
  118.                 current2 = current.next;  
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement