Advertisement
Mahmoud_Hawara

All about Doubly Linked List

Nov 15th, 2022
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.02 KB | None | 0 0
  1. public class Solver
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         DLinkedList dls = new DLinkedList();
  6.  
  7.         dls.insertFirst(0);
  8.         dls.insertFirst(1);
  9.         dls.insertFirst(2);
  10.         dls.insertFirst(3);
  11.         dls.insertFirst(4);
  12.  
  13.         dls.reverse();
  14.         dls.reverse();
  15.  
  16.         dls.rotateLeft();
  17.         dls.rotateLeft();
  18.         dls.rotateLeft();
  19.         dls.rotateLeft();
  20.         dls.rotateLeft();
  21.  
  22.  
  23.         dls.display();
  24.     }
  25. }
  26.  
  27. class Node
  28. {
  29.     int data;
  30.     Node next, prev;
  31.  
  32.     public Node(int d)
  33.     {
  34.         data = d;
  35.     }
  36. }
  37.  
  38. class DLinkedList
  39. {
  40.     Node first, last;
  41.  
  42.     public void insertFirst(int d)
  43.     {
  44.         Node temp = new Node(d);
  45.         if (isEmpty() == false)
  46.         {
  47.             first.prev = temp;
  48.         }
  49.         temp.next = first;
  50.         first = temp;
  51.         if (last == null)
  52.         {
  53.             last = first;
  54.         }
  55.         return;
  56.     }
  57.  
  58.     public void insertLast(int d)
  59.     {
  60.         if (isEmpty())
  61.         {
  62.             insertFirst(d);
  63.             return;
  64.         }
  65.         Node temp = new Node(d);
  66.         last.next = temp;
  67.         temp.prev = last;
  68.         last = temp;
  69.         return;
  70.     }
  71.  
  72.     public Node deleteFirst()
  73.     {
  74.         if (isEmpty())
  75.         {
  76.             System.out.println("Can't delete, The Linked List is Empty");
  77.             return null;
  78.         }
  79.         Node temp = first;
  80.         if (first == last)
  81.         {
  82.             first = last = null;
  83.         }
  84.         else
  85.         {
  86.             first.next.prev = null;
  87.             first = first.next;
  88.         }
  89.         return temp;
  90.     }
  91.  
  92.     public Node deleteLast()
  93.     {
  94.         if (isEmpty() || first == last)
  95.         {
  96.             return deleteFirst();
  97.         }
  98.         Node temp = last;
  99.         last.prev.next = null;
  100.         last = last.prev;
  101.         return temp;
  102.     }
  103.  
  104.     public Node deleteElementNumK(int k)
  105.     {
  106.         if (isEmpty())
  107.         {
  108.             System.out.println("Can't delete, The Linked List is Empty");
  109.             return null;
  110.         }
  111.         if (k <= 0)
  112.         {
  113.             System.out.println("Can't delete, The Linked List is 1 indexed");
  114.             return null;
  115.         }
  116.         Node cur = first;
  117.         for (int i = 1; i < k && cur != null; i++)
  118.         {
  119.             cur = cur.next;
  120.         }
  121.         if (cur == null)
  122.         {
  123.             System.out.println("Can't delete, The Size of The Linked List less than " + k);
  124.             return null;
  125.         }
  126.  
  127.         if (cur.prev == null)first = cur.next;
  128.         else cur.prev.next = cur.next;
  129.        
  130.         if (cur.next == null)last = cur.prev;
  131.         else cur.next.prev = cur.prev;
  132.        
  133.         return cur;
  134.     }
  135.  
  136.     public void reverse()
  137.     {
  138.         Node cur = first;
  139.         while (cur != null)
  140.         {
  141.             Node temp = cur.next;
  142.             cur.next = cur.prev;
  143.             cur.prev = temp;
  144.             cur = cur.prev;
  145.         }
  146.         Node temp = first;
  147.         first = last;
  148.         last = temp;
  149.         System.out.println("Done");
  150.         return;
  151.     }
  152.  
  153.     public void swap(int x, int y)
  154.     {
  155.         if (x <= 0 || y <= 0)
  156.         {
  157.             System.out.println("Can't swap, The Linked List is 1 indexed");
  158.             return;
  159.         }
  160.         if (y < x)
  161.         {
  162.             int temp = x;
  163.             x = y;
  164.             y = temp;
  165.         }
  166.         Node n1 = null, n2 = null, cur = first;
  167.         for (int i = 1; i < y && cur != null; i++)
  168.         {
  169.             if (i == x)n1 = cur;
  170.             cur = cur.next;
  171.         }
  172.         if (cur == null)
  173.         {
  174.             System.out.println("Can't swap, The size of the Linked List is less than " + y);
  175.             return;
  176.         }
  177.         if (x == y)return;
  178.         n2 = cur;
  179.        
  180.         if (n1 == first)first = n2;
  181.         else n1.prev.next = n2;
  182.         if (n2 == last)last = n1;
  183.         else n2.next.prev = n1;
  184.         n1.next.prev = n2;
  185.         if (y - x > 1)n2.prev.next = n1;
  186.         else n1.next = n1;
  187.         Node temp = n1.next;
  188.         n1.next = n2.next;
  189.         n2.next = temp;
  190.         temp = n1.prev;
  191.         n1.prev = n2.prev;
  192.         n2.prev = temp;
  193.  
  194.         return;
  195.     }
  196.  
  197.     public void display()
  198.     {
  199.         if (isEmpty())
  200.         {
  201.             System.out.print("The Linked List is Empty");
  202.         }
  203.         Node cur = first;
  204.         while (cur != null)
  205.         {
  206.             System.out.print(cur.data + " ");
  207.             cur = cur.next;
  208.         }
  209.         System.out.println();
  210.     }
  211.  
  212.     public void rotateLeft()
  213.     {
  214.         if (isEmpty())
  215.         {
  216.             System.out.print("The Linked List is Empty");
  217.             return;
  218.         }
  219.         if (first == last)return;
  220.         Node temp = first;
  221.         first = first.next;
  222.         first.prev = null;
  223.         last.next = temp;
  224.         temp.prev = last;
  225.         last = temp;
  226.         temp.next = null;
  227.         return;
  228.     }
  229.  
  230.     public boolean isEmpty()
  231.     {
  232.         return first == null;
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement