import java.io.*;
class Node{
Object info;
Node next;
Node(Object data)
{
info=data;
next=null;
}
}
class LinkedList
{
Node start;
LinkedList()
{
start=null;
}
void first(Node nnode)//for first node
{
nnode.next=start;
start=nnode;
}
void mid(Node nnode,int v)//for middle node
{
Node p=start;
for(int i=1;i<(v-1);i++)
{
p=p.next;
}
nnode.next=p.next;
p.next=nnode;
System.out.println("value inserted at "+v+" position.");
}
void end(Node nnode)//for last node
{
Node p=start;
while(p.next!=null)
{
p=p.next;
}
p.next=nnode;
System.out.println("value inserted at end");
}
void display()//dsplay link list
{
Node p=start;
if(p==null)
{
System.out.println("\n The LinkedList is empty.");
}
else
{
System.out.print("your values : ");
while(p!=null)
{
System.out.print(" "+p.info);
p=p.next;
}
System.out.println();
}
}
void search(Object data)throws IOException//search value in link list
{
Node p;
int co=1,f=0;
p=start;
if(p==null)
{
System.out.println(" The LinkedList is empty.");
}
else
{
while((p!=null)&&(f==0))
{
if((p.info).equals(data)==true)
{
f=1;
}
else
{
p=p.next;
co++;
}
}
if((p.info).equals(data)==true)
{
System.out.println(p.info+" found at "+co+" place " );
}
else
{
System.out.println(data+" does not found");
}
}
}
void del(Object data)throws Exception//delete perticular value in link list
{
Node p=start;
Node q=start.next;
int f=0;
if(start==null)
{
System.out.println(" The linked list is empty.");
}
if((p.info).equals(data)==true)
{
System.out.println(data+" deleted" );
start=p.next;
}
else
{
while((f==0)&&(p!=null))
{
if((p.info).equals(data)!=true)
{
q=p;
p=p.next;
}
else
{
f=1;
}
}
}
if(f==1)
{
q.next=p.next;
System.out.println(data+" deleted");
}
else
{
System.out.println("value does not match");
}
}
public static void main(String args[ ])throws IOException
{
LinkedList ob=new LinkedList();
int c,a=1,x;
DataInputStream dis=new DataInputStream(System.in);
while(a==1)
{
System.out.println("\nMain menu");
System.out.println("\n1:insert value");
System.out.println("2:to delete value");
System.out.println("3:display");
System.out.println("4:search value");
System.out.println("5:exit");
System.out.print("\nEnter your choice:");
c=Integer.parseInt(dis.readLine());
switch(c)
{
case 1: System.out.print("\nEnter your value:");
x=Integer.parseInt(dis.readLine());
Node nnode=new Node(new Integer(x));
if(ob.start==null)
{
ob.start=nnode;
}
else
{
System.out.println("\n1:to insert value at first place");
System.out.println("2:midle");
System.out.println("3:to insert value at end");
System.out.print("\nEnter your choice:");
int pos=Integer.parseInt(dis.readLine());
if(pos==1)
{
ob.first(nnode);
}
if(pos==2)
{
System.out.println("Enter position:");
int v=Integer.parseInt(dis.readLine());
ob.mid(nnode,v);
}
if(pos==3)
{
ob.end(nnode);
}
}
break;
case 2: try
{
System.out.print("Enter value to delete:");
x=Integer.parseInt(dis.readLine());
ob.del(new Integer(x));
}
catch(Exception e)
{}
break;
case 3:
System.out.println();
ob.display();
break;
case 4: try
{
System.out.print("Enter value for search:");
x=Integer.parseInt(dis.readLine());
ob.search(new Integer(x));
}
catch(Exception e)
{}
break;
case 5:
System.exit(1);
break;
}
}
}
}
/*OutPut:
E:\java\bin>javac LinkedList.java
E:\java\bin>java LinkedList
Main menu
1:insert value
2:to delete value
3:display
4:search value
5:exit
Enter your choice:1
Enter your value:112
No comments:
Post a Comment