0% found this document useful (0 votes)
12 views8 pages

Linear Data Structures - Doubly Linked Lists Cheatsheet - Codecademy

The document provides a comprehensive overview of a Java DoublyLinkedList class, detailing methods for adding and removing nodes from both the head and tail, as well as removing nodes by data. It includes the implementation of a Node class that supports pointers to previous and next nodes, along with necessary setter and getter methods. Additionally, it outlines the constructor and instance variables for the DoublyLinkedList class.

Uploaded by

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

Linear Data Structures - Doubly Linked Lists Cheatsheet - Codecademy

The document provides a comprehensive overview of a Java DoublyLinkedList class, detailing methods for adding and removing nodes from both the head and tail, as well as removing nodes by data. It includes the implementation of a Node class that supports pointers to previous and next nodes, along with necessary setter and getter methods. Additionally, it outlines the constructor and instance variables for the DoublyLinkedList class.

Uploaded by

usernxt123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Cheatsheets / Linear Data Structures

Doubly Linked Lists

Adding to the Tail

A Java DoublyLinkedList class can implement public void addToTail(String data) {


an .addToTail() instance method for adding
Node newTail = new Node(data);
new data to the tail of the list. .addToTail()
takes a single data argument. It uses data to Node currentTail = this.tail;
create a new Node which it adds to the tail of the list.
if (currentTail != null) {
currentTail.setNextNode(newTail);

newTail.setPreviousNode(currentTail);
}
this.tail = newTail;

if (this.head == null) {
this.head = newTail;
}
}
Adding to the Head

A Java DoublyLinkedList class can implement public void addToHead(String data) {


an .addToHead() instance method for adding
Node newHead = new Node(data);
new data to the head of the list. .addToHead()
takes a single data argument. It uses data to Node currentHead = this.head;
create a new Node which it adds to the head of the
list. if (currentHead != null) {

currentHead.setPreviousNode(newHead);
newHead.setNextNode(currentHead);
}
this.head = newHead;

if (this.tail == null) {
this.tail = newHead;
}
}

Removing the Tail

A Java DoublyLinkedList class can implement public String removeTail() {


a .removeTail() instance method for removing
Node removedTail = this.tail;
the tail of the list. .removeTail() takes no
arguments. It removes and returns the tail of the list’s
data , and sets the tail’s previous node as the new if (removedTail == null) {
tail. return null;
}
this.tail =
removedTail.getPreviousNode();

if (this.tail != null) {
this.tail.setNextNode(null);
}
if (removedTail == this.head) {
this.removeHead();
}
return removedTail.data;
}
Removing the Head

A Java DoublyLinkedList class can implement public String removeHead() {


a .removeHead() instance method for removing
Node removedHead = this.head;
the head of the list. .removeHead() takes no
arguments. It removes and returns the head of the list’s
data , and sets the head’s next node as the new if (removedHead == null) {
head. return null;
}
this.head =
removedHead.getNextNode();

if (this.head != null) {
this.head.setPreviousNode(null);
}
if (removedHead == this.tail) {
this.removeTail();
}
return removedHead.data;
}

Constructor and Instance Variables

A Java DoublyLinkedList class has two public Node head;


Node instance variables:
public Node tail;
a public head
a public tail
The constructor takes no parameters and sets both public DoublyLinkedList() {
instance variables to null . this.head = null;
this.tail = null;
}
Doubly Linked List Overview

A DoublyLinkedList class in Java has the public class DoublyLinkedList {


following attributes:
public head and tail instance Node
variables public Node head;
a constructor that sets the head and tail nodes public Node tail;
to null
an .addToHead() method to add new
nodes to the head public DoublyLinkedList() {
an .addToTail() method to add new this.head = null;
nodes to the tail this.tail = null;
a .removeHead() method to remove the
}
head node
a .removeTail() method to remove the
tail node public void addToHead(String data) {
a .removeByData() method to remove a
Node newHead = new Node(data);
node that matches the data passed in
Node currentHead = this.head;

if (currentHead != null) {

currentHead.setPreviousNode(newHead);
newHead.setNextNode(currentHead);
}
this.head = newHead;

if (this.tail == null) {
this.tail = newHead;
}
}

public void addToTail(String data) {


Node newTail = new Node(data);
Node currentTail = this.tail;

if (currentTail != null) {
currentTail.setNextNode(newTail);

newTail.setPreviousNode(currentTail);
}
this.tail = newTail;

if (this.head == null) {
this.head = newTail;
}
}

public String removeHead() {


Node removedHead = this.head;

if (removedHead == null) {
return null;
}
this.head =
removedHead.getNextNode();

if (this.head != null) {
this.head.setPreviousNode(null);
}
if (removedHead == this.tail) {
this.removeTail();
}
return removedHead.data;
}

public String removeTail() {


Node removedTail = this.tail;

if (removedTail == null) {
return null;
}
this.tail =
removedTail.getPreviousNode();

if (this.tail != null) {
this.tail.setNextNode(null);
}
if (removedTail == this.head) {
this.removeHead();
}
return removedTail.data;
}

public Node removeByData(String data) {


Node nodeToRemove = null;
Node currentNode = this.head;

while (currentNode != null) {


if (currentNode.data == data) {
nodeToRemove = currentNode;
break;
}
currentNode =
currentNode.getNextNode();
}

if (nodeToRemove == null) {
return null;
}
if (nodeToRemove == this.head) {
this.removeHead();
} else if (nodeToRemove == this.tail)
{
this.removeTail();
} else {
Node nextNode =
nodeToRemove.getNextNode();
Node previousNode =
nodeToRemove.getPreviousNode();

nextNode.setPreviousNode(previousNode);
previousNode.setNextNode(nextNode);
}
return nodeToRemove;
}

}
Removing by Data

A Java DoublyLinkedList class can implement public Node removeByData(String data) {


a .removeByData() instance method that takes
Node nodeToRemove = null;
data as an argument and returns the node that
matches data , or null if no match exists. If the Node currentNode = this.head;
node exists, .removeByData() removes it from
the list and correctly resets the pointers of its while (currentNode != null) {
surrounding nodes.
if (currentNode.data == data) {
nodeToRemove = currentNode;
break;
}
currentNode =
currentNode.getNextNode();
}

if (nodeToRemove == null) {
return null;
}
if (nodeToRemove == this.head) {
this.removeHead();
} else if (nodeToRemove == this.tail)
{
this.removeTail();
} else {
Node nextNode =
nodeToRemove.getNextNode();
Node previousNode =
nodeToRemove.getPreviousNode();

nextNode.setPreviousNode(previousNode);
previousNode.setNextNode(nextNode);
}
return nodeToRemove;
}
Updated Node Class

Doubly linked lists in Java utilize an updated Node public class Node {
class that has a pointer to the previous node. This
comes with additional setter and getter methods for
accessing the previous node. public String data;
private Node next;
private Node previous;

public Node(String data) {


this.data = data;
this.next = null;
}

public void setNextNode(Node node) {


this.next = node;
}

public void setPreviousNode(Node node)


{
this.previous = node;
}

public Node getNextNode() {


return this.next;
}

public Node getPreviousNode() {


return this.previous;
}

Print Share

You might also like