The List Type Lecture 9 Hartmut Kaiser

Slides:



Advertisements
Similar presentations
COSC 2007 Data Structures II Chapter 14 External Methods.
Advertisements

Linked Lists Linked Lists Representation Traversing a Linked List
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
The List Type Lecture 9 Hartmut Kaiser
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
M180: Data Structures & Algorithms in Java
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
Practice Quiz Question
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
1 Optimizing Malloc and Free Professor Jennifer Rexford COS 217 Reading: Section 8.7 in K&R book
Skip Lists Michael Oudshoorn. CS351 Software Engineering (AY05)2 Skip Lists Binary Search Trees: O(log n) –If, and only if, the tree is “balanced” Insertion.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
1 Optimizing Malloc and Free Professor Jennifer Rexford
TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Jan Maluszynski - HT Sorting: –Intro: aspects of sorting, different strategies –Insertion.
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
Analysis of Algorithms CS 477/677
Rossella Lau Lecture 4, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 4: C++ and list  Usage of Vector and List  C++
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
C++ Classes and Data Structures Jeffrey S. Childs
March 16 & 21, Csci 2111: Data and File Structures Week 9, Lectures 1 & 2 Indexed Sequential File Access and Prefix B+ Trees.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
COMP20010: Algorithms and Imperative Programming Lecture 4 Ordered Dictionaries and Binary Search Trees AVL Trees.
Elementary Sorting Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Defining New Types Lecture 21 Hartmut Kaiser
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
CSC 211 Data Structures Lecture 13
Data Structure & Algorithm
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
3 Data. Software And Data Data Data element – a single, meaningful unit of data. Name Social Security Number Data structure – a set of related data elements.
Algorithms IS 320 Spring 2015 Sorting. 2 The Sorting Problem Input: –A sequence of n numbers a 1, a 2,..., a n Output: –A permutation (reordering) a 1.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.
Using Sequential Containers Lecture 8 Hartmut Kaiser
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Tree-Structured Indexes Chapter Introduction  As for any index, 3 alternatives for data entries k* :  Data record with key value k   Choice.
CSC 413/513: Intro to Algorithms Hash Tables. ● Hash table: ■ Given a table T and a record x, with key (= symbol) and satellite data, we need to support:
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Mohammed I DAABO COURSE CODE: CSC 355 COURSE TITLE: Data Structures.
Insertion sort Loop invariants Dynamic memory
Algorithmic complexity: Speed of algorithms
Data Structure and Algorithms
Doubly linked lists.
Introduction to Linked Lists
Optimizing Malloc and Free
Object Oriented Programming COP3330 / CGS5409
Discussion section #2 HW1 questions?
Linked List Intro CSCE 121 J. Michael Moore.
Programming Abstractions
The List Type Lecture 9 Hartmut Kaiser
Algorithmic complexity: Speed of algorithms
Algorithmic complexity: Speed of algorithms
Linked List Intro CSCE 121.
Presentation transcript:

The List Type Lecture 9 Hartmut Kaiser

Programming Principle of the Day KISS (Keep it simple, stupid!)  Simplicity (and avoiding complexity) should always be a key goal. Simple code takes less time to write, has fewer bugs, and is easier to modify.  The principle is best exemplified by the story of Kelly Johnson (lead engineer at Lockheed) handing a team of design engineers a handful of tools, with the challenge that the jet aircraft they were designing must be repairable by an average mechanic in the field with only these tools. 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 2

Abstract Performance of our example is fine for small inputs, but quickly degrades for a larger number of students. Why? Inserting/deleting records in the middle of vectors requires moving big chunks of memory to preserve the random access property. Our program has a time complexity of O(N 2 ). Different data structure is required 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 3

The List Type We rewrote code to remove reliance on indices  Now: change data structure allowing to efficiently delete elements from the middle of the sequence Common requirement, therefore: std::list<>  Vectors are optimized for fast random access  List are optimized for fast insert and delete at any point  Generally, slower than vectors  In heavy insertion and deletion scenarios, faster  Choose data structure depending on use case 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 4

The List Type List and vector share many common ideas  Can store almost any data type  Share almost all operations Converting our program to use lists is surprisingly simple  Main change is swapping container types List do not support indexing operations  But using iterators allows to get away without those 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 5

Using Vector Type // version 3: iterators but no indexing vector extract_fails(vector & students) { vector fail; auto iter = students.begin(); while (iter != students.end()) { if (fail_grade(*iter)) { fail.push_back(*iter); iter = students.erase(iter); // watch out! } else ++iter; } return fail; } 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 6

Using List Type // version 4: using lists list extract_fails(list & students) { list fail; auto iter = students.begin(); while (iter != students.end()) { if (fail_grade(*iter)) { fail.push_back(*iter); iter = students.erase(iter); // watch out! } else ++iter; } return fail; } 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 7

Iterator Invalidation For vectors, any insert/remove operation potentially invalidated all iterators.  Reallocation, data movement  Watch out when storing iterators (i.e. end())! For lists, no iterators are invalidated  Well, except for the one pointing to an erased element  But lists are not random access, that mean that iterators do not support random access operations 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 8

Sorting a List Standard sort algorithm is usable for random access iterators only  std::sort not usable for lists: list students; student.sort(compare);  Instead of: vector students; sort(students.begin(), students.end(), compare); 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 9

Performance Data File size [records]List [s]Vector [s] /2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 10 ‘Right’ data structure is not a once and for all decision ▫ Performance might not even matter ▫ But depending on use case, performance might have a profound influence

Doubly-Linked List Given a pointer to a node in a doubly-linked list, we can remove the node in O(1) time. This isn’t possible in a singly-linked list, since we must have a pointer to the node in front of the one we want to remove. 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 11 start …

Doubly-Linked List 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 12 start … struct DLNode { DataType info; DLNodeIter next; DLNodeIter back; }; Each node is made from a struct that looks something like this. info next back

Doubly-Linked List 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 13 start …

Doubly-Linked List 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 14 start … ptr

Doubly-Linked List 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 15 start … ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr;

Doubly-Linked List 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 16 start … ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr;

Doubly-Linked List 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 17 start … ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr;

Doubly-Linked List 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 18 start … ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr;

Doubly-Linked List 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 19 start … ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr;

Doubly-Linked List 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 20 start … ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr;

Doubly-Linked List 24/2/2015, Lecture 9 CSC 1254, Spring 2015, The List Type 21 start … ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr;