// Single Linked List create,delete, display Operations
import java.util.*;
class lists
{
int info;
lists next;
}
class rklklist
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
slist sl=new slist();
int ch,n,i,x,y;
while(true)
{
System.out.printf("\n **** MENU ****\n");
System.out.println("1.Creation");
System.out.println("2.Deletion");
System.out.println("3.Display");
System.out.println("4.exit");
System.out.println("enter ur choice:");
ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("\n Enter how many nodes:");
n=sc.nextInt();
for(i=0;i<n;i++)
{
System.out.println("\n Enter the element:");
x=sc.nextInt();
sl.create(x);
}
break;
case 2: System.out.println("\nEnter the element:");
y=sc.nextInt();
sl.del(y);
break;
case 3: sl.display();
break;
case 4: System.exit(0);
default:System.out.println("Wrong choice");
}
}
}
}
class slist
{
lists start,q,tmp,head;
void create(int x)
{
tmp=new lists();
tmp.info=x;
tmp.next=null;
if(start==null)
{
start=tmp;
System.out.printf("\n start.next=%h tmp.info=%d",start,tmp.info);
}
else
{
q=start;
System.out.printf("\n q.next=%h q.info=%d",q,q.info);
while(q.next!=null)
{
q=q.next;
System.out.printf("\n q.next=%h q.info=%d",q,q.info);
}
q.next=tmp;
System.out.printf("\n q.next=%h q.info=%d",tmp,tmp.info);
return;
}
}
void del(int y)
{
if(start.info==y)
{
tmp=start;
start=start.next;
}
q=start;
while(q.next!= null)
{
if(q.next.info==y)
{
tmp=q.next;
q.next=tmp.next;
return;
}
q=q.next;
}
if(q.next.info==y)
{
tmp=q.next;
q.next=null;
return;
}
System.out.printf("\n Element %d not found ",y);
}
void display()
{
if(start== null)
System.out.printf("list is empty");
q=start;
System.out.printf("\n\t list is :");
System.out.printf("\n %h --> %d ",start,start.info);
while(q.next != null)
{
q=q.next;
System.out.printf("%h-->%d || ",q,q.info);
}
System.out.printf("%h",q.next);
return;
}
}
output:
import java.util.*;
class lists
{
int info;
lists next;
}
class rklklist
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
slist sl=new slist();
int ch,n,i,x,y;
while(true)
{
System.out.printf("\n **** MENU ****\n");
System.out.println("1.Creation");
System.out.println("2.Deletion");
System.out.println("3.Display");
System.out.println("4.exit");
System.out.println("enter ur choice:");
ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("\n Enter how many nodes:");
n=sc.nextInt();
for(i=0;i<n;i++)
{
System.out.println("\n Enter the element:");
x=sc.nextInt();
sl.create(x);
}
break;
case 2: System.out.println("\nEnter the element:");
y=sc.nextInt();
sl.del(y);
break;
case 3: sl.display();
break;
case 4: System.exit(0);
default:System.out.println("Wrong choice");
}
}
}
}
class slist
{
lists start,q,tmp,head;
void create(int x)
{
tmp=new lists();
tmp.info=x;
tmp.next=null;
if(start==null)
{
start=tmp;
System.out.printf("\n start.next=%h tmp.info=%d",start,tmp.info);
}
else
{
q=start;
System.out.printf("\n q.next=%h q.info=%d",q,q.info);
while(q.next!=null)
{
q=q.next;
System.out.printf("\n q.next=%h q.info=%d",q,q.info);
}
q.next=tmp;
System.out.printf("\n q.next=%h q.info=%d",tmp,tmp.info);
return;
}
}
void del(int y)
{
if(start.info==y)
{
tmp=start;
start=start.next;
}
q=start;
while(q.next!= null)
{
if(q.next.info==y)
{
tmp=q.next;
q.next=tmp.next;
return;
}
q=q.next;
}
if(q.next.info==y)
{
tmp=q.next;
q.next=null;
return;
}
System.out.printf("\n Element %d not found ",y);
}
void display()
{
if(start== null)
System.out.printf("list is empty");
q=start;
System.out.printf("\n\t list is :");
System.out.printf("\n %h --> %d ",start,start.info);
while(q.next != null)
{
q=q.next;
System.out.printf("%h-->%d || ",q,q.info);
}
System.out.printf("%h",q.next);
return;
}
}
output:
No comments:
Post a Comment