|
| 1 | +public class SortDLL { |
| 2 | + |
| 3 | + //Represent a node of the doubly linked list |
| 4 | + |
| 5 | + class Node{ |
| 6 | + int data; |
| 7 | + Node previous; |
| 8 | + Node next; |
| 9 | + |
| 10 | + public Node(int data) { |
| 11 | + this.data = data; |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + //Represent the head and tail of the doubly linked list |
| 16 | + Node head, tail = null; |
| 17 | + |
| 18 | + //addNode() will add a node to the list |
| 19 | + public void addNode(int data) { |
| 20 | + //Create a new node |
| 21 | + Node newNode = new Node(data); |
| 22 | + |
| 23 | + //If list is empty |
| 24 | + if(head == null) { |
| 25 | + //Both head and tail will point to newNode |
| 26 | + head = tail = newNode; |
| 27 | + //head's previous will point to null |
| 28 | + head.previous = null; |
| 29 | + //tail's next will point to null, as it is the last node of the list |
| 30 | + tail.next = null; |
| 31 | + } |
| 32 | + else { |
| 33 | + //newNode will be added after tail such that tail's next will point to newNode |
| 34 | + tail.next = newNode; |
| 35 | + //newNode's previous will point to tail |
| 36 | + newNode.previous = tail; |
| 37 | + //newNode will become new tail |
| 38 | + tail = newNode; |
| 39 | + //As it is last node, tail's next will point to null |
| 40 | + tail.next = null; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + //sortList() will sort the given list in ascending order |
| 45 | + public void sortList() { |
| 46 | + Node current = null, index = null; |
| 47 | + int temp; |
| 48 | + //Check whether list is empty |
| 49 | + if(head == null) { |
| 50 | + return; |
| 51 | + } |
| 52 | + else { |
| 53 | + //Current will point to head |
| 54 | + for(current = head; current.next != null; current = current.next) { |
| 55 | + //Index will point to node next to current |
| 56 | + for(index = current.next; index != null; index = index.next) { |
| 57 | + //If current's data is greater than index's data, swap the data of current and index |
| 58 | + if(current.data > index.data) { |
| 59 | + temp = current.data; |
| 60 | + current.data = index.data; |
| 61 | + index.data = temp; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + //display() will print out the nodes of the list |
| 69 | + public void display() { |
| 70 | + //Node current will point to head |
| 71 | + Node current = head; |
| 72 | + if(head == null) { |
| 73 | + System.out.println("List is empty"); |
| 74 | + return; |
| 75 | + } |
| 76 | + while(current != null) { |
| 77 | + //Prints each node by incrementing the pointer. |
| 78 | + |
| 79 | + System.out.print(current.data + " "); |
| 80 | + current = current.next; |
| 81 | + } |
| 82 | + System.out.println(); |
| 83 | + } |
| 84 | + |
| 85 | + public static void main(String[] args) { |
| 86 | + |
| 87 | + SortList dList = new SortList(); |
| 88 | + //Add nodes to the list |
| 89 | + dList.addNode(7); |
| 90 | + dList.addNode(1); |
| 91 | + dList.addNode(4); |
| 92 | + dList.addNode(5); |
| 93 | + dList.addNode(2); |
| 94 | + |
| 95 | + //Displaying original list |
| 96 | + System.out.println("Original list: "); |
| 97 | + dList.display(); |
| 98 | + |
| 99 | + //Sorting list |
| 100 | + dList.sortList(); |
| 101 | + |
| 102 | + //Displaying sorted list |
| 103 | + System.out.println("Sorted list: "); |
| 104 | + dList.display(); |
| 105 | + } |
| 106 | +} |
0 commit comments