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

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

Upload: dinhkhue

Post on 16-May-2019

257 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

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 StructuresLinked Lists

Page 2: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: 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

Page 3: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: Data Structures

Linked List Operations

• Basic operations include: - insert / add- remove

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

3

Page 4: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: 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

Page 5: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: 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

Page 6: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: Data Structures

null

Linked List Insertion

6

head

Insert Value: A

NULLA

tail

Page 7: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: Data Structures

null

Linked List Insertion

7

head

Insert Value: B

nullA

tail

NULLB

Page 8: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: Data Structures

null

Linked List Insertion

8

head

Insert Value: C

nullA

tail

nullB NULLC

Page 9: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: Data Structures

null

Linked List Removal

9

head

Remove First Value:

nullA

tail

nullB NULLC

Page 10: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: 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

Page 11: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: Data Structures

Linked List Implementation

11

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

}

Page 12: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: 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 newNodetail.next = newNode;tail = newNode;

}

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

Page 13: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: 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 newNodeif (isEmpty()) {

head = tail = newNode;} else {

tail.next = newNode;tail = newNode;

}}

Fixed insert method

Page 14: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: 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 newNodenewNode.next = head;head = newNode;

}

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

Page 15: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: 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 newNodenewNode.next = head;

if (!isEmpty()) {head = newNode;

} else {head = tail = newNode;

}}

Page 16: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: 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;

}}

Page 17: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: 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

17

Page 18: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: 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

Page 19: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: 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 newNodetail.next = newNode;tail = newNode;

}

Page 20: CS350: Data Structures Linked Lists - ycpcs.github.io file- singly linked lists - doubly linked lists - circular linked lists • Composed of a set of nodes that hold data and contain

CS350: 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 newNodenewNode.next = head.next;head.next = newNode;

}