1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.

Slides:



Advertisements
Similar presentations
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Advertisements

Stacks, Queues, and Linked Lists
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
Chapter 24 Lists, Stacks, and Queues
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
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.
Data Structures Intro/Linked List Review CIS 237 – Data Structures.
CSE Lecture 12 – Linked Lists …
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 7 Ming Li Department of.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Data Structures Using C++ 2E
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Information and Computer Sciences University of Hawaii, Manoa
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Chapter 16 Stacks & Queues. Objective In this chapter we will learn:  Stacks  Queues  Different implementations (arrays and linked list) of both 
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
A Generic List Class and Linked Lists Andy Wang Data Structures, Algorithms, and Generic Programming.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
April 27, 2017 COSC Data Structures I Review & Final Exam
Exam Review 2 Chapter 5 – 9 CSC212 FG CS Dept, CCNY.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
1 Generic Positional Containers and Double-Ended Queues.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Cpt S 122 – Data Structures Abstract Data Types
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Chapter 12 – Data Structures
CS 215 Final Review Ismail abumuhfouz Fall 2014.
The List ADT Sections 3.2, 3.3, 3.5 Introduction
Vectors Holds a set of elements, like an array
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Chapter 19: Stacks and Queues.
Object Oriented Programming COP3330 / CGS5409
Deques, Stacks and Queues
Stacks, Queues, and Deques
Foundational Data Structures
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Doubly Linked List Implementation
Review & Lab assignments
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Containers: Queue and List
Doubly Linked List Implementation
Chapter 3 Lists, Stacks, and Queues
The List Container and Iterators
Data Structures & Programming
Presentation transcript:

1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors

2 List Wish List Efficiently insert an element Efficiently remove an element –What are the time complexities for inserting, removing and searching for elements in a vector or sorted vector? Remove all items Assignment operator Comparison operators Constructors/destructors Generic class Convenient way to iterate through the list

3 Singly Linked List ListElement next prev frontback? null Time complexities for push_front, push_back, pop_front, pop_back?

4 Abstract view of List and Iterator ListElement next prev next prev I1 frontback null Iterator Doubly-linked list

5 List Public Interface Constructors and big-three –Copy constructor, destructor, assignment operator List(); List(const List &rhs); ~List(); const List & operator=(const List &rhs); Read-only accessor functions int size() const; bool empty() const;

6 List Public Interface (contd) Accessing values on the list Object & front(); Object & back(); and their constant versions. Locating places on the list using iterators –Both regular and constant versions iterator begin(); const_iterator begin() const; iterator end(); const_iterator end() const;

7 List Public Interface (contd) List manipulation functions int push_front(const Object & x); int push_back(const Object & x); int pop_front(); int pop_back(); iterator insert(iterator & itr, const Object & x); iterator erase( iterator itr); iterator erase( iterator start, iterator end ); void clear();

8 List Complexity Requirements O(1) Runtime complexity –Default constructor –push_front(t), push_back(t), insert(I, t) –pop_front(), pop_back(), erase(I) –begin(), end(); –front(), back(); –empty();

9 List Complexity Requirements (2) O(N) Runtime complexity –Copy Constructor –Destructor –clear() –erase(SI,EI)

10 List Iterator Public Interface Read-only operators int operator== (const iterator & rhs) const; int operator!= (const iterator & rhs) const; Object & operator* ( ) const; // return a reference to current value Write operators iterator & operator++ ( ); // prefix iterator operator++ ( int ); // postfix iterator& operator-- ( ); // prefix iterator operator-- ( int ); // postfix O(1) requirement for space and time

11 Using List List Cities; Cities.push_front(Tallahassee); –Tallahassee Cities.push_back(Gainesville); –Tallahassee, Gainesville Cities.push_front(Jacksonville); –Jacksonville, Tallahassee, Gainesville Cities.push_back(Miami); –Jacksonville, Tallahassee, Gainesville, Miami List ::iterator I; for (I = Cities.begin(); I != Cities.end(); ++I) { // print list with << }

12 List Insertion Insert Orlando before Miami // sequential search for (I = Cities.begin(); I != Cities.end(); ++I) { if (Miami == *I) { break; } // insert the new string Cities.insert(I, Orlando); –Jacksonville, Tallahassee, Gainesville, Orlando, Miami // what happens if Miami is not on the list?

13 Remove all copies of an item from List Remove all elements with value Orlando List ::iterator I = Cities.begin(); while( I != Cities.end()) { if (Orlando == *I) { I = Cities.erase(I); } else { I++; }

14 List and List Iterator –Conceptual relationship Iterator I1 begincurrentend List: A, B, C, D, E, F begincurrentend Iterator I2

15 List Implementation A Doubly Linked List With Header and Tail Nodes as Markers An Empty List

16 Nodes in a list Node –Data Value –Pointers to the previous and next element Defined within the List class, with limited scope data prev next data prev next data prev next No need for contiguous memory allocation

17 Outline of List Class (Part 1)

18 Outline of List Class (Part 2)

19 Outline of List Class (Part 3)

20 Outline of List Class (Part 4)

21 const_iterator for List Prefix Postfix

22 const_iterator class for List (contd.)

23 List Initialization Routines

24 List Insert Routine

25 List Erase Routine