You are on page 1of 6

import java.util.

*;

public class Node


{
int rno;
String name;
Node next;

Node()
{
rno=0;
name="";
next=null;
}

void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println(" enter roll no ");
rno=sc.nextInt();
System.out.println(" Enter name ");
name=sc.next();
}

Node addtop(Node first)


{

Node temp=new Node();


temp.accept();
temp.next=first;
return temp;
}
void addbottom(Node first)
{
while(first!=null)
{
if(first.next==null)
break;
else
first=first.next;
}
Node temp=new Node();
temp.accept();
first.next=temp;
}

void deletebottom(Node first)


{
Node temp=new Node();
while(first!=null)
{
temp=first;
first=first.next;

}
first=temp;
first.next=null;
}

void display()
{ System.out.println(rno+ " "+ name);
}

Node createlinkedlist(int n)
{
Node first= new Node();
first.accept();
Node top=first;
for(int i=1;i<n;i++)
{
Node temp=new Node();
temp.accept();
first.next=temp;
first=temp;
}
return top;
}

void displaylist(Node top)


{

Node first=top;
int c=0;
while(first!=null)
{ c++;

first.display();
first=first.next;
}
}

Node deletetop(Node top)


{
return top.next;
}

void main(int size)


{
int ch=0;
Scanner sc=new Scanner(System.in);
Node top=new Node();
top=createlinkedlist(size);
while(ch!=6)
{
System.out.println();
System.out.println("1. add node at top");
System.out.println("2. display");

System.out.println("3.delete node from top ");


System.out.println("4. add node at the bottom");
System.out.println("5. delete node at the bottom");
System.out.println("6. exit");
System.out.println("enter choice");
ch=sc.nextInt();
switch(ch)
{
case 3:
top=deletetop(top);
break;

case 2:
displaylist(top);
break;

case 1:

top=addtop(top);
break;

case 4:
addbottom(top);
// displaylist(top);
break;

case 5:

deletebottom(top);

case 6:
System.out.println(" exit");
}
}
}
}

You might also like