Linked lists January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey - - PowerPoint PPT Presentation

linked lists
SMART_READER_LITE
LIVE PREVIEW

Linked lists January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey - - PowerPoint PPT Presentation

Linked lists January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1 Announcements HW1 due tonight! Only one partner in a group needs to submit See course webpage for details about page/question linking, and adding a partner to


slide-1
SLIDE 1

Linked lists

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1

slide-2
SLIDE 2

Announcements

  • HW1 due tonight!

– Only one partner in a group needs to submit – See course webpage for details about page/question linking, and adding a partner to the submission

  • PA1 out

– GradeScope configuration to follow shortly

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2

slide-3
SLIDE 3

Linked lists

  • Imagine an array partially filled with data

– And we want to insert an item in a particular position in the middle

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3

A motivation

12 15 16 19 22 28 34 37 41 46 17 All of these elements must be shifted over one at a time

Linked lists are a dynamic data structure that can achieve fast insertions/ deletions in the middle

slide-4
SLIDE 4

Linked list nodes

  • A linked list is a dynamic data structure that consists
  • f nodes linked together
  • A node is a data structure that contains

– data – the location of the next node

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 4

slide-5
SLIDE 5

Node pointers

  • A node contains the address of the next node in the list

– In C++ this is recorded as a pointer to a node

  • Nodes are created in dynamic memory

– And their memory locations are not in sequence

  • The data attribute of a node varies depending on what the node

is intended to store

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5

slide-6
SLIDE 6

Linked lists

  • A linked list is a chain of nodes where each node stores the

address of the next node

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 6

7 2 6 8 Start This symbol indicates a null pointer

slide-7
SLIDE 7

Linked list implementation

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7

Node class

class Node { public: int data; Node* next; };

next points to another node, hence its type Node*

  • attributes / members of a particular node can be accessed using the

'.' (dot) operator

– or the '->' (arrow) operator as a shorthand for pointer types

  • equivalent to dereferencing and using dot operator

Node nd; nd.data = 5; Node* p = nd.next; (*p).data = 5; Node* q = *((*p).next).next; Node* r = q->next->next;

slide-8
SLIDE 8

Building a linked list

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 8

Node* a = new Node(7, null);

Assume we have written a parameterized constructor for the Node class 7 a

slide-9
SLIDE 9

Building a linked list

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 9

Node* a = new Node(7, null); a->next = new Node(3, null);

7 a 3

slide-10
SLIDE 10

Traversing a linked list

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 10

Node* a = new Node(7, null); a->next = new Node(3, null); Node* p = a; p = p->next; // go to next node p = p->next;

7 a 3 p

slide-11
SLIDE 11

Linked list insertion

  • Insertion in a singly-linked list requires only updating the next

node reference of the preceding position

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 11

… 15 16 19 22 28 a p

Node* b = new Node(17, p->next);

17 b

p->next = b;

Important! Be aware that sequential nodes are not guaranteed to be found in sequential memory locations!

slide-12
SLIDE 12

Linked list removal

  • Likewise, we can remove a node by updating the pointer of the

preceding node

– but remember to delete the removed node!

… 15 16 19 22 28 a p b

p->next = b->next; delete b;

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 12

slide-13
SLIDE 13

Some linked list variations

  • Suppose we have a basic singly-linked list with a head pointer

as defined above

– operations at the back of the list have (relatively) poor complexity, requiring a traversal – but we can give ourselves a tail pointer for very little overhead, maintained during operations at the back of the list

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 13

class Node { public: int data; Node* next; Node(int d, Node* nd) { data = d; next = nd; } }; class LinkedList { private: Node* head; int length; public: LinkedList(); ... }; class LinkedList { private: Node* head; Node* tail; int length; public: LinkedList(); ... };

slide-14
SLIDE 14

Singly-linked list with tail pointer

  • We can now perform insertion at the back of list in 𝑃 1 time!

– What about insertion in the middle of the list? – What about removal from the back of the list?

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 14

Insertion at back of list

1 2 3 4 6 7 9 head tail new

slide-15
SLIDE 15

Doubly-linked list

  • Node definition contains an additional pointer

– links to previous node in the list, allows traversal or access towards the front of the list

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 15

class Node { public: int data; Node* prev; Node* next; // constructors, etc. };

2 9 6 5 head

  • Provides access to the previous and next nodes from a single

pointer (e.g. for insertion/removal)

– but, requires more pointer management in programming

slide-16
SLIDE 16

Doubly-linked list insertion

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 16

After some specified node

12 4 23 6 head

Node* curr, * temp; ... // use a loop to move curr into place temp = new Node(); temp->data = 7; temp->prev = curr; temp->next = curr->next; curr->next->prev = temp; curr->next = temp;

curr temp 7

slide-17
SLIDE 17

Doubly-linked list removal

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 17

At some specified node

12 4 23 6 head 7

Node* curr; ... // move curr to the node to be removed curr->next->prev = curr->prev; curr->prev->next = curr->next; delete curr; curr = NULL;

curr

slide-18
SLIDE 18

Circular linked lists

  • The last node in the list points back to the first node

– requires no change to the Node definition – circular property is maintained during operations

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 18

Singly-linked version

head 12 15 7

  • How to check when we reach the end in a traversal?

– address of next is the same as the address of the front – but still must be careful to do NULL check on empty list!

slide-19
SLIDE 19

Circular singly-linked list

  • Insertion in the middle of a circular singly-linked list is no

different from inserting into a NULL-terminated list

– what about inserting at the head? – need to iterate a pointer to the last node in the list!

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 19

Insertion at head

head 12 15 9 7 3

Node* ptr, * newnode; ptr = head; while (ptr->next != head) ptr = ptr->next; newnode = new Node(); newnode->data = 4; newnode->next = head; ptr->next = newnode; head = newnode;

newnode 4 ptr

slide-20
SLIDE 20

Circular doubly-linked list

  • The last node in the list points to the first node

– and the first node points to the last node

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 20

head 6 23 15 What is the time complexity of accessing the last element of a circular doubly-linked list?

slide-21
SLIDE 21

Readings for this lesson

  • Carrano & Henry

– Background: 1.4, C1.1-C1.4 – C2.3, C2.5 (Pointers, dynamic arrays) – Chapter 4 (Linked lists)

  • Next class:

– Carrano & Henry: Chapter 6 (ADT Stack)

January 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 21