cs350: data structures doubly linked lists - ycpcs.github.io · cs350: data structures doubly...

19
CS350: Data Structures © James Moscola James Moscola Department of Engineering & Computer Science York College of Pennsylvania CS350: Data Structures Doubly Linked Lists

Upload: others

Post on 03-Sep-2019

43 views

Category:

Documents


0 download

TRANSCRIPT

CS350: Data Structures © James Moscola

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATA

LOG

2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATA

LOG

2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATALO

G 2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

James MoscolaDepartment of Engineering & Computer ScienceYork College of Pennsylvania

CS350: Data StructuresDoubly Linked Lists

CS350: Data Structures

Doubly Linked Lists

• Adds an additional pointer to a the list nodes that points to the previous node in the list

• Traversable in either the forward or backward direction

• Resolves the issue of removing the last node in the linked list - Becomes a O(1) operation as opposed to O(N)

2

data

CS350: Data Structures

Doubly Linked List Operations

• Basic operations include: - insert / add- remove / removeFirst / removeLast

• Additional operations may include: - getFirst / getLast- find- isEmpty- makeEmpty

3

CS350: Data Structures

Doubly Linked List Implementation

• Basic implementation uses head and tail pointers that points to the first node and the last node in the list - Both pointers points to NULL upon initialization when no nodes exist

in the list

• Depending on implementation, insertion may take place at the head of the list, at the tail of the list, or at some other specified node

4

head

NULL

tail

CS350: Data Structures

After Inserting a Node

• In this illustration, nodes are inserted at the tail end (nodes can be inserted at either the head or the tail) - After inserting the first node, A- The head and the tail pointers are reassigned to point to the first

node

5

ANULL

head

tail

NULL

CS350: Data Structures

After Inserting Additional Nodes

• In this illustration, nodes are inserted at the tail end (nodes can be inserted at either the head or the tail) - After inserting nodes in the sequence A, B, C- The tail pointer advances with each insertion at the tail end

6

BA C NULL

head

tail

NULL

CS350: Data Structures

After Inserting Additional Nodes

• In this illustration, nodes are inserted at the head - After inserting the node D- The head pointer retreats with each insertion at the head

7

BA C

head

tail

D NULL

NULL

CS350: Data Structures

After Removing the First Node

• Nodes can be removed from the head of the list or the tail

- After removing a single node from the head of the list

- The head pointer advances with a removal from the head of the list

8

BA C

head

tail

NULLNULL

CS350: Data Structures

After Removing the Last Node

• Nodes can be removed from the head of the list or the tail

- After removing a single node from the tail of the list

- The tail pointer retreats with a removal from the tail of the list

9

BA

head

tail

NULLNULL

CS350: Data Structures

Doubly Linked List Implementation

10

public class DLinkedListNode<E> {public E data;public LinkedListNode<E> next;public LinkedListNode<E> prev;

}

CS350: Data Structures

Doubly Linked List Implementation

11

// Inserts at the tail of the list

public void append (E data) {DLinkedListNode<E> newNode = new DLinkedListNode<E>();newNode.data = data; // assign data to newNodenewNode.prev = tail;tail.next = newNode;tail = newNode;

}

This method is oversimplified, what happens if this is called when the list is empty?

CS350: Data Structures

Doubly Linked List Implementation

12

// Inserts at the tail of the list

public void append (E data) {DLinkedListNode<E> newNode = new DLinkedListNode<E>();newNode.data = data; // assign data to newNodeif (isEmpty()) {

head = tail = newNode;} else {

newNode.prev = tail;tail.next = newNode;tail = newNode;

}}

Fixed append method

CS350: Data Structures

Doubly Linked List Implementation

13

// Inserts at the head of the list

public void prepend (E data) {DLinkedListNode<E> newNode = new DLinkedListNode<E>();newNode.data = data; // assign data to newNodenewNode.next = head;head.prev = newNode;head = newNode;

}

This method is oversimplified, what happens if this is called when the list is empty?

CS350: Data Structures

Doubly Linked List Implementation

14

// Removes node from head of list and returns its value

public E removeFirst() {if (head != null) {

E nodeData = head.data;head.next.prev = null;head = head.next;return nodeData;

} else {return null;

}}

CS350: Data Structures

Considerations for Linked List Implementation

• Implementation as previously shown requires a error checking in the insertion and removal methods to check for edge cases (i.e. checking for an empty list)

• To improve the speed of operations, it is possible to remove these tests - Tradeoff: speedup comes at the expense of one/two additional

‘dummy’ nodes in the Linked List

• Idea: create one or two dummy nodes (sentinel nodes) that exists in the linked list at ALL times - Eliminates the need to always check for NULL

- Generalized the insertion and removal methods

15

CS350: Data Structures

• To check for empty: (head.next == tail);

• When traversing the list check to see if current position points to either the head or the tail to determine if at the end of the list

Doubly Linked List with Two Sentinel Nodes

16

Empty list

NULLNULL

head

NULLNULL

tail

CS350: Data Structures

Doubly Linked List with Two Sentinel Nodes

17

List with two nodes

NULLNULL

head

NULLNULL

tail

A B

CS350: Data Structures

Doubly Linked List with a Single Sentinel Node

18

Empty list

NULL

head

tail

How should isEmpty( ) be implemented?

CS350: Data Structures

Doubly Linked List with a Single Sentinel Node

19

List with two nodes

NULL

head

tail

A B