Lists CMSC 202, Version 4/02.

Slides:



Advertisements
Similar presentations
Linear Lists – Array Representation
Advertisements

DATA STRUCTURES USING C++ Chapter 5
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
CMSC 202 Lesson 23 Templates II. Warmup Write the templated Swap function _______________________________ void Swap( ________ a, ________ b ) { _______________.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Classes and Data Structures Jeffrey S. Childs
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
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.
Motivation for Generic Programming in C++
Chapter 16: Linked Lists.
CSCE 210 Data Structures and Algorithms
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
C++ Programming:. Program Design Including
Abstract Data Types and Encapsulation Concepts
Lists CS 3358.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
C++ Plus Data Structures
Lists The List ADT.
Stack and Queue APURBO DATTA.
Templates II CMSC 202.
Lecture 9 Concepts of Programming Languages
Array Lists Chapter 6 Section 6.1 to 6.3
Chapter 16-2 Linked Structures
Linked Lists.
ADT Implementations: Templates and Standard Containers
Object Oriented Programming COP3330 / CGS5409
Classes with Dynamically Allocated Data
Introduction to Classes
Data structures in C++.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CMSC 341 Binary Search Trees.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Doubly Linked List Implementation
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 16 Linked Structures
Lists - I The List ADT.
Lists - I The List ADT.
CMSC 341 Binary Search Trees.
Linked Lists.
Lists.
Data Structures & Algorithms
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Lists - I The List ADT.
CMSC 341 List 2.
Data Structures and Algorithms Memory allocation and Dynamic Array
Lists - I The List ADT.
Lists CMSC 202, Version 4/02.
Data Abstraction: The Walls
Doubly Linked List Implementation
Abstract Data Types ADT: A set of objects and a set of operations on those objects. examples: integers: +, - , *, … Collection, insert, remove, … set:
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 3 Lists, Stacks, and Queues
CMSC 341 Binary Search Trees 2/21/2006.
Abstract Data Types Stacks CSCI 240
21 Data Structures.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Lists CMSC 202, Version 4/02

Review: ADTs A concept, not an implementation A set of (homogeneous) objects together with a set of operations on those objects No mention of how the operations are implemented No rules tell which operations are required A design decision CMSC 202, Version 4/02

Review: ADTs (con’t) From the outside, the user sees only a collection of operations that together define the behavior of the abstraction. The user also sees how to use the operations (via the operation interfaces). On the other side, the programmer implementing the abstraction sees the data variables that are used to maintain the state. CMSC 202, Version 4/02

A List ADT A list is a dynamic, homogeneous tuple (set of ordered items) A1, A2, A3, …, An where Ai is the ith item in the list. The position of item Ai is i. Positions range from 1 to n, inclusive. The size of a list is n. A list of containing no items 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 CMSC 202, Version 4/02

Typical List Operations Construct a new empty list Construct a new list as a copy of an existing list Destroy the list Make the list empty Insert an element into the list Remove an element from the list Locate the position of an item in the list Find the value of the nth item in the list Assign one list to another Determine if the list is full Determine if the list is empty Print the list CMSC 202, Version 4/02

Design Considerations How many items will the list be able to hold? Some maximum number An “infinite” number (how will this be handled?) Will duplicate items be allowed? If so, how will this affect insert, delete, print, etc.? Positions should be numbered from 1 – N in keeping with the ADT definition CMSC 202, Version 4/02

Example ADT: List of Integers All lists work the same way, regardless of the type of data stored in the list. We’ll use a list of integers for our discussion. We will follow this procedure: Outline assumptions that we will make about a list Decide on the list operations Translate the list operations into their C++ interfaces, deciding which will be public, private, and friends Be careful not to make any assumptions about how the list will actually be represented (e.g., an array, a linked list). CMSC 202, Version 4/02

Design Assumptions: The list will be a list of integers. Notice that we did not say “int’s”. How the integers are implemented will be decided later (ints, strings, or some other representation). The list can hold a maximum of 100 items numbered 1 - 100 The list can be empty. Duplicate items will not be allowed. CMSC 202, Version 4/02

Design (con’t) Operations: Construct a new empty list Construct a new list as a copy of an existing list Destroy the list Assign one list to another Determine if the list is full Determine if the list is empty Make the list empty Find the value of the kth item in the list Locate the position of an item in the list Insert an element into the list Remove an element from the list Print the list CMSC 202, Version 4/02

Operation Interfaces Construct a new empty list // default constructor IntList::IntList(int initialSize = 100); Construct a new list as a copy of an existing list // copy constructor IntList::IntList(const IntList& rhs); CMSC 202, Version 4/02

Operation Interfaces (con’t) Destroy the list // destructor IntList::~IntList( ); Make the list empty void IntList::MakeEmpty( ); CMSC 202, Version 4/02

Operation Interfaces (con’t) Check for an empty list bool IntList::IsEmpty ( ) const; Check for a full list bool IntList::IsFull ( ) const; CMSC 202, Version 4/02

Operation Interfaces (con’t) Assign one list to another IntList& IntList::operator= (const IntList&); CMSC 202, Version 4/02

Operation Interfaces (con’t) Insert an item into the list Design decision: Boolean function If the item is already in the list, return false and do nothing Data, not a “package” (such as a node with it’s next pointer) are send to the funciton bool IntList::Insert(int x); What do you think of this design decision? CMSC 202, Version 4/02

Operation Interfaces (con’t) Remove an item from the list Design Decision: Boolean function If the item is not in the list, return false and do nothing bool IntList::Remove(int x); What do you think of this design decision? CMSC 202, Version 4/02

Operation Interfaces (con’t) Locate the position of an item in the list Design decision: Positions will be numbered from 1 - N If the item is not in the list, return 0 int IntList::FindPosition(int x) const; What do you think of this design decision? CMSC 202, Version 4/02

Operation Interfaces (con’t) Return the item in the kth position Design Decision: If the position is out of range, return 0. int IntList::operator[ ] (int k) const; What do you think of this design decision? CMSC 202, Version 4/02

Operation Interfaces (con’t) Print the list Design decision: Overload the << operator rather than create a named function Make operator<< a friend of the IntList class ostream& operator<< (ostream&, const IntList &); What do you think of this design decision? CMSC 202, Version 4/02

IntList Interface class IntList { friend ostream& operator<<(ostream& out, const IntList &L); public: IntList (int size = 100); IntList (const IntList&); ~IntList ( ); IntList& operator= (const IntList &); void MakeEmpty ( ); int operator[ ] (int position) const; int FindPosition (int x) const; bool Insert (int x); bool Remove (int x); bool IsFull ( ) const; bool IsEmpty ( ) const; private: // private data and methods }; CMSC 202, Version 4/02

Using IntList IntList L1; // a list with room for 100 integers int x; L1.Insert (7); L1.Insert (42); x = L1[1]; cout << L1 << endl; L1.Remove (22); if (! L1.IsEmpty ( ) ) L1. MakeEmpty ( ) ; CMSC 202, Version 4/02

Using IntList (cont’d) Observations: Client (application/user) code can only use public methods and friend functions provided by IntList. We can now choose any data representation for the IntList that we want to with no impact on the client code. If the representation changes later, there is no impact on the client code (except for possible recompilation). CMSC 202, Version 4/02

Using IntList (cont’d) We deliver the interface (.H) to the customer in source code form. It doesn’t matter if the application programmer sees the representation. He/she still has no direct access to an IntList. . CMSC 202, Version 4/02

IntList Implementation Choices: array linked list Regardless of the choice, we need to store the following information: maximum capacity of the list actual size of the list the list items CMSC 202, Version 4/02

Array Implementation class IntList { friend . . . public: . . . private: // private member functions here int m_capacity; int m_size; int *m_theArray; }; CMSC 202, Version 4/02

Linked List Implementation I struct Node { // using a struct as a node int m_data; Node* m_next; }; class IntList { friend . . . public: . . . private: // private member functions here int m_capacity; int m_size; Node *m_theList; }; CMSC 202, Version 4/02

Linked List Implementation II class Node { // using an object as a node Node( int d = 0, Node* n = NULL ); int m_data; Node* m_next; friend class IntList; }; class IntList { public: // same as before private: // private member functions here int m_capacity; int m_size; Node *m_head; }; CMSC 202, Version 4/02

Array vs. Linked List What are my memory requirements? What are my response time requirements? What is the relative performance of each method for each implementation? Will the list be used by other applications for other types of data? How easy is it to generalize the list implementation (e.g., using templates)? CMSC 202, Version 4/02

Generalized Linked List template <class DATATYPE> class List; // forward declaration class Node { friend class List<DATATYPE>; Node( const DATATYPE& d = DATATYPE( ), DATATYPE * n = NULL); DATATYPE m_data; Node<DATATYPE>* m_next; }; CMSC 202, Version 4/02

Generalized Linked List (con’t) template <class DATATYPE> class List { public: . . . bool Insert(const DATATYPE &data); DATATYPE operator[ ] (int k) const; . . . private: // private member functions here int m_capacity; int m_size; Node<DATATYPE> *m_theList; }; CMSC 202, Version 4/02

Generalized Linked List (con’t) #include “list.H” int main( ) { List<int> integerList; List<SomeClass> someList; . . . return 0; } (See your text for more details on the generalized linked list implementation.) CMSC 202, Version 4/02