|
8 | 8 | * |
9 | 9 | * @author fawad |
10 | 10 | */ |
11 | | -publicclass SinglyLinkedList { |
| 11 | +class Node { |
12 | 12 |
|
13 | | - Node head; // head of the linked list |
| 13 | + int data; |
| 14 | + Node next; |
14 | 15 |
|
15 | | - // Node class |
16 | | - static class Node { |
| 16 | + public Node(int data) { |
| 17 | + this.data = data; |
| 18 | + } |
| 19 | +} |
17 | 20 |
|
18 | | - int data; |
19 | | - Node next; |
| 21 | +public class SinglyLinkedList { |
20 | 22 |
|
21 | | - Node(int data) { |
22 | | - this.data = data; |
23 | | - next = null; |
24 | | - } |
25 | | - } |
| 23 | + private Node head; |
| 24 | + private int size; |
26 | 25 |
|
27 | | - // Display the linked list |
28 | | - public void display() { |
29 | | - Node temp = head; |
30 | | - while (temp != null) { |
31 | | - System.out.print(temp.data + " "); |
32 | | - temp = temp.next; |
33 | | - } |
34 | | - System.out.println(); |
| 26 | + public SinglyLinkedList() { |
| 27 | + head = null; |
| 28 | + size = 0; |
| 29 | + } |
| 30 | + public int size(){ |
| 31 | + return size; |
| 32 | + } |
| 33 | + public boolean isEmpty() { |
| 34 | + return head == null; |
35 | 35 | } |
36 | 36 |
|
37 | | - // Count the number of nodes in the linked list |
38 | | - public int countNodes() { |
39 | | - int count = 0; |
40 | | - Node temp = head; |
41 | | - while (temp != null) { |
42 | | - count++; |
43 | | - temp = temp.next; |
| 37 | + public void addFirst(int value) { |
| 38 | + Node n = new Node(value); |
| 39 | + if (isEmpty()) { |
| 40 | + head = n; |
| 41 | + size++; |
| 42 | + } else { |
| 43 | + n.next = head; |
| 44 | + head = n; |
| 45 | + size++; |
44 | 46 | } |
45 | | - return count; |
46 | 47 | } |
47 | 48 |
|
48 | | - // Find a node with a given key value |
49 | | - public Node find(int key) { |
50 | | - Node temp = head; |
51 | | - while (temp != null) { |
52 | | - if (temp.data == key) { |
53 | | - return temp; |
| 49 | + public void addAtEnd(int value) { |
| 50 | + Node n = new Node(value); |
| 51 | + if (isEmpty()) { |
| 52 | + head = n; |
| 53 | + size++; |
| 54 | + } else { |
| 55 | + Node p = head; |
| 56 | + while (p.next != null) { |
| 57 | + p = p.next; |
54 | 58 | } |
55 | | - temp = temp.next; |
| 59 | + p.next = n; |
| 60 | + size++; |
56 | 61 | } |
57 | | - return null; |
58 | 62 | } |
59 | 63 |
|
60 | | - // Add a new node with given data at the beginning of the linked list |
61 | | - public void addAtFirst(int data) { |
62 | | - Node newNode = new Node(data); |
63 | | - newNode.next = head; |
64 | | - head = newNode; |
| 64 | + public void addAfter(int key, int value) { |
| 65 | + Node n = new Node(value); |
| 66 | + Node p = find(key); |
| 67 | + if (p == null) { |
| 68 | + System.out.println("Node with key " + key + " not found"); |
| 69 | + return; |
| 70 | + } |
| 71 | + n.next = p.next; |
| 72 | + p.next = n; |
| 73 | + size++; |
| 74 | + |
65 | 75 | } |
66 | 76 |
|
67 | | - // Add a new node with given data at the end of the linked list |
68 | | - public void addAtEnd(int data) { |
69 | | - Node newNode = new Node(data); |
70 | | - if (head == null) { |
71 | | - head = newNode; |
| 77 | + public void delete(int key) { |
| 78 | + Node temp = head; |
| 79 | + Node prev = null; |
| 80 | + |
| 81 | + if (temp != null && temp.data == key) { |
| 82 | + head = temp.next; // Change the head node |
| 83 | + size--; |
72 | 84 | return; |
73 | 85 | } |
74 | | - Nodetemp = head; |
75 | | - while (temp.next != null) { |
| 86 | + while (temp != null && temp.data != key) { |
| 87 | + prev = temp; |
76 | 88 | temp = temp.next; |
| 89 | + |
77 | 90 | } |
78 | | - temp.next = newNode; |
79 | | - } |
80 | 91 |
|
81 | | - // Add a new node with given data after the node with a given key value |
82 | | - public void addAfter(int key, int data) { |
83 | | - Node newNode = new Node(data); |
84 | | - Node temp = find(key); |
85 | 92 | if (temp == null) { |
86 | | - System.out.println("Node with key " + key + " not found"); |
| 93 | + System.out.println("Key is not found."); |
87 | 94 | return; |
88 | 95 | } |
89 | | - newNode.next = temp.next; |
90 | | - temp.next = newNode; |
| 96 | + prev.next = temp.next; |
| 97 | + size--; |
91 | 98 | } |
92 | 99 |
|
93 | | - // Delete the last node in the linked list |
94 | 100 | public void deleteFromEnd() { |
95 | 101 | if (head == null) { |
96 | 102 | System.out.println("Linked list is empty"); |
97 | 103 | return; |
98 | 104 | } |
99 | 105 | if (head.next == null) { |
100 | 106 | head = null; |
| 107 | + size--; |
101 | 108 | return; |
102 | 109 | } |
103 | 110 | Node temp = head; |
104 | 111 | while (temp.next.next != null) { |
105 | 112 | temp = temp.next; |
106 | 113 | } |
107 | 114 | temp.next = null; |
| 115 | + size--; |
108 | 116 | } |
| 117 | + public void display() { |
109 | 118 |
|
110 | | - public void delete(int key) { |
111 | | - Node temp = head; |
112 | | - Node prev = null; |
113 | | - |
114 | | - // If the head node itself contains the key to be deleted |
115 | | - if (temp != null && temp.data == key) { |
116 | | - head = temp.next; // Change the head node |
117 | | - return; |
| 119 | + if (isEmpty()) { |
| 120 | + System.out.println("List is Empty."); |
| 121 | + } else { |
| 122 | + Node n = head; |
| 123 | + while (n != null) { |
| 124 | + System.out.print(n.data + "\t"); |
| 125 | + n = n.next; |
| 126 | + } |
| 127 | + System.out.println(""); |
118 | 128 | } |
| 129 | + } |
119 | 130 |
|
120 | | - // Search for the key to be deleted, keep track of the previous node as well |
121 | | - while (temp != null && temp.data != key) { |
122 | | - prev = temp; |
| 131 | + public Node find(int key) { |
| 132 | + Node temp = head; |
| 133 | + while (temp != null) { |
| 134 | + if (temp.data == key) { |
| 135 | + return temp; |
| 136 | + } |
123 | 137 | temp = temp.next; |
124 | 138 | } |
| 139 | + return null; |
| 140 | + } |
125 | 141 |
|
126 | | - // If key was not present in the linked list |
127 | | - if (temp == null) { |
128 | | - return; |
129 | | - } |
| 142 | + public static void main(String[] args) { |
| 143 | + SinglyLinkedList n = new SinglyLinkedList(); |
| 144 | + n.addFirst(12); |
| 145 | + n.addAtEnd(20); |
| 146 | + n.addAfter(12, 18); |
| 147 | + System.out.println(n.size()); |
| 148 | + n.display(); |
| 149 | + n.delete(18); |
| 150 | + n.display(); |
| 151 | + System.out.println(n.size()); |
130 | 152 |
|
131 | | - // Unlink the node from the linked list |
132 | | - prev.next = temp.next; |
133 | 153 | } |
134 | 154 |
|
135 | 155 | } |
0 commit comments