You are on page 1of 7

import java.util.

*;

public class Main

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

System.out.print("1.Insert at front 2.Insert at end 3.Insert


at middle 4.Delete from front 5.Delete from end 6.Delete at middle");

LL l=new LL();

System.out.print("\nEnter your choice=");

int ch=sc.nextInt();

while(ch!=7)

if(ch==1)

System.out.print("\nEnter data to be added first=");

int data=sc.nextInt();

l.insertfront(data);

l.display();

else if(ch==2)

{
System.out.print("\nEnter data to be added at end=");

int data=sc.nextInt();

l.insertatend(data);

l.display();

else if(ch==3)

System.out.print("\nEnter data and position where


data is to be added");

int data=sc.nextInt();

int pos=sc.nextInt();

l.insertatmiddle(data,pos);

l.display();

else if(ch==4)

l.deletefront();

l.display();

else if(ch==5)

l.deleteend();

l.display();

else
{

System.out.print("\nEnter position where data is to be


deleted");

int pos=sc.nextInt();

l.deleteatmiddle(pos);

l.display();

System.out.print("Next choice=");

ch=sc.nextInt();

l.reverse();

l.display();

class LL

Node head;

class Node

int data;

Node next;

Node(int data)
{

this.data=data;

next=null;

void insertfront(int data)

Node nn=new Node(data);

nn.next=head;

head=nn;

void insertatend(int data)

Node nn=new Node(data);

Node temp=head;

while(temp.next!=null)

temp=temp.next;

temp.next=nn;

nn.next=null;

void insertatmiddle(int data,int pos)

{
Node nn=new Node(data);

Node temp=head;

Node prev=null;

for(int i=0;i<=pos-1;i++)

prev=temp;

temp=temp.next;

prev.next=nn;

nn.next=temp;

void deletefront()

head=head.next;

void deleteend()

Node temp=head;

while(temp.next.next!=null)

temp=temp.next;

temp.next=null;

void deleteatmiddle(int pos)


{

Node temp=head;

Node prev=null;

for(int i=0;i<=pos-1;i++)

prev=temp;

temp=temp.next;

prev.next=temp.next;

void display()

Node temp=head;

while(temp!=null)

System.out.print(temp.data+"->");

temp=temp.next;

System.out.print("null\n");

void reverse()

Node prev=head;

Node curr=head.next;
Node n=head.next.next;

while(n!=null)

curr.next=prev;

prev=curr;

curr=n;

n=n.next;

head.next=null;

curr.next=prev;

head=curr;

You might also like