Docsity

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

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