Singly Linked Lists 2014 Goodrich Tamassia Goldwasser Singly

  • Slides: 17
Download presentation
Singly Linked Lists © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 1

Singly Linked Lists © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 1

Singly Linked List A singly linked list is a concrete data structure consisting of

Singly Linked List A singly linked list is a concrete data structure consisting of a sequence of nodes, starting from a head pointer Each node stores n head n next element node element link to the next node A B © 2014 Goodrich, Tamassia, Goldwasser C Singly Linked Lists D 2

Review: struct in C struct Node { char element[4]; struct Node *next; }; typedef

Review: struct in C struct Node { char element[4]; struct Node *next; }; typedef struct Node NODE; … NODE *my. Node = (NODE *) malloc( sizeof(NODE) ); Is my. Node a. the created node, or b. a pointer to the created node? © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 3

Inserting at the Head • Allocate new node • Insert new element • Have

Inserting at the Head • Allocate new node • Insert new element • Have new node point to old head • Update head to point to new node © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 4

Inserting at the Tail • Allocate a new node • Insert new element •

Inserting at the Tail • Allocate a new node • Insert new element • Have new node point to null • Have old last node point to new node • Update tail to point to new node © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 5

Removing at the Head • Update head to point to next node in the

Removing at the Head • Update head to point to next node in the list • Free the remove node © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 6

Removing at the Tail • Removing at the tail of a singly linked list

Removing at the Tail • Removing at the tail of a singly linked list is not efficient! • There is no constant-time way to update the tail to point to the previous node © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 7

List ADT (Abstract Data Type) Operations: n n n Insert an item Delete an

List ADT (Abstract Data Type) Operations: n n n Insert an item Delete an item Replace the i-th item © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 8

List ADT (Abstract Data Type) Operations: n n n Insert an item Delete an

List ADT (Abstract Data Type) Operations: n n n Insert an item Delete an item Replace the i-th item Representations: n n Sequential (arrays) Linked (linked lists) © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 9

List ADT (size N, # of steps in the worst case) Sequential Representation (arrays)

List ADT (size N, # of steps in the worst case) Sequential Representation (arrays) Linked Representation (linked lists) Insert an item Delete an item Replace the i-th item © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 10

List ADT of size N # of steps in the worst case Sequential Representation

List ADT of size N # of steps in the worst case Sequential Representation (arrays) Linked Representation (linked lists) Insert an item Shift N items O(N) Traverse N items O(N) Delete an item Shift N-1 items O(N) Traverse N items O(N) Direct access to a[i] O(1) Traverse N items O(N) Replace the i-th item © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 11

Space comparison List of size N n maximum Capacity Max. Size in array Assume

Space comparison List of size N n maximum Capacity Max. Size in array Assume n n data/element uses 4 bytes Next pointer uses 4 bytes Array Linked list Space © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 12

Space comparison List of size N n maximum Capacity Max. Size in array Assume

Space comparison List of size N n maximum Capacity Max. Size in array Assume n n data/element uses 4 bytes Next pointer uses 4 bytes Space © 2014 Goodrich, Tamassia, Goldwasser Array Linked list 4 * Max. Size 8*N+4 Singly Linked Lists 13

Circularly Linked Lists © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 14

Circularly Linked Lists © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 14

tail (but no head) © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 15

tail (but no head) © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 15

Rotation What are the instructions? © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 16

Rotation What are the instructions? © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 16

Insertion at (head) What are the instructions? © 2014 Goodrich, Tamassia, Goldwasser Singly Linked

Insertion at (head) What are the instructions? © 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 17