26
CS 350: Data Structures © James Moscola David Babcock (courtesy of James Moscola) Department of Physical Sciences York College of Pennsylvania CS 350 : Data Structures Linked Lists Department of Physical Sciences York College of Pennsylvania Monday, September 3, 12

CS 350 : Data Structures Linked Listsfaculty.ycp.edu/~dbabcock/PastCourses/cs350/lectures/Linked_Lists.pdf · - circular linked lists ... CS 350: Data Structures Linked List Insertion

  • Upload
    lamdat

  • View
    226

  • Download
    0

Embed Size (px)

Citation preview

CS 350: 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

David Babcock (courtesy of James Moscola)Department of Physical SciencesYork College of Pennsylvania

CS 350 : Data StructuresLinked Lists

Department of Physical SciencesYork College of Pennsylvania

Monday, September 3, 12

CS 350: Data Structures

Linked Lists

• Come in a variety of different forms- singly linked lists- doubly linked lists- circular linked lists

• Composed of a set of nodes that hold data and contain one or more pointers to neighboring nodes in the list- singly linked lists contain only a pointer to the next node in the list- doubly linked lists contain a pointer to the next node and the

previous node in the list

2

data

Monday, September 3, 12

CS 350: Data Structures

Linked List Operations

• Basic operations include:-insert / add

-remove

• Additional operations may include:-getFirst

-getLast

-find

-isEmpty

-makeEmpty

3Monday, September 3, 12

CS 350: Data Structures

Linked List Insertion

• Basic implementation uses a head pointer that points to the first node in the list- 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

Monday, September 3, 12

CS 350: Data Structures

Linked List Insertion

5

Start with Empty List

head points to the front of the linked list

tail points to the back of the linked list

head

null

tail

Monday, September 3, 12

CS 350: Data Structures

null

Linked List Insertion

6

head

tail

Monday, September 3, 12

CS 350: Data Structures

null

Linked List Insertion

6

head

Insert Value: A

nullA

tail

Monday, September 3, 12

CS 350: Data Structures

null

Linked List Insertion

7

head

nullA

tail

Monday, September 3, 12

CS 350: Data Structures

null

Linked List Insertion

7

head

Insert Value: B

nullA

tail

nullB

Monday, September 3, 12

CS 350: Data Structures

null

Linked List Insertion

8

head

nullA

tail

nullB

Monday, September 3, 12

CS 350: Data Structures

null

Linked List Insertion

8

head

Insert Value: C

nullA

tail

nullB nullC

Monday, September 3, 12

CS 350: Data Structures

null

Linked List Removal

9

head

nullA

tail

nullB nullC

Monday, September 3, 12

CS 350: Data Structures

null

Linked List Removal

9

head

Remove First Value:

nullA

tail

nullB nullC

Monday, September 3, 12

CS 350: Data Structures

Linked List Removal

• Removing last value is not very efficient when using singly linked lists- Want to make next-to-last node in list the last node- Must traverse entire list, starting from the head, to find the next-to-

last node in the list- O(N)

10

null

head

nullA

tail

nullB nullC

Monday, September 3, 12

CS 350: Data Structures

Linked List Implementation

11

public class LinkedListNode<E> { public E data; public LinkedListNode<E> next; }

Monday, September 3, 12

CS 350: Data Structures

Linked List Implementation

12

// Inserts at the tail of the list

public void insert (E data) { LinkedListNode<E> newNode = new LinkedListNode<E>(); newNode.data = data; // assign data to newNode tail.next = newNode; tail = newNode; }

Monday, September 3, 12

CS 350: Data Structures

Linked List Implementation

12

// Inserts at the tail of the list

public void insert (E data) { LinkedListNode<E> newNode = new LinkedListNode<E>(); newNode.data = data; // assign data to newNode tail.next = newNode; tail = newNode; }

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

Monday, September 3, 12

CS 350: Data Structures

Linked List Implementation

13

// Inserts at the tail of the list public void insert (E data) { LinkedListNode<E> newNode = new LinkedListNode<E>(); newNode.data = data; // assign data to newNode if (isEmpty()) { head = tail = newNode; } else { tail.next = newNode; tail = newNode; } }

Fixed insert method

Monday, September 3, 12

CS 350: Data Structures

Linked List Implementation

14

// Inserts at the head of the list public void insertAtHead (E data) { LinkedListNode<E> newNode = new LinkedListNode<E>(); newNode.data = data; // assign data to newNode newNode.next = head; head = newNode; }

Monday, September 3, 12

CS 350: Data Structures

Linked List Implementation

14

// Inserts at the head of the list public void insertAtHead (E data) { LinkedListNode<E> newNode = new LinkedListNode<E>(); newNode.data = data; // assign data to newNode newNode.next = head; head = newNode; }

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

Monday, September 3, 12

CS 350: Data Structures

Linked List Implementation

15

// Inserts at the head of the list public void insertAtHead (E data) { LinkedListNode<E> newNode = new LinkedListNode<E>(); newNode.data = data; // assign data to newNode newNode.next = head;

if (!isEmpty()) { head = newNode; } else { head = tail = newNode; } }

Monday, September 3, 12

CS 350: Data Structures

Linked List Implementation

16

// Removes node from head of list and returns its value public E remove() { if (head != null) { E nodeData = head.data; head = head.next; return nodeData; } else { return null; } }

Monday, September 3, 12

CS 350: Data Structures

Considerations for Linked List Implementation

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

• To improve the speed of the Linked List operations, it is possible to remove these tests- Tradeoff: speedup comes at the expense of one additional ‘dummy’

node in the Linked List

• Idea: create a dummy node that exists in the linked list at ALL times ... it is created as part of the list and points to the head node- Eliminates the need to always check for null- Generalized the insert and remove methods

17Monday, September 3, 12

CS 350: Data Structures

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

null null null

Linked List with Header Node

18

head

nullA

tail

null null

head

tail

Empty list List with single node

Monday, September 3, 12

CS 350: Data Structures

Linked List Implementation

19

// Inserts at the tail of the list // When using dummy header node, no need to test for null

public void insert (E data) { LinkedListNode<E> newNode = new LinkedListNode<E>(); newNode.data = data; // assign data to newNode tail.next = newNode; tail = newNode; }

Monday, September 3, 12

CS 350: Data Structures

Linked List Implementation

20

// Inserts at the head of the list public void insertAtHead (E data) { LinkedListNode<E> newNode = new LinkedListNode<E>(); newNode.data = data; // assign data to newNode newNode.next = head; head = newNode; }

Monday, September 3, 12