Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists The List ADT.

Similar presentations


Presentation on theme: "Lists The List ADT."— Presentation transcript:

1 Lists The List ADT

2 List ADT 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

3 Operations on a List Construct a new empty list
Construct a new list as a copy of an existing list Destroy the list Remove all the elements from the list without destroying the list (make the list empty) Insert an element into the list Remove an element from the list

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

5 Other considerations What design considerations are there regarding this list. (NOT implementation details) Will the list hold an “infinite” number of elements, or will it have limited capacity? If so, what’s the maximum number of elements? 2. How will the list handle duplicate elements?

6 List of ints All lists work the same way, regardless of the type of data stored in the list. We’ll use a list of ints for our discussion. The next thing we need to do is translate these operations into a class’ public interface. We are not yet ready to talk about the implementation of these operations.

7 Construct a new empty list
This is the default constructor // default constructor intList::intList ( int initialSize = 100);

8 Construct a new list as a copy
This is the copy constructor // copy constructor intList::intList (const intList& rhs);

9 Destroy the list The destructor // intList destructor
intList::~intList ( );

10 Insert an element into the list
A member function void intList::insert (int x); Should this prototype be modified? Design Decision Are there other possible functions that could be used instead of or in addiiton to insert( ) ?

11 Make the list empty A member function void intList::makeEmpty ( );

12 Find an element and return its position
A member function int intList::find (int x ); Design decisions -- What if X is not in the list?

13 Remove an element from the list
A member function void intList::remove (int x); Should this prototype be modified? Design Decisions Are there other possible functions that could be used instead of or in addition to remove( ) ? What if X is not in the list?

14 Value of the Nth element
A member function int intList::elementAt (int index); Design Decision Are there other possible functions to that could be defined instead of or in addition to elementAt( )? What if index is too big or too small?

15 Full or Empty Both are simple boolean member functions bool isEmpty ( ); bool isFull ( );

16 Print the List This is operator<< -- not a member ostream& operator<< (ostream&, const intList &);

17 List Assignment This is operator = intList& intList::operator= (const intList&);

18 intList public interface
class intList { public: intList ( int size = 100); intList (const intList&); ~intList ( ); intList& operator= (const intList &); bool isFull ( ) const; bool isEmpty ( ) const; void makeEmpty ( ); const int& elementAt (int index) const; int find (int x) const; void insert (int x); void remove (int x); private: ?????? }; ostream& operator<< (ostream& out, const intList& L);

19 Using intList intList L1; // a default list of 100 ints L1.insert (7);
int x = L1.elementAt( 1 ); cout << L1 << endl; L1.remove (22); if (! L1.isEmpty ( ) ) L1. makeEmpty ( ) ; // etc, etc, etc

20 Using intList (cont’d)
The user code (whether in main( ), some other function, or in the member function of some other object CAN ONLY use public methods provided by intList. We can implement the intList (the private stuff) anyway we want with no impact on the user code – the user code CANNOT have any knowledge of the implementation

21 Designing an array implementation
What data will we keep? Partially determined by needs of the public methods. How do we provide “infinite” capacity? What methods would be affected? How do we handle errors?

22 Array implementation class intList { public: …….
private: int capacity; int size; int *theArray; };

23 Consider each public method
Big 4 – discussed last time isEmpty, isFull – easy?? elementAt -- index validation issues insert – what if array is full? Duplicates? find – what if not found? Duplicates? remove – what if not found? Duplicates? makeEmpty – like part of destructor

24 Advanced thoughts What is the asymptotic performance of each public method?


Download ppt "Lists The List ADT."

Similar presentations


Ads by Google