SINGLE LINKED LIST

import java.io.*; class node {    int n;    node next; } class SingleLinkedList {  public static void main(String args[])t...

import java.io.*;
class node
{
   int n;
   node next;
}
class SingleLinkedList
{
 public static void main(String args[])throws IOException
 {
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   node p,q=null,r=null, head = null;
   int ch=0;
  do
 {
  System.out.println("1:CREATION /N 2:INSERTION /N 3:DELETION /N 4:TRAVERSAL /N       5:SEARCHING");
  System.out.println("ENTER YOUR CHOICE");
  ch=Integer.parseInt(br.readLine());
  switch(ch)
 {
  case 1:
   System.out.println("ENTER HOW MANY NODES DO YOU WANT");
   int no=Integer.parseInt(br.readLine());
   while(no>=1)
   {
     p=new node();
   System.out.println("ENTER DATA");                                                                                p.n=Integer.parseInt(br.readLine());
  p.next=null;
  if(head==null)
  {
    head=p;
    q=p;
  }
 else
 {          
  q.next=p;
  q=p;
 }
no--;
 }
break;
case 2:
System.out.println( "1:INSERT A NODE AT BEGINNIN/N 2:INSERT A NODE AT ENDING /N 3:INSERT A NODE AT USER CHOICE" );
System.out.println("ENTER YOUR CHOICE");
int op=Integer.parseInt(br.readLine());
p=new node();
System.out.println("Enter data");
p.n=Integer.parseInt(br.readLine());
p.next=null;
switch(op)
{
 case 1:
 p.next=head;
head=p;
break ;

case 2:
q=head;
while(q.next != null)
 {
    q=q.next;
 }
q.next=p;
break;

case 3:
System.out.println("ENTER POSITION DO YOU WANT DO INSERT");
int pos=Integer.parseInt(br.readLine());
q=head;
r=head.next;
while(pos- 1> 1)
{
  q=q.next;
  r=r.next;
   pos--;
 }
 q.next=p;
 r.next=r;
 break;
 }
 break;

case 3:
System.out.println("1:DELETING A NODE AT BEGINNING/N 2:DELETING A NODE AT ENDING /N 3:DELETING A NODE AT USER CHOICE");
System.out.println("ENTER YOUR CHOCE");
op=Integer.parseInt(br.readLine());
switch(op)
{
   case 1 :
   head=head.next;
   break;

  case 2:
   q=head;
   r=head.next;
   while(r.next != null)
   {
      q=q.next;
      r=r.next;
   }
   q.next=null;
   break ;

 case 3:
  System.out.println("ENTER POSITION");
  int pos=Integer.parseInt(br.readLine());
  q=head;
  r=head.next;
  while(pos-1 > 1)
  {
    q=q.next;
     r=r.next;
     pos--;
   }          
   q.next=r.next;
   break;
   }
   break;

   case 4:
    System.out.println("THE LIST IS");
    p=head;
    while(p != null)
   {
   System.out.println("DATA ="+p.n+"\tADDRESS = "+p+"\tNEXT ELEMENT ADDRESS =         "+p.next);
   p=p.next;
   }
  break;

 case 5:
System.out.println("ENTER ELEMENT TO SEARTCH");
int ele =Integer.parseInt(br.readLine());
p=head;
int code =0;
while(p!=null)
{
   if(ele==p.n)
   {
     code =1;
     break;
    }
  p=p.next;
}
if(code==1)
{
System.out.println("ELEMENT FOUND");
 }
else
{
System.out.println("ELEMENT  NOT FOUND");
 }
break;
 }
}while(ch<=5);
}
}
  

Output:

1:CREATION /N 2:INSERTION /N 3:DELETION /N 4:TRAVERSAL /N 5:SEARCHING
ENTER YOUR CHOICE
1
ENTER HOW MANY NODES DO YOU WANT
5
ENTER DATA  1
ENTER DATA  2
ENTER DATA  3
ENTER DATA  4
ENTER DATA  5
1:CREATION /N 2:INSERTION /N 3:DELETION /N 4:TRAVERSAL /N 5:SEARCHING
ENTER YOUR CHOICE
4

THE LIST IS

DATA =1 ADDRESS = node@9304b1   NEXT ELEMENT ADDRESS = node@190d11

DATA =2 ADDRESS = node@190d11   NEXT ELEMENT ADDRESS = node@a90653

DATA =3 ADDRESS = node@a90653   NEXT ELEMENT ADDRESS = node@de6ced

DATA =4 ADDRESS = node@de6ced   NEXT ELEMENT ADDRESS = node@c17164

DATA =5 ADDRESS = node@c17164   NEXT ELEMENT ADDRESS = null

1:CREATION /N 2:INSERTION /N 3:DELETION /N 4:TRAVERSAL /N 5:SEARCHING
ENTER YOUR CHOICE  4


Related

Data Structures 4492948214997496350

Post a Comment

emo-but-icon

item