Doubly Linked Lists: A Data Structure with Previous and Next References, Lecture notes of Data Structures and Algorithms

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

2021/2022

Uploaded on 09/12/2022

rajeshi
rajeshi ๐Ÿ‡บ๐Ÿ‡ธ

4.1

(9)

237 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 62
Fall 2018
Alexandra Papoutsaki & William Devanny
1
Lecture 12: Doubly
Linked Lists
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Doubly Linked Lists: A Data Structure with Previous and Next References and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

CS 62

Fall 2018

Alexandra Papoutsaki & William Devanny

Lecture 12: Doubly

Linked Lists

Doubly Linked List

  • A linked list consisting of a sequence of nodes, starting from a

head pointer and ending to a tail

  • Each node stores
    • Element
    • Link to the previous node
    • Link to the next node

Elt

next

node

Head

Elt

Elt Elt

previous

Tail

DoublyLinkedList

public class DoublyLinkedList extends AbstractList {

protected int count; // number within list

protected DoublyLinkedNode head; // ref. to first element

protected DoublyLinkedNode tail; // ref. to last element

//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 temp = head;

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 )

public void addLast(E value) {

tail = new DoublyLinkedNode(value, null, tail);

if (head == null)

head = tail; //fix up head, itโ€™s the first node

count++;

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 before = null;

DoublyLinkedNode after = head;

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 current = new DoublyLinkedNode(o,after,before);

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 previous = null;

DoublyLinkedNode finger= head;

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