









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Lecture notes on Doubly Linked Lists, a data structure consisting of a sequence of nodes with references to both the previous and next nodes. The notes cover the basics of Doubly Linked Lists, including their implementation using Java, and various operations such as adding and removing nodes at the head, tail, and between nodes. The document also mentions the time complexity of these operations.
Typology: Lecture notes
1 / 17
This page cannot be seen from the preview
Don't miss anything!










Doubly Linked List
Elt
Head
Elt
Elt Elt
Tail
DoublyLinkedList
public class DoublyLinkedList
protected int count; // number within list
protected DoublyLinkedNode
protected DoublyLinkedNode
//construct an empty list
public DoublyLinkedList() {
head = null;
tail = null;
count = 0;
}
public E getFirst() {
return head.value(); //returns first value in list
}
public E getLast() {
return tail.value(); //returns lastvalue in list
}
public int size() {
return count;
}
4
Adding at the head
Head
Elt
Elt2 Elt
Tail
Head Tail
Elt
Elt2 Elt
Elt
Elt
Elt2 Elt
Elt
Head
Tail
Removing at the head
Elt
Elt Elt Elt
Head
Elt
Elt Elt Elt
Head
Tail
Tail
Elt
Elt2 Elt
Head
Tail
Removing at the head is ๐( 1 )
public E removeFirst(){
//check that list is not empty
DoublyLinkedNode
head = head.next(); // move head down list
if (head != null)
head.setPrevious(null);
else
tail = null; //remove final value
count--;
return temp.value();
}
Adding at the tail is ๐( 1 )
Removing from the tail
Elt
Elt
Elt
Head
Tail
Elt
Elt
Elt1 Elt
Head
Tail
Elt
Elt
Elt1 Elt
Head
Tail
Adding between nodes Elt Elt Elt Head Tail Example: add(2,Elt3) Elt Elt Elt Head Tail Elt Elt3 Elt Tail Elt Head Elt
Adding between nodes is ๐(๐)
public void add(int i, E o) {
//check that i in range. If i==0 call addFirst,
//if i==size() call addLast()
DoublyLinkedNode
DoublyLinkedNode
while (i > 0) { // search for ith position, or end of list
before = after;
after = after.next();
i--;
// create new value to insert in correct position
DoublyLinkedNode
count++; // make after and before value point to new value
before.setNext(current);
after.setPrevious(current);
Removing between nodes is ๐(๐)
public E remove(int i) {
//check that i in range. If i==0 call removeFirst,
//if i==size() call removeLast()
DoublyLinkedNode
DoublyLinkedNode
while (i > 0) { // search for ith position, or end of list
previous = finger;
finger = finger.next();
i--;
previous.setNext(finger.next());
finger.next().setPrevious(previous);
count--;
return finger.value();
More on Linked Lists
structure5 provides iterators both for singly and doubly
linked lists ร Check the code!
java.util.LinkedList implements a Doubly Linked List
Doubly linked lists are often represented as circular:
Head
Elt
Elt Elt
Tail