# CS350: Data Structures Linked Lists - ycpcs. singly linked lists - doubly linked lists - circular linked…

Post on 16-May-2019

212 views

Embed Size (px)

TRANSCRIPT

CS350: Data Structures James Moscola

College Catalog20092011

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

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

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

'GHI&1015?26511??@A9/**/")*&&1015?2D50633

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

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

'GHI

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

CS350: Data Structures

Linked List Operations

Basic operations include: - insert / add- remove

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

3

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

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

CS350: Data Structures

null

Linked List Insertion

6

head

Insert Value: A

NULLA

tail

CS350: Data Structures

null

Linked List Insertion

7

head

Insert Value: B

nullA

tail

NULLB

CS350: Data Structures

null

Linked List Insertion

8

head

Insert Value: C

nullA

tail

nullB NULLC

CS350: Data Structures

null

Linked List Removal

9

head

Remove First Value:

nullA

tail

nullB NULLC

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

CS350: Data Structures

Linked List Implementation

11

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

}

CS350: Data Structures

Linked List Implementation

12

// Inserts at the tail of the list

public void insert (E data) {LinkedListNode newNode = new LinkedListNode();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?

CS350: Data Structures

Linked List Implementation

13

// Inserts at the tail of the list

public void insert (E data) {LinkedListNode newNode = new LinkedListNode();newNode.data = data; // assign data to newNodeif (isEmpty()) {

head = tail = newNode;} else {

tail.next = newNode;tail = newNode;

}}

Fixed insert method

CS350: Data Structures

Linked List Implementation

14

// Inserts at the head of the list

public void insertAtHead (E data) {LinkedListNode newNode = new LinkedListNode();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?

CS350: Data Structures

Linked List Implementation

15

// Inserts at the head of the list

public void insertAtHead (E data) {LinkedListNode newNode = new LinkedListNode();newNode.data = data; // assign data to newNodenewNode.next = head;

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

} else {head = tail = newNode;

}}

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;

}}

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

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

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 newNode = new LinkedListNode();newNode.data = data; // assign data to newNodetail.next = newNode;tail = newNode;

}

CS350: Data Structures

Linked List Implementation

20

// Inserts at the head of the list

public void insertAtHead (E data) {LinkedListNode newNode = new LinkedListNode();newNode.data = data; // assign data to newNodenewNode.next = head.next;head.next = newNode;

}

Recommended