0% found this document useful (0 votes)
3 views10 pages

3 DoublyLinkedListPrograms@Java

The document contains several Java programs demonstrating the implementation of a Doubly Linked List (DLL). It includes functionalities for creating a DLL, inserting nodes at the beginning and end, and deleting nodes from both ends, along with display methods to show the current elements in the list. Each program is accompanied by sample output showcasing the results of the operations performed.

Uploaded by

sanjanadeo2005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

3 DoublyLinkedListPrograms@Java

The document contains several Java programs demonstrating the implementation of a Doubly Linked List (DLL). It includes functionalities for creating a DLL, inserting nodes at the beginning and end, and deleting nodes from both ends, along with display methods to show the current elements in the list. Each program is accompanied by sample output showcasing the results of the operations performed.

Uploaded by

sanjanadeo2005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

//Java Program to create a Doubly Linked list

class DLLNode
{
public int info;
public DLLNode prev;
public DLLNode next;

public DLLNode()
{
info=0;
prev=null;
next=null;
}
}

class CreateDLL
{
public DLLNode first,last;
public CreateDLL()
{
first=null;
last=null;
}
public void CreateDLL(int item)
{
DLLNode newNode=new DLLNode();
[Link]=item;
[Link]=[Link]=null;
if(first==null)
{
first=newNode;
last=newNode;
}
else
{
[Link]=newNode;
[Link]=last;
last=newNode;
}
}

public void Display()


{
DLLNode temp=new DLLNode();
if(first==null)
{
[Link]("Empty Linked List");
}
else
{
temp = first;
while (temp != last)
{
[Link]([Link] + " ");
temp = [Link];
}
[Link]([Link]);
}
}
public static void main(String[] args)
{
CreateDLL obj1=new CreateDLL();
[Link](100);
[Link](200);
[Link](300);
[Link](400);
[Link](500);
[Link]("Doubly Linked List Elements are: ");
[Link]();
}
}

Output:
Doubly Linked List Elements are:
100
200
300
400
500
//Java Program to insert a node at the beginning of the Doubly Linked list
class DLLNode
{
public int info;
public DLLNode prev;
public DLLNode next;

public DLLNode()
{
info=0;
prev=null;
next=null;
}
}

class InsertionDLL
{
public DLLNode first,last;
public InsertionDLL()
{
first=null;
last=null;
}
public void insertBeg(int item)
{
DLLNode newNode=new DLLNode();
[Link]=item;
[Link]=[Link]=null;
if(first==null)
{
first=newNode;
last=newNode;
}
else
{
[Link]=first;
[Link]=newNode;
first=newNode;
}
}

public void Display()


{
DLLNode temp=new DLLNode();
if(first==null)
{
[Link]("Empty Linked List");
}
else
{
temp = first;
while (temp != last)
{
[Link]([Link] + " ");
temp = [Link];
}
[Link]([Link]);
}
}
public static void main(String[] args)
{
InsertionDLL obj1=new InsertionDLL();
[Link](100);
[Link](200);
[Link](300);
[Link](400);
[Link](500);
[Link]("Doubly Linked List Elements are: ");
[Link]();
}
}

Output:
Doubly Linked List Elements are:
500
400
300
200
100
//Java Program to insert a node at the end of the Doubly Linked list
class DLLNode
{
public int info;
public DLLNode prev;
public DLLNode next;

public DLLNode()
{
info=0;
prev=null;
next=null;
}
}

class InsertionDLL
{
public DLLNode first,last;
public InsertionDLL()
{
first=null;
last=null;
}
public void insertEnd(int item)
{
DLLNode newNode=new DLLNode();
[Link]=item;
[Link]=[Link]=null;
if(first==null)
{
first=newNode;
last=newNode;
}
else
{
[Link]=newNode;
[Link]=last;
last=newNode;
}
}

public void Display()


{
DLLNode temp=new DLLNode();
if(first==null)
{
[Link]("Empty Linked List");
}
else
{
temp = first;
while (temp != last)
{
[Link]([Link] + " ");
temp = [Link];
}
[Link]([Link]);
}
}
public static void main(String[] args)
{
InsertionDLL obj1=new InsertionDLL();
[Link] (100);
[Link](200);
[Link](300);
[Link](400);
[Link](500);
[Link]("Doubly Linked List Elements are: ");
[Link]();
}
}

Output:

Doubly Linked List Elements are:


100
200
300
400
500
//Java Program to delete the first node of the Doubly Linked list
class DLLNode
{
public int info;
public DLLNode prev;
public DLLNode next;

public DLLNode()
{
info=0;
prev=null;
next=null;
}
}

class DeletionDLL
{
public DLLNode first,last;
public DeletionDLL()
{
first=null;
last=null;
}
public void insertEnd(int item) //Creating Doubly Linked List
{
DLLNode newNode=new DLLNode();
[Link]=item;
[Link]=[Link]=null;
if(first==null)
{
first=newNode;
last=newNode;
}
else
{
[Link]=newNode;
[Link]=last;
last=newNode;
}
}
public void deleteFirst()
{
if(first==null)
{
[Link]("Empty Linked List");
}
else if(first==last)
{
first=null;
last=null;
}
else
{
first=[Link];
}
}
public void Display()
{
DLLNode temp=new DLLNode();
if(first==null)
{
[Link]("Empty Linked List");
}
else
{
temp = first;
while (temp != last)
{
[Link]([Link] + " ");
temp = [Link];
}
[Link]([Link]);
}
}

public static void main(String[] args)


{
DeletionDLL obj1=new DeletionDLL ();
[Link](100);
[Link](200);
[Link](300);
[Link](400);
[Link](500);
[Link]("Doubly Linked List Elements are: ");
[Link]();
[Link]("Deleting the first node:");
[Link]();
[Link]();
}
}

Output:

Doubly Linked List Elements are:


100
200
300
400
500
Deleting the first node:
200
300
400
500
//Java Program to delete the last node of the Doubly Linked list
class DLLNode
{
public int info;
public DLLNode prev;
public DLLNode next;

public DLLNode()
{
info=0;
prev=null;
next=null;
}
}

class DeletionDLL
{
public DLLNode first,last;
public DeletionDLL()
{
first=null;
last=null;
}
public void insertEnd(int item) //Creating Doubly Linked List
{
DLLNode newNode=new DLLNode();
[Link]=item;
[Link]=[Link]=null;
if(first==null)
{
first=newNode;
last=newNode;
}
else
{
[Link]=newNode;
[Link]=last;
last=newNode;
}
}
public void deleteLast()
{
if(last==null)
{
[Link]("Empty Linked List");
}
else if(first==last)
{
first=null;
last=null;
}
else
{
DLLNode temp=new DLLNode();
temp=first;
while([Link]!=last)
{
temp=[Link];
}
[Link]=null;
last=temp;

}
}

public void Display()


{
DLLNode temp=new DLLNode();
if(first==null)
{
[Link]("Empty Linked List");
}
else
{
temp = first;
while (temp != last)
{
[Link]([Link] + " ");
temp = [Link];
}
[Link]([Link]);
}
}

public static void main(String[] args)


{
DeletionDLL obj1=new DeletionDLL();
[Link](100);
[Link](200);
[Link](300);
[Link](400);
[Link](500);
[Link]("Doubly Linked List Elements are: ");
[Link]();
[Link]("Deleting the last node:");
[Link]();
[Link]();
}
}

Output:

Doubly Linked List Elements are:


100
200
300
400
500
Deleting the last node:
100
200
300
400

You might also like