Fundamentals of Python: From First Programs Through Data Structures

Slides:



Advertisements
Similar presentations
Fall 2002CMSC Discrete Structures1 Enough Mathematical Appetizers! Let us look at something more interesting: Algorithms.
Advertisements

Chapter 22 Implementing lists: linked implementations.
Singly linked lists Doubly linked lists
Pointers.
Fundamentals of Python: From First Programs Through Data Structures
Disk Storage, Basic File Structures, and Hashing
Chapter 12: File System Implementation
DATA STRUCTURES USING C++ Chapter 5
CHP-5 LinkedList.
FILE SYSTEM IMPLEMENTATION
Hashing / Hash tables Chapter 20 CSCI 3333 Data Structures.
Linked Lists CENG 213 Data Structures.
Fall 2002CMSC Discrete Structures1 You Never Escape Your… Relations.
Fall 2002CMSC Discrete Structures1 Now it’s Time for… RecurrenceRelations.
Data Structures1 Basic Data Structures Elementary Structures Arrays Lists Search Structures Binary search Tree Hash Tables Sequence Structures Stacks Queues.
Computer Science 112 Fundamentals of Programming II Overview of Collections.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 10 Introduction to Arrays
Computer Science 112 Fundamentals of Programming II Lists.
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.
COMPSCI 105 S Principles of Computer Science Linked Lists 2.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Memory Management A memory manager should take care of allocating memory when needed by programs release memory that is no longer used to the heap. Memory.
METU Department of Computer Eng Ceng 302 Introduction to DBMS Disk Storage, Basic File Structures, and Hashing by Pinar Senkul resources: mostly froom.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 13 Disk Storage, Basic File Structures, and Hashing.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 3: Arrays, Linked Lists, and Recursion
Stacks, Queues, and Deques
Data Structures Using C++ 2E
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 17 Disk Storage, Basic File Structures, and Hashing.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Data Structures Using C++ 2E
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Data Structures Using Java1 Chapter 4 Linked Lists.
Built-in Data Structures in Python An Introduction.
File Structures. 2 Chapter - Objectives Disk Storage Devices Files of Records Operations on Files Unordered Files Ordered Files Hashed Files Dynamic and.
Fundamentals of Python: From First Programs Through Data Structures Chapter 13 Collections, Arrays, and Linked Structures.
CE Operating Systems Lecture 17 File systems – interface and implementation.
Linked List by Chapter 5 Linked List by
Fundamentals of Java Lesson 13: Linear Collections: Lists.
Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
LINKED LISTS.
Linked List ADT used to store information in a list
Lecture 6 of Computer Science II
Fundamentals of Programming II Overview of Collections
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Fundamentals of Programming II Working with Arrays
Disk Storage, Basic File Structures, and Hashing
Disk Storage, Basic File Structures, and Buffer Management
Topics Introduction to File Input and Output
Linked Lists.
Dynamic Data Structures and Generics
Data Structures & Algorithms
Topics Introduction to File Input and Output
Introduction to Computer Science
Presentation transcript:

Fundamentals of Python: From First Programs Through Data Structures Chapter 16 Linear Collections: Lists

Objectives After completing this chapter, you will be able to: Explain the difference between index-based operations on lists and position-based operations on lists Analyze the performance trade-offs between an array-based implementation and a linked implementation of index-based lists Fundamentals of Python: From First Programs Through Data Structures

Objectives (continued) Analyze the performance trade-offs between an array-based implementation and a linked implementation of positional lists Create and use an iterator for a linear collection Develop an implementation of a sorted list Fundamentals of Python: From First Programs Through Data Structures

Overview of Lists A list supports manipulation of items at any point within a linear collection Some common examples of lists: Recipe, which is a list of instructions String, which is a list of characters Document, which is a list of words File, which is a list of data blocks on a disk Items in a list are not necessarily sorted Items in a list are logically contiguous, but need not be physically contiguous in memory Fundamentals of Python: From First Programs Through Data Structures

Overview of Lists (continued) Head: First item in a list Tail: Last item in a list Index: Each numeric position (from 0 to length – 1) Fundamentals of Python: From First Programs Through Data Structures

Overview of Lists (continued) Fundamentals of Python: From First Programs Through Data Structures

Using Lists Universal agreement on the names of the fundamental operations for stacks and queues but for lists, there are no such standards The operation of putting a new item in a list is sometimes called “add” and sometimes “insert” Broad categories of operations on lists: Index-based operations Content-based operations Position-based operations Fundamentals of Python: From First Programs Through Data Structures

Index-Based Operations Index-based operations manipulate items at designated indices within a list In array-based lists, these provide random access From this perspective, lists are called vectors or sequences Fundamentals of Python: From First Programs Through Data Structures

Content-Based Operations Content-based operations are based not on an index, but on the content of a list Usually expect an item as an argument and do something with it and the list Fundamentals of Python: From First Programs Through Data Structures

