COMPSCI 105 SS 2015 Principles of Computer Science

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Linked Lists.
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
Data Structures Using C++
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.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Computer Science 112 Fundamentals of Programming II Lists.
COMPSCI 105 S Principles of Computer Science Linked Lists 2.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
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, Third Edition Chapter 17: Linked Lists.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
COMPSCI 105 S Principles of Computer Science Linked Lists 1.
Chapter 3: Arrays, Linked Lists, and Recursion
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
Data Structures Using Java1 Chapter 4 Linked Lists.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 18 Linked Lists, Stacks, Queues, and Priority Queues.
COMPSCI 105 SS 2015 Principles of Computer Science Linked Lists 1.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
Data Structures Using C++1 Chapter 5 Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
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.
Tirgul 10. What We Will See Today:  Linked lists  Graphs  Trees  Iterators and Generators.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Linear Data Structures
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
CSC 205 Programming II Lecture 15 Linked List – Other Variations.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
1 Lecture 15: Big O Notation (Wednesday) Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science Test See Web for Details Don’t be deleted!
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.
Chapter 16: Linked Lists.
Fundamentals of Programming II Linked Lists
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
CMSC202 Computer Science II for Majors Lecture 12 – Linked Lists
Linked List Variations
Advanced Linked lists Doubly Linked and Circular Lists
EEL 4854 IT Data Structures Linked Lists
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Circularly Linked Lists
Arrays and Linked Lists
Linear Data Structures Chapter 3
Computer Science 210 Computer Organization
Node Applications in Abstract Data Types
Python LinkedLists.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Revised based on textbook author’s notes.
Linked Lists Chapter 5 (continued)
Presentation transcript:

COMPSCI 105 SS 2015 Principles of Computer Science Linked Lists 2

Agenda & Reading Agenda Extra Reading: Variations of Linked Lists Singly Linked Lists with Head and Tail Doubly Linked Lists with Dummy head node Extra Reading: http://en.literateprograms.org/Singly_linked_list_(Python) COMPSCI105 Lecture 13

1 Singly Linked Lists Singly Linked List with Head and Tail In some singly linked list implementations, it is handy to have a reference to the last node of the list. Allow more efficient access (i.e. insertion) at the end of Linked List Useful for queue-like structure, e.g. a waiting list COMPSCI105 Lecture 13

1 Singly Linked Lists Singly Linked List with Head and Tail Cases: Insertion General case: Insert a new node to the beginning of a list Insert a new node to the end of a list Insert a new node to the middle of a list An empty list: Insert a new node to an empty list Deletion Remove a node at the beginning of a list Remove a node at the end of a list Remove a node from the middle of a list Only one node left in the list: Remove the only one node from a list NEW! NEW! NEW! NEW! COMPSCI105 Lecture 13

1 Singly Linked Lists Inserting a Node : add_to_tail Add an item to the end of the Linked List: General case: (non-empty list) Steps: create a new node and place the item as its data change the next reference of the old last node of the list to refer to the new node modify the tail to refer to the new node increment the count new_node = Node(item) self.tail.set_next(new_node) 1 Count: 4 head tail 5 self.tail = new_node 1 3 2 self.count += 1 COMPSCI105 Lecture 13

1 Singly Linked Lists Inserting a Node : add_to_head/tail Insert a new node to an empty list Steps: create a new node and place the item as its data change both the head and tail to refer to the new node increment the count Same steps in both cases Count: 0 head tail 1 new_node = Node(item) 2 self.head = new_node 3 1 1 if self.tail == None: self.tail = new_node self.count += 1 COMPSCI105 Lecture 13

1 Singly Linked Lists Deleting a Node: remove_from_tail Remove an item from the end of the Linked List: General case (non-empty list): Steps: locate the previous node modify the next of the pervious node to None modify the tail to refer to the previous node decrement the count prev = self.head while (prev.get_next() != self.tail): prev = prev.get_next() Count: 4 head tail 3 prev.set_next(None) prev self.tail = prev self.count -= 1 COMPSCI105 Lecture 13

1 Singly Linked Lists Deleting a Node: remove_from_head/tail Remove a node from a list with one element Steps: modify the head and tail to refer to None decrement the count Same steps in both cases Count: 1 head tail self.head = self.head.get_next() if self.head == None: self.tail = None 1 self.count += 1 COMPSCI105 Lecture 13

Comparing Implementations Python List Singly Linked List is_empty O(1) : if len(my_list) == 0 O(1) size O(1) : len(my_list) O(1) with count variable O(n) with count variable addTohead O(n) : insert(0, item) addToTail O(1) : append O(1) with tail reference O(n) without tail add O(n) : insert(index, item) O(n) removeFromHead O(n) : pop(0) removeFromTail O(1) : pop() O(n) even with tail remove O(n) : pop(n) Locate the prev node COMPSCI105 Lecture 13

