473,421 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,421 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 3680

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 Puzzles Some companies certainly ask for these...
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 people who say that singly linked list allows...
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 has to have both a head and a tail pointer, and...
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 algorithm (assuming an object oriented language) ...
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. To represent and store these matrices, I'd like to...
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> #include<stdlib.h> #define SIZE 10 typedef struct dlist
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 wrote. i/p: 1 2 3 4 5 6 7 8 .....n,n-1. o/p: 2 1 4...
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 have rewritten myself is the getword function. I am...
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 pages then why we need the doubly linked list between...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.