Position-Based Operations Position-based operations: Performed relative to currently established position or cursor within a list Allow user to navigate the list by moving this cursor In some programming languages, a separate object called an iterator provides these operations Places in which a positional list’s cursor can be: Just before the first item Between two adjacent items Just after the last item Fundamentals of Python: From First Programs Through Data Structures

Position-Based Operations (continued) Fundamentals of Python: From First Programs Through Data Structures

Position-Based Operations (continued) When a positional list is first instantiated or when it becomes empty, its cursor is undefined Fundamentals of Python: From First Programs Through Data Structures

Position-Based Operations (continued) Fundamentals of Python: From First Programs Through Data Structures

Position-Based Operations (continued) Fundamentals of Python: From First Programs Through Data Structures

Position-Based Operations (continued) Fundamentals of Python: From First Programs Through Data Structures

Position-Based Operations (continued) Fundamentals of Python: From First Programs Through Data Structures

Position-Based Operations (continued) Fundamentals of Python: From First Programs Through Data Structures

Interfaces for Lists Fundamentals of Python: From First Programs Through Data Structures

Interfaces for Lists (continued) Fundamentals of Python: From First Programs Through Data Structures

Applications of Lists Lists are probably the most widely used collections in computer science In this section, we examine two important applications: Heap-storage management Disk file management Fundamentals of Python: From First Programs Through Data Structures

Heap-Storage Management Object heap: Area of memory from which PVM allocates segments for new data objects When an object no longer can be referenced from a program, PVM can return that object’s memory segment to the heap for use by other objects Heap-management schemes can have a significant impact on an application’s overall performance Especially if the application creates and abandons many objects during the course of its execution Fundamentals of Python: From First Programs Through Data Structures

Heap-Storage Management (continued) Contiguous blocks of free space on the heap can be linked together in a free list Scheme has two defects: Over time, large blocks on the free list become fragmented into many smaller blocks Searching free list for blocks of sufficient size can take O(n) running time (n is the number of blocks in list) Solutions: Have garbage collector periodically reorganize free list by recombining adjacent blocks To reduce search time, multiple free lists can be used Fundamentals of Python: From First Programs Through Data Structures

Organization of Files on a Disk Major components of a computer’s file system: A directory of files, the files, and free space The disk’s surface is divided into concentric tracks, and each track is further subdivided into sectors (t, s) specifies a sector’s location on the disk A file system’s directory is organized as a hierarchical collection Assume it occupies the first few tracks on the disk and contains an entry for each file Fundamentals of Python: From First Programs Through Data Structures

Organization of Files on a Disk (continued) Fundamentals of Python: From First Programs Through Data Structures

Organization of Files on a Disk (continued) A file might be completely contained within a single sector or might span several sectors Usually, the last sector is only partially full The sectors that make up a file do not need to be physically adjacent Each sector except last one ends with a pointer to the sector containing the next portion of the file Unused sectors are linked together in a free list A disk system’s performance is optimized when multisector files are not scattered across the disk Fundamentals of Python: From First Programs Through Data Structures

Implementation of Other ADTs Lists are frequently used to implement other collections, such as stacks and queues Two ways to do this: Extend the list class For example, to implement a sorted list Use an instance of the list class within the new class and let the list contain the data items For example, to implement stacks and queues ADTs that use lists inherit their performance characteristics Fundamentals of Python: From First Programs Through Data Structures

Indexed List Implementations We develop array-based and linked implementations of the IndexedList interface and a linked implementation of the PositionalList interface Fundamentals of Python: From First Programs Through Data Structures

An Array-Based Implementation of an Indexed List An ArrayIndexedList maintains its data items in an instance of the Array class Uses instance variable to track the number of items Initial default capacity is automatically increased when append or insert needs room for a new item Fundamentals of Python: From First Programs Through Data Structures

A Linked Implementation of an Indexed List The structure used for a linked stack, which has a pointer to its head but not to its tail, would be an unwise choice for a linked list The singly linked structure used for the linked queue (with head and tail pointers) works better append puts new item at tail of linked structure Fundamentals of Python: From First Programs Through Data Structures

Time and Space Analysis for the Two Implementations The running times of the IndexedList methods can be determined in the following ways: Examine the code and do the usual sort of analysis Reason from more general principles We take the second approach Fundamentals of Python: From First Programs Through Data Structures

Time and Space Analysis for the Two Implementations (continued) Fundamentals of Python: From First Programs Through Data Structures

Time and Space Analysis for the Two Implementations (continued) Space requirement for array implementation is capacity + 2, which comes from: An array that can hold capacity references A reference to the array A variable for the number of items Space requirement for the linked implementation is 2n + 3, which comes from: n data nodes; each node containing two references Variables that point to the first and last nodes Fundamentals of Python: From First Programs Through Data Structures

Implementing Positional Lists Positional lists use either arrays or linked structures In this section, we develop a linked implementation Array-based version is left as an exercise for you Fundamentals of Python: From First Programs Through Data Structures

