Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solver
- {
- public static void main(String args[])
- {
- DLinkedList dls = new DLinkedList();
- dls.insertFirst(0);
- dls.insertFirst(1);
- dls.insertFirst(2);
- dls.insertFirst(3);
- dls.insertFirst(4);
- dls.reverse();
- dls.reverse();
- dls.rotateLeft();
- dls.rotateLeft();
- dls.rotateLeft();
- dls.rotateLeft();
- dls.rotateLeft();
- dls.display();
- }
- }
- class Node
- {
- int data;
- Node next, prev;
- public Node(int d)
- {
- data = d;
- }
- }
- class DLinkedList
- {
- Node first, last;
- public void insertFirst(int d)
- {
- Node temp = new Node(d);
- if (isEmpty() == false)
- {
- first.prev = temp;
- }
- temp.next = first;
- first = temp;
- if (last == null)
- {
- last = first;
- }
- return;
- }
- public void insertLast(int d)
- {
- if (isEmpty())
- {
- insertFirst(d);
- return;
- }
- Node temp = new Node(d);
- last.next = temp;
- temp.prev = last;
- last = temp;
- return;
- }
- public Node deleteFirst()
- {
- if (isEmpty())
- {
- System.out.println("Can't delete, The Linked List is Empty");
- return null;
- }
- Node temp = first;
- if (first == last)
- {
- first = last = null;
- }
- else
- {
- first.next.prev = null;
- first = first.next;
- }
- return temp;
- }
- public Node deleteLast()
- {
- if (isEmpty() || first == last)
- {
- return deleteFirst();
- }
- Node temp = last;
- last.prev.next = null;
- last = last.prev;
- return temp;
- }
- public Node deleteElementNumK(int k)
- {
- if (isEmpty())
- {
- System.out.println("Can't delete, The Linked List is Empty");
- return null;
- }
- if (k <= 0)
- {
- System.out.println("Can't delete, The Linked List is 1 indexed");
- return null;
- }
- Node cur = first;
- for (int i = 1; i < k && cur != null; i++)
- {
- cur = cur.next;
- }
- if (cur == null)
- {
- System.out.println("Can't delete, The Size of The Linked List less than " + k);
- return null;
- }
- if (cur.prev == null)first = cur.next;
- else cur.prev.next = cur.next;
- if (cur.next == null)last = cur.prev;
- else cur.next.prev = cur.prev;
- return cur;
- }
- public void reverse()
- {
- Node cur = first;
- while (cur != null)
- {
- Node temp = cur.next;
- cur.next = cur.prev;
- cur.prev = temp;
- cur = cur.prev;
- }
- Node temp = first;
- first = last;
- last = temp;
- System.out.println("Done");
- return;
- }
- public void swap(int x, int y)
- {
- if (x <= 0 || y <= 0)
- {
- System.out.println("Can't swap, The Linked List is 1 indexed");
- return;
- }
- if (y < x)
- {
- int temp = x;
- x = y;
- y = temp;
- }
- Node n1 = null, n2 = null, cur = first;
- for (int i = 1; i < y && cur != null; i++)
- {
- if (i == x)n1 = cur;
- cur = cur.next;
- }
- if (cur == null)
- {
- System.out.println("Can't swap, The size of the Linked List is less than " + y);
- return;
- }
- if (x == y)return;
- n2 = cur;
- if (n1 == first)first = n2;
- else n1.prev.next = n2;
- if (n2 == last)last = n1;
- else n2.next.prev = n1;
- n1.next.prev = n2;
- if (y - x > 1)n2.prev.next = n1;
- else n1.next = n1;
- Node temp = n1.next;
- n1.next = n2.next;
- n2.next = temp;
- temp = n1.prev;
- n1.prev = n2.prev;
- n2.prev = temp;
- return;
- }
- public void display()
- {
- if (isEmpty())
- {
- System.out.print("The Linked List is Empty");
- }
- Node cur = first;
- while (cur != null)
- {
- System.out.print(cur.data + " ");
- cur = cur.next;
- }
- System.out.println();
- }
- public void rotateLeft()
- {
- if (isEmpty())
- {
- System.out.print("The Linked List is Empty");
- return;
- }
- if (first == last)return;
- Node temp = first;
- first = first.next;
- first.prev = null;
- last.next = temp;
- temp.prev = last;
- last = temp;
- temp.next = null;
- return;
- }
- public boolean isEmpty()
- {
- return first == null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement