COMPSCI 105 S2 2014 Principles of Computer Science Linked Lists 2.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Why not just use Arrays? Java ArrayLists.
Singly linked lists Doubly linked lists
Chapter 17 Linked Lists.
Linked Lists Chapter 4.
DATA STRUCTURES USING C++ Chapter 5
Computer Science 112 Fundamentals of Programming II Queues and Priority Queues.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Data Structures: A Pseudocode Approach with C
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
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
Data Structures Using C++ 2E
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Computer Science 112 Fundamentals of Programming II Introduction to Stacks.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
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.
Computer Science 112 Fundamentals of Programming II Interfaces and Implementations.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Iterators, Linked Lists, MapReduce, Dictionaries, and List Comprehensions... OH MY! Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
COMPSCI 105 SS 2015 Principles of Computer Science Linked Lists 1.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
Chapter 5 Linked Lists II
Data Structures Using C++1 Chapter 5 Linked Lists.
COMPSCI 105 SS 2015 Principles of Computer Science 09 ADT & Stacks.
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.
COMPSCI 105 SS 2015 Principles of Computer Science
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Tirgul 10. What We Will See Today:  Linked lists  Graphs  Trees  Iterators and Generators.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
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.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
LINKED LISTS.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
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.
C++ Programming:. Program Design Including
The List ADT.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
CS-104 Final Exam Review Victor Norman.
Advanced Linked lists Doubly Linked and Circular Lists
EEL 4854 IT Data Structures Linked Lists
CSCI 3333 Data Structures Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linear Data Structures Chapter 3
CS190/295 Programming in Python for Life Sciences: Lecture 6
Linked Lists.
CS212D: Data Structures Week 5-6 Linked List.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Python LinkedLists.
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
Data Structures & Algorithms
Programming II (CS300) Chapter 07: Linked Lists
Introduction to Computer Science
Presentation transcript:

COMPSCI 105 S Principles of Computer Science Linked Lists 2

Agenda & Reading  Agenda  Introduction  The OrderedList  Operations  Iterators  Linked List Iterators  Extra Reading  Textbook:  Problem Solving with Algorithms and Data Structures  Chapter 3 – The Ordered List Abstract Data Type  Reference:  COMPSCI1052

18.1 Introduction Unordered Vs Ordered  A list is a collection of items where each item holds a relative position with respect to the others.  It must maintain a reference to the first node (head)  It is commonly known as a linked list  Unordered Vs Ordered  Unordered meaning that the items are not stored in a sorted fashion. 10COMPSCI , 26, 93, 17, 77 and 31 17, 26, 31, 54, 77 and 93 Unordered Ordered

18.2 The OrderedList The OrderedList  The structure of an ordered list is a collection of items where each item holds a relative position that is based upon some underlying characteristic of the item.  i.e. either ascending or descending 10COMPSCI1054 my_orderedlist = OrderedList() num_list = [77, 17, 26, 31, 93, 54] for num in num_list: my_orderedlist.add(num) my_orderedlist = OrderedList() num_list = [77, 17, 26, 31, 93, 54] for num in num_list: my_orderedlist.add(num) Integers are in ascending order

18.2 The OrderedList The OrderedList ADT  Constructor  is_empty()  size()  remove()  add()  search() 10COMPSCI1055 Same as those of UnorderedList Different!

18.2 The OrderedList The OrderedList Abstract Data Type  What are the operations which can be used with an OrderedList Abstract Data?  creates a new list that is empty.  It needs no parameters and returns an empty list.  add(item) adds a new item to the list.  It needs the item and returns nothing. Assume the item is not already in the list.  remove(item) removes the item from the list.  It needs the item and modifies the list. Assume the item is present in the list.  search(item) searches for the item in the list.  It needs the item and returns a boolean value.  is_empty() tests to see whether the list is empty.  It needs no parameters and returns a boolean value.  size() returns the number of items in the list.  It needs no parameters and returns an integer. No checking is done in the implementation! 17COMPSCI1056

18.3 Operations Inserting a Node (Review)  UnorderedList  Only insert new item at the beginning of a linked list  Create a new Node and store the new data into it  Connect the new node to the linked list by changing references  change the next reference of the new node to refer to the old first node of the list  modify the head of the list to refer to the new node 10COMPSCI new_node.set_next(self.head) self.head = new_node new_node.set_next(self.head) self.head = new_node new_node = Node(item)

18.3 Operations Inserting a Node - OrderedList  OrderedList  Create a new node and store the new data in it  Determine the point of insertion  Insert at the beginning of a linked list, OR  Insert at the middle of a linked list  Use the prev reference  Connect the new node to the linked list by changing references 10COMPSCI1058 new_node = Node(item) Must determine the point of insertion

18.3 Operations Determine the point of insertion  Starting point:  current = self.head  previous = None  stop = False 10COMPSCI1059 while current != None and not stop: if current.get_data() > item: stop = True else: previous = current current = current.get_next() while current != None and not stop: if current.get_data() > item: stop = True else: previous = current current = current.get_next() Integers are in ascending order my_orderedlist.add(49) previous 26 < < 4954 > < 49

