The List Type Lecture 9 Hartmut Kaiser

Slides:



Advertisements
Similar presentations
Linked Lists Linked Lists Representation Traversing a Linked List
Advertisements

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
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
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.
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
The List Type Lecture 9 Hartmut Kaiser
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.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Using Sequential Containers Lecture 8 Hartmut Kaiser
LINKED LISTS.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Intro to Data Structures Concepts ● We've been working with classes and structures to form linked lists – a linked list is an example of something known.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Mohammed I DAABO COURSE CODE: CSC 355 COURSE TITLE: Data Structures.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
Insertion sort Loop invariants Dynamic memory
Chapter 16: Linked Lists.
CSE 1342 Programming Concepts
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
COSC160: Data Structures Linked Lists
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
Week 4 - Monday CS221.
Data Structure By Amee Trivedi.
October 2nd – Dictionary ADT
Standard Template Library
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Introduction to Linked Lists
Algorithmic complexity: Speed of algorithms
Lectures linked lists Chapter 6 of textbook
Big-O notation Linked lists
Review Deleting an Element from a Linked List Deletion involves:
Data Structure and Algorithms
Informatica PowerCenter Performance Tuning Tips
UNIT-3 LINKED LIST.
ENERGY 211 / CME 211 Lecture 12 October 17, 2008.
Programming Abstractions
Compsci 201 Priority Queues & Autocomplete
Doubly linked lists.
Introduction to Linked Lists
Compiler Construction
Optimizing Malloc and Free
Discussion section #2 HW1 questions?
Linked List Intro CSCE 121 J. Michael Moore.
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Defining New Types Lecture 22 Hartmut Kaiser
Programming Abstractions
Recursive Linked List Operations
A Robust Data Structure
slides adapted from Marty Stepp and Hélène Martin
Algorithmic complexity: Speed of algorithms
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Priority Queues & Heaps
Algorithmic complexity: Speed of algorithms
Data Structures Introduction
slides created by Marty Stepp
CS210- Lecture 6 Jun 13, 2005 Announcements
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Linked List Intro CSCE 121.
CO4301 – Advanced Games Development Week 12 Using Trees
Chapter 9 Linked Lists.
slides created by Marty Stepp
Presentation transcript:

The List Type Lecture 9 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/˜hkaiser/fall_2013/csc1254.html

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. http://en.wikipedia.org/wiki/KISS_principle 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

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(N2). Different data structure is required 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

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 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

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 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

Using Vector Type // version 3: iterators but no indexing vector<student_info> extract_fails(vector<student_info>& students) { vector<student_info> 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; 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

Using List Type // version 4: using lists list<student_info> extract_fails(list<student_info>& students) { list<student_info> 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; 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

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 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

Sorting a List Standard sort algorithm is usable for random access iterators only std::sort not usable for lists: list<student_info> students; student.sort(compare); Instead of: vector<student_info> students; sort(students.begin(), students.end(), compare); 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

Performance Data File size [records] List [s] Vector [s] 700 0.1 7000 0.8 6.7 70000 8.8 597.1 ‘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 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

Doubly-Linked List … start 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. 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

Doubly-Linked List start … struct DLNode { DataType info; DLNodeIter next; DLNodeIter back; }; Each node is made from a struct that looks something like this. info next back 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

Doubly-Linked List … start 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

Doubly-Linked List … start ptr 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

Doubly-Linked List … start ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr; 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

Doubly-Linked List … start ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr; 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

Doubly-Linked List … start ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr; 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

Doubly-Linked List … start ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr; 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

Doubly-Linked List … start ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr; 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

Doubly-Linked List … start ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr; 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type

Doubly-Linked List … start ptr ptr->back->next = ptr->next; ptr->next->back = ptr->back; delete ptr; 10/3/2012, Lecture 9 CSC 1254, Fall 2013, The List Type