Program:
import java.io.*;
class Node
{
public int data;
public Node next;
public Node previous;
public Node(int x)
{
data=x;
}
public void display()
{
System.out.print(data+" ");
}
}
class lab
{
private Node first;
private Node last;
public lab()
{
first=null;
last=null;
}
public void insertfirst(int x)
{
Node newnode=new Node(x);
if(isEmpty())
last=newnode;
else
first.previous=newnode;
newnode.next=first;
first=newnode;
}
public void insertlast(int y)
{
Node newnode=new Node(y);
if(isEmpty())
first=newnode;
else
{
last.next=newnode;
newnode.previous=last;
}
last=newnode;
}
public boolean isEmpty()
{
return(first==null&&last==null);
}
public void display()
{
Node current=first;
while(current!=null)
{
System.out.print(" "+current.data);
current=current.next;
}
}
public void delfirst()
{
first=first.next;
first.previous=null;
}
public void dellast()
{
last=last.previous;
last.next=null;
}
public void insertatp(int p,int x2)
{
Node newnode=new Node(x2);
Node current=first;
int count=1;
while(count+1<p)
{
current=current.next;
count++;
if(current.next==null)
{
System.out.print("Error");
break;
}
}
Node temp=current.next;
current.next=newnode;
newnode.next=temp;
}
public void deletatp(int p1)
{
Node current=first;
int count=1;
while(count+1<p1)
{
current=current.next;
count++;
if(current.next==null)
{
System.out.print("Error");
break;
}
}
Node temp=current.next;
current.next=temp.next;
}
}
class doublylinkedlist
{
public static void main (String arg[]) throws IOException
{
lab a1=new lab();
DataInputStream x=new DataInputStream(System.in);
System.out.print("Enter the number of elements:");
int n= Integer.parseInt(x.readLine());
System.out.print("\nInsert at first:\n");
System.out.print("\nEnter elements:\n");
for(int i=0;i<n;i++)
a1.insertfirst(Integer.parseInt(x.readLine()));
a1.display();
System.out.print("\nInsert last:\n");
System.out.print("Enter elements:\n");
for(int i=0;i<n;i++)
a1.insertlast(Integer.parseInt(x.readLine()));
a1.display();
System.out.println("\nDo you want to delete first element:\n1.Yes\t2.No");
switch(Integer.parseInt(x.readLine()))
{
case 1:
a1.delfirst(); break;
case 2:
default: break;
}
a1.display();
System.out.println("\nDo you want to delete the last element:\n1.Yes\t2.No");
switch(Integer.parseInt(x.readLine()))
{
case 1:
a1.dellast(); break;
case 2:
default: break;
}
a1.display();
System.out.print("\nInsert at particular position:\nEnter the position & element:\n");
a1.insertatp(Integer.parseInt(x.readLine()),Integer.parseInt(x.readLine()));
a1.display();
System.out.print("\nDelete from particular position:\nEnter the position:\n");
a1.deletatp(Integer.parseInt(x.readLine()));
a1.display();
}}
Output:
nn@linuxmint ~ $ javac doublylinkedlist.java
Note: doublylinkedlist.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java doublylinkedlist
Enter the number of elements:5
Insert at first:
Enter elements:
5
4
3
2
1
1 2 3 4 5
Insert last:
Enter elements:
6
7
8
9
10
1 2 3 4 5 6 7 8 9 10
Do you want to delete first element:
1.Yes 2.No
1
2 3 4 5 6 7 8 9 10
Do you want to delete the last element:
1.Yes 2.No
1
2 3 4 5 6 7 8 9
Insert at particular position:
Enter the position & element:
5
6
2 3 4 5 6 6 7 8 9
Delete from particular position:
Enter the position:
5
2 3 4 5 6 7 8 9
import java.io.*;
class Node
{
public int data;
public Node next;
public Node previous;
public Node(int x)
{
data=x;
}
public void display()
{
System.out.print(data+" ");
}
}
class lab
{
private Node first;
private Node last;
public lab()
{
first=null;
last=null;
}
public void insertfirst(int x)
{
Node newnode=new Node(x);
if(isEmpty())
last=newnode;
else
first.previous=newnode;
newnode.next=first;
first=newnode;
}
public void insertlast(int y)
{
Node newnode=new Node(y);
if(isEmpty())
first=newnode;
else
{
last.next=newnode;
newnode.previous=last;
}
last=newnode;
}
public boolean isEmpty()
{
return(first==null&&last==null);
}
public void display()
{
Node current=first;
while(current!=null)
{
System.out.print(" "+current.data);
current=current.next;
}
}
public void delfirst()
{
first=first.next;
first.previous=null;
}
public void dellast()
{
last=last.previous;
last.next=null;
}
public void insertatp(int p,int x2)
{
Node newnode=new Node(x2);
Node current=first;
int count=1;
while(count+1<p)
{
current=current.next;
count++;
if(current.next==null)
{
System.out.print("Error");
break;
}
}
Node temp=current.next;
current.next=newnode;
newnode.next=temp;
}
public void deletatp(int p1)
{
Node current=first;
int count=1;
while(count+1<p1)
{
current=current.next;
count++;
if(current.next==null)
{
System.out.print("Error");
break;
}
}
Node temp=current.next;
current.next=temp.next;
}
}
class doublylinkedlist
{
public static void main (String arg[]) throws IOException
{
lab a1=new lab();
DataInputStream x=new DataInputStream(System.in);
System.out.print("Enter the number of elements:");
int n= Integer.parseInt(x.readLine());
System.out.print("\nInsert at first:\n");
System.out.print("\nEnter elements:\n");
for(int i=0;i<n;i++)
a1.insertfirst(Integer.parseInt(x.readLine()));
a1.display();
System.out.print("\nInsert last:\n");
System.out.print("Enter elements:\n");
for(int i=0;i<n;i++)
a1.insertlast(Integer.parseInt(x.readLine()));
a1.display();
System.out.println("\nDo you want to delete first element:\n1.Yes\t2.No");
switch(Integer.parseInt(x.readLine()))
{
case 1:
a1.delfirst(); break;
case 2:
default: break;
}
a1.display();
System.out.println("\nDo you want to delete the last element:\n1.Yes\t2.No");
switch(Integer.parseInt(x.readLine()))
{
case 1:
a1.dellast(); break;
case 2:
default: break;
}
a1.display();
System.out.print("\nInsert at particular position:\nEnter the position & element:\n");
a1.insertatp(Integer.parseInt(x.readLine()),Integer.parseInt(x.readLine()));
a1.display();
System.out.print("\nDelete from particular position:\nEnter the position:\n");
a1.deletatp(Integer.parseInt(x.readLine()));
a1.display();
}}
Output:
nn@linuxmint ~ $ javac doublylinkedlist.java
Note: doublylinkedlist.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java doublylinkedlist
Enter the number of elements:5
Insert at first:
Enter elements:
5
4
3
2
1
1 2 3 4 5
Insert last:
Enter elements:
6
7
8
9
10
1 2 3 4 5 6 7 8 9 10
Do you want to delete first element:
1.Yes 2.No
1
2 3 4 5 6 7 8 9 10
Do you want to delete the last element:
1.Yes 2.No
1
2 3 4 5 6 7 8 9
Insert at particular position:
Enter the position & element:
5
6
2 3 4 5 6 6 7 8 9
Delete from particular position:
Enter the position:
5
2 3 4 5 6 7 8 9
0 comments:
Post a Comment