Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked List Variations

Similar presentations


Presentation on theme: "Linked List Variations"— Presentation transcript:

1 Linked List Variations
October 13, 2017 Hassan Khosravi / Geoffrey Tien

2 Hassan Khosravi / Geoffrey Tien
Extra pointers From the table of operation complexities from the previous slide set, notice how the operations at the end of the list have (relatively) poor complexity, involving a complete traversal of the list we can give ourselves a tail pointer, for very little additional overhead head 1 2 3 4 6 7 tail 9 With a tail pointer, what is the complexity inserting into an ordered list, if the item is known to be larger than the largest so far? What if the item is not necessarily larger than the largest item? new October 13, 2017 Hassan Khosravi / Geoffrey Tien

3 Hassan Khosravi / Geoffrey Tien
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 struct Node { int data; struct Node* prev; struct Node* next; }; head 2 2 2 2 Provides access to the previous and next nodes from a single pointer (e.g. for insertion/removal) but, requires more pointer management in programming October 13, 2017 Hassan Khosravi / Geoffrey Tien

4 Doubly-linked list insertion
After some specified node Node* curr, * temp; ... // move curr into place temp = (Node*) malloc(sizeof(Node)); temp->data = 7; temp->prev = curr; temp->next = curr->next; curr->next->prev = temp; curr->next = temp; head 6 12 4 23 curr temp 7 October 13, 2017 Hassan Khosravi / Geoffrey Tien

5 Doubly-linked list removal
At some specified node Node* curr; ... // move curr to the node to be removed curr->next->prev = curr->prev; curr->prev->next = curr->next; free(curr); curr = NULL; head 6 12 7 4 23 curr October 13, 2017 Hassan Khosravi / Geoffrey Tien

6 Hassan Khosravi / Geoffrey Tien
Exercise Write a function that inserts a node at the front of a (possibly empty) doubly-linked list, with the following prototype: Node* insertHead(Node* front, int value); struct Node { int data; struct Node* prev; struct Node* next; }; October 13, 2017 Hassan Khosravi / Geoffrey Tien

7 Circular linked lists Singly-linked version The last node in the list points back to the first node 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! October 13, 2017 Hassan Khosravi / Geoffrey Tien

8 Circular singly-linked list
Insertion at head 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! Node* ptr, * newnode; ptr = head; while (ptr->next != head) ptr = ptr->next; newnode = (Node*) malloc(sizeof(Node)); newnode->data = 4; newnode->next = head; ptr->next = newnode; head = newnode; ptr head 12 15 7 3 9 newnode 4 October 13, 2017 Hassan Khosravi / Geoffrey Tien

9 Circular doubly-linked list
The last node in the list points to the first node and the first node points to the last node head 6 23 15 What is the time complexity of accessing the last element of a circular doubly-linked list? October 13, 2017 Hassan Khosravi / Geoffrey Tien

10 Hassan Khosravi / Geoffrey Tien
Exercise Write a function that inserts a node at the front of a (possibly empty) circular doubly-linked list, with the following prototype: Node* insertHead(Node* front, int value); struct Node { int data; struct Node* prev; struct Node* next; }; October 13, 2017 Hassan Khosravi / Geoffrey Tien

11 Readings for this lesson
Thareja Chapter 6 Next class: Thareja Chapter 7 (Stacks) Lab reminders next week: Monday, Oct.16 – Lab 3 in-lab activity Tuesday, Oct.17 – Lab 3 quiz Friday, Oct.20 – Lab 3 quiz October 13, 2017 Hassan Khosravi / Geoffrey Tien


Download ppt "Linked List Variations"

Similar presentations


Ads by Google