18.3 Operations Inserting a Node - OrderedList  Insert at the beginning of a linked list  Insert at the middle of a linked list  change the next reference of the new node to refer to the current node of the list  modify the next reference of the prev node to refer to the new node COMPSCI10510 new_node.set_next(self.head) self.head = new_node new_node.set_next(self.head) self.head = new_node new_node.set_next(current) previous.set_next(new_node)

18.3 Operations Searching an Item  Searches for the item in the list. Returns a Boolean.  Examples: 10COMPSCI10511 print (my_linked_list.search(31)) print (my_linked_list.search(39)) current True False

18.3 Operations Searching an item  To search an item in a linked list:  set a pointer to be the same address as head,  process the data in the node, (search)  move the pointer to the next node, and so on.  Loop stops either 1) found the item, or 2) when the next pointer is None, or 3) value in the node is greater than the item that we are searching 10COMPSCI10512 current = self.head while current != None: if current.get_data() == item: return True elif current.get_data() > item: return False else: current = current.get_next() return False current = self.head while current != None: if current.get_data() == item: return True elif current.get_data() > item: return False else: current = current.get_next() return False

18.3 Operations UnorderedList Vs OrderedList UnorderedListOrderedList is_emptyO(1) sizeO(1) with count variable addO(1)O(n) removeO(n) searchO(n) 10COMPSCI10513

18.4 Iterators Iterators  Traversals are very common operations, especially on containers.  Python’s for loop allows programmer to traverse items in strings, lists, tuples, and dictionaries:  Python compiler translates for loop to code that uses a special type of object called an iterator  An iterator guarantees that each element is visited exactly once.  It is useful to be able to traverse an UnorderedList or an OrderedList, i.e., visit each element exactly once. 10COMPSCI10514

18.4 Iterators An Example: Python List Traversals  All of Python's standard built-in sequence types support iteration  The for-in statement makes it easy to loop over the items in a list:  The list object supports the iterator protocol.  To explicitly create an iterator, use the built-in iter function: 10COMPSCI10515 for item in num_list: print(item) for item in num_list: print(item) i = iter(num_list) print(next(i)) # fetch first value print(next(i)) i = iter(num_list) print(next(i)) # fetch first value print(next(i))

18.4 Iterators Create your own Iterator  You can create your own iterators if you write a function to generate the next item. You need to add:  Constructor  The __iter__() method, which must return the iterator object, and  the __next__() method, which returns the next element from a sequence.  For example: 10COMPSCI10516 my_iterator = NumberIterator(11, 20) for num in my_iterator: print(num, end=" ") my_iterator = NumberIterator(11, 20) for num in my_iterator: print(num, end=" ")

18.4 Iterators The NumberIterator Class  Constructor, __iter__(), __next__ 10COMPSCI10517 class NumberIterator: def __init__(self, low, high): self.current = low self.high = high def __iter__(self): return self def __next__(self): if self.current > self.high: raise StopIteration else: self.current += 1 return self.current - 1 class NumberIterator: def __init__(self, low, high): self.current = low self.high = high def __iter__(self): return self def __next__(self): if self.current > self.high: raise StopIteration else: self.current += 1 return self.current - 1 Raise this error to tell consumer to stop

18.5 Linked List Iterators Linked List Traversals  Now, we would like to traverse an UnorderedList or an OrderedList using a for-loop, i.e., visit each element exactly once.  However, we will get the following error:  Solution:  Create an iterator class for the linked list.  Add the __iter()__ method to return an instance of the LinkedListIterator class. 10COMPSCI10518 for num in my_linked_list: print(num, end=" ") for num in my_linked_list: print(num, end=" ") for num in my_linked_list: TypeError: 'UnorderedList' object is not iterable for num in my_linked_list: TypeError: 'UnorderedList' object is not iterable

18.5 Linked List Iterators The LinkedListIterator  The UnorderedList class: 10COMPSCI10519 class LinkedListIterator: def __init__( self, head): self.current = head def __next__( self ): if self.current != None: item = self.current.get_data() self.current = self.current.get_next() return item else : raise StopIteration class LinkedListIterator: def __init__( self, head): self.current = head def __next__( self ): if self.current != None: item = self.current.get_data() self.current = self.current.get_next() return item else : raise StopIteration class UnorderedList:... def __iter__(self): return LinkedListIterator(self.head) class UnorderedList:... def __iter__(self): return LinkedListIterator(self.head)

Exercise  What is the content inside the UnorderedList and OrderedList after executing the following code fragment? 10COMPSCI10520 name_list = ["Gill", "Tom", "Eduardo", "Raffaele", "Serena", "Bella"] my_unorderedlist = UnorderedList() for name in name_list: my_unorderedlist.add(name) my_unorderedlist = UnorderedList() for name in name_list: my_unorderedlist.add(name) my_orderedlist = OrderedList() for name in name_list: my_orderedlist.add(name) my_orderedlist = OrderedList() for name in name_list: my_orderedlist.add(name)