472,353 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,353 developers and data experts.

Doubly Linked List In java || Implement DLL Data Structure

package com.ds.algorithms.linkedlist.DLLImplementation;

import java.util.NoSuchElementException;

/**
* @Author pankaj
* @create 4/12/21 7:05 PM
*/
public class DoublyLinkedList {
private Node head;// first node of DLL
private Node tail;//Last Node of DLL
private int length;// length of DLL

private class Node{
private int data;
private Node next;
private Node previous;
Node(int data) {
this.data=data;
}
}
// constructor
DoublyLinkedList()
{
this.head=null;
this.tail=null;
this.length=0;
}
// isEmpty()
// two ways to check , DLL is Empty 1. By checking head ref 2. By checking DLL length is zero(0)
public boolean isEmpty()
{
if(head==null)
return true;
else return false;
// return length==0;// DLL is empty-> true
}
//length() of DLL
public int length()
{
return length;
}

//------------------- print DLL in Forward Direction -----------------------
public void printDLLForward()
{
if (head==null) {
return;
}
Node traverse=head;
while (traverse !=null)
{
System.out.print(traverse.data+" --> ");
traverse=traverse.next;
}
System.out.println("null");
}
// ------------------------ print DLL in Backward Direction ----------------------
public void printDLLBackward()
{
if (tail==null) {
return;
}
Node traverse=tail;
while (traverse !=null)
{
System.out.print(traverse.data+" --> ");
traverse=traverse.previous;
}
System.out.println("null");
}
//------------------- insertFirst ---------------------
public void insertFirst(int data){
Node newNode=new Node(data);
if (isEmpty())
{
tail=newNode;
}else {
head.previous=newNode; //========
}
newNode.next=head;
head=newNode;
length++;
}

// -----------------insertLast ------------------------------------------
public void insertLast(int data)
{
Node newNode=new Node(data);
if (isEmpty())
{
head=newNode;// head is constant
} else {
tail.next=newNode;//--------
newNode.previous=tail;
}
tail=newNode;
length++;
}
// -------------------------------delete First ------------------------------
public Node deleteFirst()
{
if (isEmpty())
{
throw new NoSuchElementException("DLL is Already Empty !!!!");
}
Node newNode= head;
if (head == tail)
{
tail=null;
} else {
head.next.previous=null;
}
head=head.next;
newNode.next=null;
length--;
return newNode;
}
// ------------------ delete Last -------------------------
public Node deleteLast()
{
if(isEmpty()) {
throw new NoSuchElementException();
}
Node newNode=tail;
if (head==tail)
{
head=null;
} else {
tail.previous.next=null;
}
tail= tail.previous;
newNode.previous=null;
length--;
return newNode;
}
public static void main(String[] args) {
DoublyLinkedList dll=new DoublyLinkedList();
/*dll.insertFirst(0);
dll.insertFirst(1);
dll.insertFirst(5);
dll.insertFirst(7);
dll.insertFirst(9);
dll.insertFirst(12);
dll.printDLLForward();*/
// dll.printDLLBackward();

dll.insertLast(1);
dll.insertLast(2);
dll.insertLast(3);
dll.insertLast(4);
dll.insertLast(5);

dll.printDLLForward();
dll.printDLLBackward();
System.out.println("----------------------------------------");
// dll.deleteFirst();
//dll.printDLLForward();
// dll.deleteFirst();
// dll.deleteFirst();
dll.deleteLast();
dll.printDLLForward();
}
}
Apr 14 '21 #1
0 3448

Sign in to post your reply or Sign up for a free account.

Similar topics

270
by: Jatinder | last post by:
I found these questions on a web site and wish to share with all of u out there,Can SomeOne Solve these Porgramming puzzles. Programming...
4
by: dssuresh6 | last post by:
Whether browsing forward or backward can be done using a singly linked list. Is there any specific case where a doubly linked list is needed? For...
1
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It...
2
by: murali | last post by:
Hi, I want to insert a node in an sorted doubly linked list of integers in ascending order. The list should not have duplicate nodes. I need an...
5
by: adam.kleinbaum | last post by:
Hi there, I'm a novice C programmer working with a series of large (30,000 x 30,000) sparse matrices on a Linux system using the GCC compiler. ...
4
kim6987
by: kim6987 | last post by:
can you please spend a little time evaluating this code. I can not run it successfully thanks :) #include<stdio.h> #include<conio.h>...
1
by: Mahesh | last post by:
Hi Coders, I was asked to write a program to interchange numbers using doubly linked list @ Amazon. Here is the details with Code that i...
4
by: arnuld | last post by:
This program follows from the section 6.5 of K&R2 where authors created a doubly-linked list using a binary-tree based approach. The only thing I...
3
Malathi
by: Malathi | last post by:
Hi, In SQL clustered tables (B tree), pages are linked in all levels by doubly linked list. Already the index is pointing to the corresponding...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.