import java.io.*;
class Node
{
private Node next;
private int data;
public Node(int data)
{
this.data=data;
next=null;
}
public void setNext(Node next)
{
this.next=next;
}
public Node getNext()
{
return next;
}
public void setData(int data)
{
this.data=data;
}
public int getData()
{
return data;
}
}
class LinkList
{
private Node head;
public LinkList( )
{
head=null;
}
public void insertAppend(int data)
{
Node newNode=new Node(data);
if(head==null)
{
head=newNode;
}
else
{
Node trav=head;
while(trav.getNext() !=null)
{
trav=trav.getNext();
}
trav.setNext(newNode);
}
}
public int count()
{ int i=0;
Node trav=head;
while(trav != null)
{
trav=trav.getNext();
i++;
}
return i;
}
public void insertPosition(int data,int pos)
{
Node newNode=new Node(data);
int c=count();
if(pos > c+1)
System.out.println("empty");
else if(pos==1)
{
Node t=head;
head=newNode;
head.setNext(t);
}
else
{
Node prev=null;
int i=1,flag=0;
Node trav=head;
while(i<pos)
{
prev=trav;
trav=trav.getNext();
i++;
}
newNode.setNext(trav);
prev.setNext(newNode);
}
}
public void deleteLast()
{
if(head==null)
System.out.println("Empty ");
else if( head.getNext() ==null)
head=null;
else
{
Node trav=head;
while(trav.getNext().getNext() !=null)
trav=trav.getNext();
trav.setNext(null);
}
}
public void deletePosition(int pos)
{
int c=count();
if(pos > c)
{
System.out.println("not possible");
return ;
}
else if(head==null)
System.out.println("empty");
else if(pos==1)
{
Node t=head.getNext();
head=t;
}
else
{
int i=1,flag=0;
Node trav=head;
Node prev=null;
while(i<pos)
{
prev=trav;
trav=trav.getNext();
i++;
}
prev.setNext(trav.getNext());
}
}
public void display()
{
System.out.println("LinkList is: ");
Node temp=head;
if(head==null)
System.out.println("Empty ");
else
{ while(temp.getNext() !=null)
{
System.out.print(temp.getData() +"-->");
temp=temp.getNext();
}
System.out.println(temp.getData());
}
}
}
class LinkMain
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader
(System.in));
LinkList l1=new LinkList();
int d,p;
int c;
do
{
System.out.println("Menu");
System.out.println("1.insert append ");
System.out.println("2.Delete Last");
System.out.println("3.Display ");
System.out.println("4.insert By position ");
System.out.println("5.Delete By position ");
System.out.println("6.Exit ");
System.out.println("enter your choice:");
c=Integer.parseInt(br.readLine());
switch(c)
{
case 1:
System.out.println("enter your
data:");
d=Integer.parseInt(br.readLine());
l1.insertAppend(d);
break;
case 2: l1.deleteLast();
break;
case 3: l1.display();
break;
case 4: System.out.println("enter your
data:");
d=Integer.parseInt(br.readLine());
System.out.println("enter your
position:");
p=Integer.parseInt(br.readLine());
l1.insertPosition(d,p);
break;
case 5: System.out.println("enter
your position to delete:");
p=Integer.parseInt(br.readLine());
l1.deletePosition(p);
break;
case 6: break;
}
}while(c !=6);
}
}
class Node
{
private Node next;
private int data;
public Node(int data)
{
this.data=data;
next=null;
}
public void setNext(Node next)
{
this.next=next;
}
public Node getNext()
{
return next;
}
public void setData(int data)
{
this.data=data;
}
public int getData()
{
return data;
}
}
class LinkList
{
private Node head;
public LinkList( )
{
head=null;
}
public void insertAppend(int data)
{
Node newNode=new Node(data);
if(head==null)
{
head=newNode;
}
else
{
Node trav=head;
while(trav.getNext() !=null)
{
trav=trav.getNext();
}
trav.setNext(newNode);
}
}
public int count()
{ int i=0;
Node trav=head;
while(trav != null)
{
trav=trav.getNext();
i++;
}
return i;
}
public void insertPosition(int data,int pos)
{
Node newNode=new Node(data);
int c=count();
if(pos > c+1)
System.out.println("empty");
else if(pos==1)
{
Node t=head;
head=newNode;
head.setNext(t);
}
else
{
Node prev=null;
int i=1,flag=0;
Node trav=head;
while(i<pos)
{
prev=trav;
trav=trav.getNext();
i++;
}
newNode.setNext(trav);
prev.setNext(newNode);
}
}
public void deleteLast()
{
if(head==null)
System.out.println("Empty ");
else if( head.getNext() ==null)
head=null;
else
{
Node trav=head;
while(trav.getNext().getNext() !=null)
trav=trav.getNext();
trav.setNext(null);
}
}
public void deletePosition(int pos)
{
int c=count();
if(pos > c)
{
System.out.println("not possible");
return ;
}
else if(head==null)
System.out.println("empty");
else if(pos==1)
{
Node t=head.getNext();
head=t;
}
else
{
int i=1,flag=0;
Node trav=head;
Node prev=null;
while(i<pos)
{
prev=trav;
trav=trav.getNext();
i++;
}
prev.setNext(trav.getNext());
}
}
public void display()
{
System.out.println("LinkList is: ");
Node temp=head;
if(head==null)
System.out.println("Empty ");
else
{ while(temp.getNext() !=null)
{
System.out.print(temp.getData() +"-->");
temp=temp.getNext();
}
System.out.println(temp.getData());
}
}
}
class LinkMain
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader
(System.in));
LinkList l1=new LinkList();
int d,p;
int c;
do
{
System.out.println("Menu");
System.out.println("1.insert append ");
System.out.println("2.Delete Last");
System.out.println("3.Display ");
System.out.println("4.insert By position ");
System.out.println("5.Delete By position ");
System.out.println("6.Exit ");
System.out.println("enter your choice:");
c=Integer.parseInt(br.readLine());
switch(c)
{
case 1:
System.out.println("enter your
data:");
d=Integer.parseInt(br.readLine());
l1.insertAppend(d);
break;
case 2: l1.deleteLast();
break;
case 3: l1.display();
break;
case 4: System.out.println("enter your
data:");
d=Integer.parseInt(br.readLine());
System.out.println("enter your
position:");
p=Integer.parseInt(br.readLine());
l1.insertPosition(d,p);
break;
case 5: System.out.println("enter
your position to delete:");
p=Integer.parseInt(br.readLine());
l1.deletePosition(p);
break;
case 6: break;
}
}while(c !=6);
}
}
No comments:
Post a Comment