import java.util.Scanner;
public class SLL {
class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}}
public Node head = null;
public void addnodeatpositon(int data,int n) {
Node newNode = new Node(data);
int i=1;
newNode.data=data;
Node current=head;
while(i<(n-1)) {
current=current.next;
i++;
}
newNode.next=(current.next);
current.next=newNode;
}
public void deleteatposition(int n) {
int i=1;
Node current=head;
while(i<(n-1)) {
current=current.next;
i++;
}
current.next=(current.next).next;
}
public void addNode(int data) {
Node newNode = new Node(data);
if(head == null) {
head = newNode;
}
else {
newNode.next=head;
head=newNode;
}
}
public void delete(){
if(head == null) {
System.out.println("List is Empty");
}else {
head=head.next;
}
}
public void displayreverseorder() {
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Nodes of singly linked list: ");
while(current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public void search(int key) {
Node current=head;
while (current.next!=null) {
if(current.data==key)
{ System.out.println("Item is found");
break;
} current= current.next;
if(current.next==null)
System.out.println("Item is not found");
}
}
public static void main(String[] args) {
SLL list=new SLL();
list.addNode(7);
list.addNode(6);
list.addNode(5);
list.addNode(3);
list.addNode(2);
list.addNode(1);
list.displayreverseorder();
list.addnodeatpositon(8,4);
list.displayreverseorder();
list.deleteatposition(4);
list.displayreverseorder();
}
} //This code is contributed by Pronay(Bangladesh)