public static void search(int data){
Node temp=head;
while(temp.next!=null){
if(temp.data==data){
System.out.println("Element found");
return;
}
temp=temp.next;
}
System.out.println("Element not found");
return;
}
public static void deleteAtfirst(){
if(head==null){
System.out.println("No elements found");
return;
}
head=head.next;
System.out.println("First element deleted");
return;
}
public static void deleteAtLast(){
if(head==null){
System.out.println("No elements found");
return;
}
Node temp=head;
while(temp.next.next!=null){
temp=temp.next;
}
temp.next=null;
System.out.println("Last element deleted");
return;
}
public static void deleteRandom(int data){
if(head==null){
System.out.println("No elements found");
return;
}
Node temp=head;
while(temp.next.data!=data){
temp=temp.next;
}
temp.next=temp.next.next;
}
public class Main
{
static class Node{
int data;
Node next;
Node(int data){
this.data=data;
}
};
static Node head=null;
public static void append(int data){
Node nn=new Node(data);
if(head==null){
head=nn;
return;
}
Node temp=head;
while(temp.next!=null){
temp=temp.next;
}
temp.next=nn;
return;
}
public static void insert(int data){
Node nn=new Node(data);
if(head==null){
head=nn;
return;
}
nn.next=head;
head=nn;
return;
}
public static void search(int data){
Node temp=head;
while(temp.next!=null){
if(temp.data==data){
System.out.println("Element found");
return;
}
temp=temp.next;
}
System.out.println("Element not found");
return;
}
public static void deleteAtfirst(){
if(head==null){
System.out.println("No elements found");
return;
}
head=head.next;
System.out.println("First element deleted");
return;
}
public static void deleteAtLast(){
if(head==null){
System.out.println("No elements found");
return;
}
Node temp=head;
while(temp.next.next!=null){
temp=temp.next;
}
temp.next=null;
System.out.println("Last element deleted");
return;
}
public static void deleteRandom(int data){
if(head==null){
System.out.println("No elements found");
return;
}
Node temp=head;
while(temp.next.data!=data){
temp=temp.next;
}
temp.next=temp.next.next;
}
public static void main(String[] args) {
int arr[]={1,2,3,4,5};
for(int i=0;i<5;i++){
// append(arr[i]);
insert(arr[i]);
}
search(3);
deleteAtfirst();
deleteAtLast();
deleteRandom(7);
Node temp=head;
while(temp!=null){
System.out.print(temp.data+" ");
temp=temp.next;
}
}
}