umar single linked list
public class SLL {
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public Node head = null;
public Node tail = null;
public void insert(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
}
public void insertAtBeginning(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
newNode.next = head;
head = newNode;
}
}
public void insertAtLast(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
}
public void insertAfter(Node prev_node, int data) {
if (prev_node == null) {
System.out.println("The given previous node cannot be null");
return;
}
Node newNode = new Node(data);
newNode.next = prev_node.next;
prev_node.next = newNode;
if (newNode.next == null) {
tail = newNode;
}
}
public void deleteBeginning() {
if (head == null) {
System.out.println("The list is empty");
return;
} else {
if (head != tail) {
head = head.next;
} else {
head = tail = null;
}
}
}
public void deleteEnd() {
if (head == null) {
System.out.println("The list is empty");
} else {
if (head != tail) {
Node current = head;
while (current.next != tail) {
current = current.next;
}
tail = current;
tail.next = null;
} else {
head = tail = null;
}
}
}
void deleteAtPosition(int position) {
if (head == null) {
System.out.println("The list is empty");
return;
}
Node temp = head;
if (position == 0) {
head = temp.next;
if (head == null) {
tail = null;
}
return;
}
for (int i = 0; temp != null && i < position - 1; i++) {
temp = temp.next;
}
if (temp == null || temp.next == null) {
System.out.println("Given position in the list is not available");
return;
}
Node next = temp.next.next;
if (temp.next == tail) {
tail = temp;
}
temp.next = next;
}
public void searchNode(int data) {
Node current = head;
int i = 1;
boolean flag = false;
if (head == null) {
System.out.println("The list is empty");
} else {
while (current != null) {
if (current.data == data) {
flag = true;
break;
}
i++;
current = current.next;
}
}
if (flag) {
System.out.println("Element is present at position " + i);
} else {
System.out.println("Element is not present in the list");
}
}
public Node NodeReverse(Node head) {
if (head == null) {
System.out.println("The list is empty");
return null;
}
Node prev = null;
Node current = head;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
public void reverseList() {
head = NodeReverse(head);
}
public void display() {
Node current = head;
if (head == null) {
System.out.println("The list is empty");
return;
}
System.out.println("The data in the list are:");
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public static void main(String[] args) {
SLL newList = new SLL();
newList.insert(5);
newList.deleteEnd();
newList.deleteBeginning();
newList.deleteAtPosition(4);
newList.insert(6);
newList.insert(8);
newList.insert(3);
newList.reverseList();
newList.searchNode(3);
newList.insertAtBeginning(5);
newList.insertAtBeginning(6);
newList.insertAtLast(9);
newList.insertAtLast(10);
Node secondNode = newList.head.next;
newList.insertAfter(secondNode, 7);
newList.display();
}
}
Comments
Post a Comment