import java.io.*;
class Node
{
private Node next;
private int data;
Node(int data)
{
this.data=data;
}
public Node getNext()
{
return next;
}
public void setNext(Node next)
{
this.next=next;
}
public int getData()
{
return data;
}
public void setData(int data)
{
this.data=data;
}
}
class LinkList
{
Node head=null;
public void addValue(int data,int pdata)
{
Node newNode=new Node(data);
if(head == null)
{
head=newNode;
}
else if(head.getData() == pdata && head.getNext() == null)
{
head.setNext(newNode);
}
else
{
Node trav=head;
//Node prev=null;
while(trav != null && trav.getData() != pdata)
{
//prev=trav;
trav=trav.getNext();
}
if(trav == null)
{
System.out.println("Give Value not found");
}
else
{
Node temp= trav.getNext();
newNode.setNext(temp);
trav.setNext(newNode);
}
}
}
public void deleteValue(int data)
{
if(head==null)
{
System.out.println("LInk LIst is empty");
}
else if(head.getData() == data)
{
head=head.getNext();
}
else
{
Node trav=head;
Node prev=null;
while(trav != null && trav.getData() != data)
{
prev=trav;
trav=trav.getNext();
}
if(trav == null)
System.out.println("Give Value not found");
else
{
prev.setNext(trav.getNext());
}
}
}
public void display()
{
if(head==null)
{
System.out.println("LInk LIst is empty");
}
else
{
Node trav=head;
while(trav.getNext() !=null )
{
System.out.print(trav.getData()+"->");
trav=trav.getNext();
}
System.out.println(trav.getData());
}
}
}
class PracQuiz
{
public static void main(String args[]) throws IOException
{
LinkList ls=new LinkList();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch,d,p;
do
{
System.out.println("Menu----");
System.out.println("1.Insert after value");
System.out.println("2.Delete By value");
System.out.println("3.Display");
System.out.println("4.Exit");
System.out.println("ENter Your Choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Insert data after you want your New Node");
d=Integer.parseInt(br.readLine());
System.out.println("Insert value after you want to enter new Node");
p=Integer.parseInt(br.readLine());
ls.addValue( d, p);
break ;
case 2:
System.out.println("Insert data of node that you want to delete");
d=Integer.parseInt(br.readLine());
ls.deleteValue(d);
break;
case 3:
System.out.println("Your Link list is:");
ls.display();
break;
case 4:
break;
}
}while(ch!=4);
}
}
{
private int data;
Node(int data)
{
this.data=data;
}
{
return next;
}
public void setNext(Node next)
{
this.next=next;
}
public int getData()
{
return data;
}
public void setData(int data)
{
this.data=data;
}
}
class LinkList
{
Node head=null;
public void addValue(int data,int pdata)
{
Node newNode=new Node(data);
if(head == null)
{
head=newNode;
}
else if(head.getData() == pdata && head.getNext() == null)
{
head.setNext(newNode);
}
else
{
Node trav=head;
//Node prev=null;
while(trav != null && trav.getData() != pdata)
{
//prev=trav;
trav=trav.getNext();
}
if(trav == null)
{
System.out.println("Give Value not found");
}
else
{
Node temp= trav.getNext();
newNode.setNext(temp);
trav.setNext(newNode);
}
}
}
public void deleteValue(int data)
{
if(head==null)
{
System.out.println("LInk LIst is empty");
}
else if(head.getData() == data)
{
head=head.getNext();
}
else
{
Node trav=head;
Node prev=null;
while(trav != null && trav.getData() != data)
{
prev=trav;
trav=trav.getNext();
}
if(trav == null)
System.out.println("Give Value not found");
else
{
prev.setNext(trav.getNext());
}
}
}
public void display()
{
if(head==null)
{
System.out.println("LInk LIst is empty");
}
else
{
Node trav=head;
while(trav.getNext() !=null )
{
System.out.print(trav.getData()+"->");
trav=trav.getNext();
}
System.out.println(trav.getData());
}
}
}
class PracQuiz
{
public static void main(String args[]) throws IOException
{
LinkList ls=new LinkList();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch,d,p;
do
{
System.out.println("Menu----");
System.out.println("1.Insert after value");
System.out.println("2.Delete By value");
System.out.println("3.Display");
System.out.println("4.Exit");
System.out.println("ENter Your Choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Insert data after you want your New Node");
d=Integer.parseInt(br.readLine());
System.out.println("Insert value after you want to enter new Node");
p=Integer.parseInt(br.readLine());
ls.addValue( d, p);
case 2:
System.out.println("Insert data of node that you want to delete");
d=Integer.parseInt(br.readLine());
ls.deleteValue(d);
break;
case 3:
System.out.println("Your Link list is:");
ls.display();
break;
case 4:
break;
}
}while(ch!=4);
}
}
No comments:
Post a Comment