The Data Structures for a Linked Positional List We don’t use a singly linked structure to implement a positional list because it provides no convenient mechanism for moving to a node’s predecessor Code to manipulate a list can be simplified if a sentinel node is added at the head of the list Points forward to what was the first node and backward to what was the last node Fundamentals of Python: From First Programs Through Data Structures

The Data Structures for a Linked Positional List (continued) The head pointer now points to the sentinel node Resulting structure resembles circular linked structure studied earlier Fundamentals of Python: From First Programs Through Data Structures

The Data Structures for a Linked Positional List (continued) Fundamentals of Python: From First Programs Through Data Structures

Methods Used to Navigate from Beginning to End Purpose of hasNext is to determine whether next can be called to move the cursor to the next item first moves cursor to first item, if there is one Also resets lastItemPos pointer to None, to prevent replace and remove from being run at this point Fundamentals of Python: From First Programs Through Data Structures

Methods Used to Navigate from Beginning to End (continued) Fundamentals of Python: From First Programs Through Data Structures

Methods Used to Navigate from Beginning to End (continued) next cannot be run if hasNext is False Raises an exception if this is the case Otherwise, sets lastItemPos to cursor’s node, moves cursor to next node, and returns item at lastItemPos Fundamentals of Python: From First Programs Through Data Structures

Methods Used to Navigate from Beginning to End (continued) Fundamentals of Python: From First Programs Through Data Structures

Methods Used to Navigate from End to Beginning Where should the cursor be placed to commence a navigation from the end of the list to its beginning? When previous is run, cursor should be left in a position where the other methods can appropriately modify the linked structure last places the cursor at the header node instead Header node is node after the last data node hasPrevious returns True when cursor’s previous node is not the header node Fundamentals of Python: From First Programs Through Data Structures

Insertions into a Positional List Scenarios in which insertion can occur: Method hasNext returns False  new item is inserted after the last one Method hasNext returns True  new item is inserted before the cursor’s node Fundamentals of Python: From First Programs Through Data Structures

Removals from a Positional List remove removes item most recently returned by a call to next or previous Should not be called right after insert/remove Uses lastItemPos to detect error or locate node Fundamentals of Python: From First Programs Through Data Structures

Time and Space Analysis of Positional List Implementations There is some overlap in the analysis of positional lists and index-based lists, especially with regard to memory usage Use of a doubly linked structure adds n memory units to the tally for the linked implementation The running times of all of the methods, except for __str__, are O(1) Fundamentals of Python: From First Programs Through Data Structures

Iterators 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 Fundamentals of Python: From First Programs Through Data Structures

Iterators (continued) If every collection included an iterator, you could define a constructor that creates an instance of one type of collection from items in any other collection: Users of ArrayStack can run code such as: s = ArrayStack(aQueue) s = ArrayStack(aString) Fundamentals of Python: From First Programs Through Data Structures

Using an Iterator in Python Python uses an iterator to access items in lyst Fundamentals of Python: From First Programs Through Data Structures

Using an Iterator in Python (continued) Although there is no clean way to write a normal loop using an iterator, you can use a try-except statement to handle the exception The for loop is just “syntactic sugar,” or shorthand, for an iterator-based loop Fundamentals of Python: From First Programs Through Data Structures

Implementing an Iterator Define method to be called when iter function is run: __iter__ Expects only self as an argument Automatically builds and returns a generator object Fundamentals of Python: From First Programs Through Data Structures

Case Study: Developing a Sorted List Request: Develop a sorted list collection Analysis: Client should be able to use the basic collection operations (e.g., str, len, isEmpty), as well as the index-based operations [] for access and remove and the content-based operation index An iterator can support position-based traversals Fundamentals of Python: From First Programs Through Data Structures

Case Study: Developing a Sorted List (continued) Fundamentals of Python: From First Programs Through Data Structures

Case Study: Developing a Sorted List (continued) Design: Because we would like to support binary search, we develop just an array-based implementation, named ArraySortedList Fundamentals of Python: From First Programs Through Data Structures

Case Study: Developing a Sorted List (continued) Checking some preconditions and completing the index method are left as exercises for you Fundamentals of Python: From First Programs Through Data Structures

Summary A list is a linear collection that allows users to insert, remove, access, and replace elements at any position Operations on lists are index-based, content-based, or position-based An index-based list allows access to an element at a specified integer index A position-based list lets the user scroll through it by moving a cursor Fundamentals of Python: From First Programs Through Data Structures

Summary (continued) List implementations are based on arrays or on linked structures A doubly linked structure is more convenient and faster for a positional list than a singly linked structure An iterator is an object that allows a user to traverse a collection and visit its elements In Python, a collection can be traversed with a for loop if it supports an iterator A sorted list is a list whose elements are always in ascending or descending order Fundamentals of Python: From First Programs Through Data Structures