1 Singly Linked Lists Exercise 1 What is the output of the following code fragment? my_list = LinkedList() for x in range(1,10): my_list.add_to_head(x) for num in my_list: print(num, end=" ") print() for x in range(1,9): my_list.remove_from_tail() for num in my_list: print(num, end=" ") COMPSCI105 Lecture 13

2 Doubly Linked Lists Doubly Linked Lists A common variation on linked lists is to have two pointers to other nodes within each node: one to the next node on the list, and one to the previous node. Doubly-linked lists make some operations, such as deleting a tail node, more efficient. Double-linked lists can have iterators for efficient forward and backward traversals. COMPSCI105 Lecture 13

2 Doubly Linked Lists Doubly Linked List Each NodeDLL references both its predecessor and its successor Each object in a doubly linked list will contain three member variables: data : value stored in this node next : refer to the next node in the list prev : refer to the previous node in the list xi next prev data class NodeDLL: def __init__(self, init_data, next_node=None, prev_node=None): … COMPSCI105 Lecture 13

2 Doubly Linked Lists The NodeDLL class class NodeDLL: def __init__(self, init_data, next_node=None, prev_node=None): ... def get_data(self): return self.data def get_prev(self): return self.prev def get_next(self): return self.next def set_data(self, new_data): self.data = newdata def set_next(self, new_next): self.next = new_next def set_prev(self, new_prev): self.prev = new_prev COMPSCI105 Lecture 13

2 Doubly Linked Lists Doubly Linked List A head reference is used to reference the first node in the list. A tail reference points to the last node. Examples: A doubly linked list with 4 nodes: An Empty list: head 12 36 47 57 tail head tail COMPSCI105 Lecture 13

2 Doubly Linked Lists Inserting a Node def add_before(self, item, curr): Add an item to the middle of the Linked List: General case: (non-empty list) Steps: create a new node and place the item as its data, set the next of the new node to refer to the curr, set the prev of the new node to refer to the previous node of curr. modify the prev of the curr node to refer to the new node modify the next of the node that is to precede the new node to refer to the new node 12 36 47 57 27 curr new_node = NodeDLL(item, curr, curr.get_prev()) curr.set_prev(new_node) COMPSCI105 Lecture 13 new_node.get_prev().set_next(new_node)

2 Doubly Linked Lists Inserting a Node new_node = NodeDLL(item, curr, curr.get_prev()) curr.set_prev(new_node) new_node.get_prev().set_next(new_node) However, we still need to handle insertion in different cases: Case 1: an empty list Case 2: at the beginning of the list Case 3: at the end of the list head 47 57 head 27 curr 47 57 head 67 curr COMPSCI105 Lecture 13

2 Doubly Linked Lists Deleting a Node def remove(self, curr): Remove an item from the middle of the Linked List: General case: (non-empty list) Steps: modify the next of the node that precede curr so that it refers to the node that follows curr; modify the prev of the node that follows curr so that it refers to the node that precedes curr. 12 36 47 57 curr curr.get_prev().set_next(curr.get_next()) curr.get_next().set_prev(curr.get_prev()) COMPSCI105 Lecture 13

2 Doubly Linked Lists Deleting a Node curr.get_prev().set_next(curr.get_next()) curr.get_next().set_prev(curr.get_prev()) Again, we still need to handle deletion in different cases: Case 1: One element Case 2: at the beginning of the list Case 3: at the end of the list head 27 47 57 head curr 47 57 head COMPSCI105 curr Lecture 13

2 Doubly Linked Lists Circular + Dummy Head node How can we simplify the implementation? Use Circular doubly linked list with a dummy head node The prev of the dummy head node refers to the last node The next of the last node refers to the dummy head node Eliminates special cases for insertions and deletions Dummy head node is always present, even when the linked list is empty COMPSCI105 Lecture 13

2 Doubly Linked Lists Inserting a Node def add_before(self, item, curr): At the beginning: At the end: 37 57 27 curr self.add_before(item, self.head.get_next()) 12 47 ... 57 curr self.add_before(item, self.head) COMPSCI105 Lecture 13

2 Doubly Linked Lists Deleting a Node def remove(self, curr): At the beginning: At the end: 12 36 47 ... curr self.remove(self.head.get_next()) 47 57 12 ... curr self.remove(self.head.get_prev()) COMPSCI105 Lecture 13

Comparing Implementations Python List Singly Linked List Circular Doubly with a dummy head is_empty O(1) size O(1) with count addTohead O(n) addToTail O(1) with tail O(n) without add removeFromHead removeFromTail O(n) even with tail remove Locate the prev node COMPSCI105 Lecture 13

2 Doubly Linked Lists Exercise 2 What is the output of the following code fragment? from LinkedListDLL import LinkedListDLL def Test_DoublyLinkedlist(): my_list = LinkedListDLL() for x in range(1,10): my_list.add_to_head(x) print(my_list.size()) for x in range(1,9): my_list.remove_from_tail() print() COMPSCI105 Lecture 13