Lists - I The List ADT.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
The List ADT Textbook Sections
Linked Lists CENG 213 Data Structures.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Data Structures Using C++ 2E
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
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.
C++ Classes and Data Structures Jeffrey S. Childs
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,
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
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.
Pointers and Dynamic Arrays
Unit – I Lists.
CS505 Data Structures and Algorithms
The List ADT Reading: Textbook Sections 3.1 – 3.5
CE 221 Data Structures and Algorithms
Lists CS 3358.
Vectors Holds a set of elements, like an array
CISC181 Introduction to Computer Science Dr
Abstract Data Types data object set or collection of instances
Lists The List ADT.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 4 Linked Lists
CMSC 341 Lecture 5 Stacks, Queues
Chapter 16-2 Linked Structures
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Linked Lists.
Object Oriented Programming COP3330 / CGS5409
Chapter 4 Linked Lists.
CMSC 341 Binary Search Trees 11/19/2018.
The List ADT Reading: Textbook Sections 3.1 – 3.5
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
CSC 143 Queues [Chapter 7].
CMSC 341 Binary Search Trees.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
CMSC 341.
CMSC 341 Binary Search Trees 1/3/2019.
Doubly Linked List Implementation
CSC 143 Stacks [Chapter 6].
Chapter 4 Linked Lists.
CMSC 341 Binary Search Trees 2/23/2019.
CMSC 341.
Lists - I The List ADT.
Lists - I The List ADT.
CMSC 341.
Queues Jyh-Shing Roger Jang (張智星)
CMSC 341 Binary Search Trees.
Lists - I The List ADT.
CMSC 341 List 2.
General List.
CMSC 341 Binary Search Trees 5/8/2019.
Lists CMSC 202, Version 4/02.
CMSC 341 Binary Search Trees 5/28/2019.
Lists CMSC 202, Version 4/02.
Doubly Linked List Implementation
Getting queues right … finally (?)
Abstract Data Types ADT: A set of objects and a set of operations on those objects. examples: integers: +, - , *, … Collection, insert, remove, … set:
Chapter 3 Lists, Stacks, and Queues
CMSC 341 Binary Search Trees 2/21/2006.
The List Container and Iterators
A dictionary lookup mechanism
Data Structures & Programming
Presentation transcript:

Lists - I The List ADT

List ADT (expanded from Weiss) A list is a dynamic ordered tuple of homogeneous elements A1, A2, A3, …, AN where Ai is the ith element of the list Definition: The position of element Ai is i; positions range from 1 to N inclusive Definition: The size of a list is N ( a list of NO elements is called “an empty list”) dynamic means the list can change over time homogeneous means the elements are all of the same type ordered means that A1+1 follows (or succeeds) Ai and Ai-1 precedes Ai

Other considerations What design considerations are there regarding this list. Will the list hold an “infinite” number of elements, or will it have limited capacity? If so, what’s the maximum number of elements? How will the list handle insertion of duplicate elements? If the list allows insertion of duplicates, how does it handle deletion of a duplicated element?

Operations on a List List( ) -- construct an empty list List(const List &rhs) -- construct a list as a copy of rhs ~List( ) -- destroy the list const List &operator= (const List &rhs) make this list contain copies of the elements of rhs in the same order elements are deep copied from rhs, not used directly. If L1 = (A1, A2, A3) and L2 = (B1, B2) before the assignment, then L2 = L1 causes L2 = (A1, A2, A3)

Operations on a List (cont) bool isEmpty( ) const -- returns true if the list size is zero void makeEmpty( ) -- causes the list to become empty void remove (const Object &x) the first occurrence of x is removed from the list, if it is present. If x is not present, the list is unchanged. an occurrence of x is an element Ai of the list such that Ai == x Also: insert find findPrevious What about ‘size’? -- there is no operator that returns the size of a list Also no retrieve(int) that gives the ith element of the list. Not possible for (i=1; i <=L.size(); i++) cout << L.retrieve(i); How do you scan a list? How do you retrieve an element from a list?

What’s Missing? There is no size( ) method that returns the size of the list There is no retrieve( int i) or operator[ int i] method that access the ith element in the list So, it’s NOT possible to write code like this: for (int i = 1; j < L.size( ); i++) cout << L.retrieve ( i ); How do we “scan” a list and look at all the elements, one at a time?

Iterators An iterator is an object that provides access to the elements of a collection (in a specified order) without exposing the underlying structure of the collection. order dictated by the iterator collection provides iterators on demand each iterator on a collection is independent iterator operations are generic C++ tradition (as in the STL) is to think of iterators as generalizations of pointers. An iterator “points” to an element of the collection and can be made to “point” to the previous or next element by operating on the iterator itself, not on the collection.

Iterator Operations bool isPastEnd( ) -- returns true if the iterator is past the end of the list void advance( ) -- advances the iterator to the next position in the list. If iterator already past the end, no change. const Object &retrieve( ) -- returns the element in the list at the current position of the iterator. It is an error to invoke “retrieve” on an iterator that isPastEnd Scanning in pseudocode Iterator iter = collection.first(); while (!iter.isPastEnd()) { do something with iter.retrieve(); iter.advance(); } No knowledge about the structure of the collection is needed. Collection.first() provides an iterator pointing to the first element of the collection If collection is empty, the iterator is already pastEnd.

List Operations ListIter<Object> first( ) -- returns an iterator representing the first element on the list ListIter<Object> zeroth( ) -- returns an iterator representing the header of a list ListIter<Object> find(const Object &x) -- returns an iterator representing the first occurrence of x in the list. If x not present, the iterator isPastEnd. ListIter<Object> findPrevious(const Object &x) -- returns an iterator representing the element before x in the list. If x is not in the list, the iterator represents the last element in the list. If x is first element (or list is empty), the iterator returned is equal to the one returned by zeroth( ). On first() -- what if list is empty? OK, the iterator isPastEnd More about header nodes later – implementation may or may not have one

“scanning” a Collection Iterator iter = collection.first ( ); while (! iter.isPastEnd ( ) ) { Object x = iter.retrieve( ) ; // do something with x iter.advance ( ); }

List Operators (cont) void insert (const Object &x, const ListIter<Object> &p) inserts a copy of x in the list after the element referred to by p if p isPastEnd, the insertion fails without an indication of failure.

Ex: Building a List List<int> list; // empty list of int ListIter<int> iter = list.zeroth( ); for (int i=0; i < 5; i++) { list.insert(i, iter); iter.advance( ); } Builds list: 0, 1, 2, 3, 4

Ex: Building a List #2 List<int> list; // empty list of int ListIter<int> iter = list.zeroth( ); for (int i=0; i < 5; i++) { list.insert(i, iter); } Builds list: 4,3, 2, 1, 0 Why different from previous slide?? This one doesn’t advance the iterator so all inserts at the front, resulting in nodes appearing in “reverse” order from insertion order Can you build a list (from empty) using the iterator returned by first() instead of zeroth()? Ans: no -- first() isPastEnd on an empty list.

More List Operations Find an element in the list and return it’s position Tell me the value of the Nth element Determine if the list is full Determine if the list is empty Print the list