Dummy Nodes, Doubly Linked Lists and Circular Linked Lists

Slides:



Advertisements
Similar presentations
Linked Lists Chapter 4.
Advertisements

CS 367 – Introduction to Data Structures
Data Structure Lecture-5
Linked Lists CENG 213 Data Structures.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Comparison summary Array based (dynamic) Keeps place for up to 4N elements Each element takes 1 memory places Fast accession time Slow removals and insertion.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 17 Linked List.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 18 Linked Lists, Stacks, Queues, and Priority Queues.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
Page 1 – Spring 2010Steffen Vissing Andersen Software Development with UML and Java 2 SDJ I2, Spring 2010 Agenda – Week 8 Linked List (a reference based.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
COMPSCI 105 SS 2015 Principles of Computer Science
Linked Lists. Array List Issues Painful insert/remove at start/middle.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
Linked Lists and Generics Written by J.J. Shepherd.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
One implementation of the LIST ADT Insert new node before current and new node becomes current (assume new node created) node newNode = new node; head.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Chapter 16: Linked Lists.
Fundamentals of Programming II Linked Lists
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
UNIT – I Linked Lists.
Linked lists.
Linked List Variations
EEL 4854 IT Data Structures Linked Lists
Linked Lists.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 4 Linked Lists
Linked List Sudeshna Sarkar.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Circularly Linked Lists
Recursion & Linked Lists
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
CS212D: Data Structures Week 5-6 Linked List.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Problem Understanding
Chapter 4 Linked Lists.
LINKED LISTS.
General List.
Programming II (CS300) Chapter 07: Linked Lists
Linked Lists.
Linked List Improvements
Linked lists.
Linked Lists.
Linked List Insert After
CMPT 225 Lecture 5 – linked list.
Presentation transcript:

Dummy Nodes, Doubly Linked Lists and Circular Linked Lists

Dummy Nodes

Plain Linked List Linked List has annoying special cases: InsertAt(index, value) If index = 0 InsertFirst(value) Else Node* newNode = new Node(value) Node* temp = head Repeat (index – 1) times temp = temp->next If temp == nullptr error newNode->next = temp->next temp->next = newNode

Plain Linked List Empty List: List with 1 value: Solution : Dummy Node ListNode that head always points to Created at construction, does not count in length Empty List: List with 1 value:

Plain Linked List Eliminates special cases InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head Repeat index times temp = temp->next If temp == nullptr error newNode->next = temp->next temp->next = newNode

Linked List Directional Challenges

BigO's What is BigO for our improved linked list operations? O(1) O(1) With length & tail Get Length Insert Start Insert at InsertEnd Retrieve Retrieve Remove Remove middle First / L ast Middle Beginning End Value Value O(1) O(1) O(n) O(1) O(1) O(n) O(1) O(n)

Retrieve First / Last Value BigO's What is BigO for our improved linked list operations? With length & tail Get Length Insert Start Insert at middle InsertEnd Retrieve First / Last Value Retrieve Middle Value Remove Beginning Remove End  O(1) O(1) O(n)

Reverse Traverse What if we want to reverse through the list? E.g. Print in reverse order

Reverse Traverse What if we want to print the list in reverse order? Iterative: for(i = length-1 … 0) cout << retrieveAt(i) BigO?

Reverse Traverse What if we want to print the list in reverse order? Iterative: for(i = length-1 … 0) //O(n) cout << retrieveAt(i) //O(n) BigO? O(n2)

Lesson Access middle: Avoid retrieveAt(i) in linked list! ArrayList : O(1) LinkedList : O(n) Avoid retrieveAt(i) in linked list!

Doubly Linked Lists

Doubly Linked Node Doubly linked node Pointers to previous and next

Doubly Linked List Doubly Linked List

With Dummies Dummy/Sentinel nodes to avoid special cases

Construct Valid starting state Head/Tail dummy nodes Head Tail prev : null next : tail Tail prev : head next : null Length : 0

Inserting InsertAt(index, value) : Insert(0, 8) previous = head repeat index times previous = previous->next make newNode

Inserting InsertAt(index, value) : Insert(0, 8) previous = head repeat index times previous = previous->next make newNode attach newNode to prev and prev->next

Inserting InsertAt(index, value) : Insert(0, 8) previous = head repeat index times previous = previous->next make newNode attach newNode to prev and prev->next attach newNode neighbors to newNode

Inserting InsertAt(index, value) : Insert(0, 8) previous = head repeat index times previous = previous->next make newNode attach newNode to prev and prev->next attach newNode neighbors to newNode update length

Inserting InsertAt(2, 15) previous = head repeat index times previous = previous->next make newNode attach newNode to prev and prev->next attach newNode neighbors to newNode update length

Inserting InsertAt(2, 15) previous = head repeat index times previous = previous->next make newNode attach newNode to prev and prev->next attach newNode neighbors to newNode update length

Inserting InsertAt(2, 15) previous = head repeat index times previous = previous->next make newNode attach newNode to prev and prev->next attach newNode neighbors to newNode update length

Inserting InsertAt(2, 15) previous = head repeat index times previous = previous->next make newNode attach newNode to prev and prev->next attach newNode neighbors to newNode update length

Removal RemoveAt(index) : RemoveAt(1) if index < 0 or index >= length, error current = head->next repeat index times current = current >next attach current's neighbors to each other delete current update length

Removal RemoveAt(index) : RemoveAt(1) if index < 0 or index >= length, error current = head->next repeat index times current = current >next attach current's neighbors to each other delete current update length

Removal RemoveAt(index) : RemoveAt(1) if index < 0 or index >= length, error current = head->next repeat index times current = current >next attach current's neighbors to each other delete current update length

Removal RemoveAt(index) : RemoveAt(1) if index < 0 or index >= length, error current = head->next repeat index times current = current >next attach current's neighbors to each other delete current update length

Final BigO Big O's for Doubly Linked O(1) O(1) O(n) Get Length Anything at start or end Anything in middle Forward Traversal of list Reverse Traversal of list  O(1) O(1) O(n)

Circular List Circular linked list Last node links back to first Used to model domains, not for efficiency

Doubly Linked Tasks Implement: Constructor InsertAt RemoveAt