L3b_-_List_(4_on_1) - Lecture Overview Lecture 3b List ADT...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 3b List ADT and its implementations 2 --- 84 [CS1102C AY0708S1 Lecture 3b] Lecture Overview ± List ADT ² Specification ± Implementation for List ADT ² Array Based ² Linked List Based ± Variation of Linked Lists List ADT Your name is not on the guest list! 4 --- 84 [CS1102C AY0708S1 Lecture 3b] List ADT ± A sequence of items where positional order matter <a 1 , a 2 ,…, a n-1 , a n > ± Lists are very pervasive in computing ² e.g. student list, list of events, list of appointments etc The list ADT idx : Position, integer item : Data stored in list, can be any data type List add(idx,item) remove(idx) retrieve(idx, item) List() getLegnth() isEmpty()
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5 --- 84 [CS1102C AY0708S1 Lecture 3b] List ADT : C++ Specification (Ver.1) typedef int ListItemType ; class List { public : List(); bool isEmpty() const; int getLength() const; void insert(int index, ListItemType newItem, bool& success); void remove(int index, bool& success); void retrieve(int index, ListItemType& dataItem, bool& success) const; private : //Implementation dependent. //See subsequent implementation slides }; // end List class To facillitate changing of data type of the item const indicates that this method does not modifies the attributes of an object success is passed by reference, act as a flag to indicate the outcome 6 --- 84 [CS1102C AY0708S1 Lecture 3b] List ADT: Utilizing Exception ± The current specification has one minor problems: ² The outcome of insert() , remove() is returned as a boolean parameter ² Careless programmer may not check this status ² Cause hard to debug errors during runtime ± Make use of exception (Lecture 2) to: ² Force programmer to handle the error or crash the program ² Categorize the exception into 2 types: ± List Exception: Problems with the list itself, e.g. list is full ± List Index Exception: Invalid Index. l.insert(13, 9999, insertOK); //l is a List l.insert(14,10000, insertOK); Mistake: No checking of insertOK before the next insertion 7 --- 84 [CS1102C AY0708S1 Lecture 3b] List ADT: C++ Exception #include <cstdlib> using namespace std; class ListIndexOutOfRangeException { public : ListIndexOutOfRangeException () : message (“Index Exception") { } ListIndexOutOfRangeException (char* msg) : message (msg) { } const char *what () const { return message; } private : const char *message; }; ± Definition for class ListException is similar 8 --- 84 [CS1102C AY0708S1 Lecture 3b] List ADT : C++ Specification (Ver.2) typedef int ListItemType ; class List { public : List(); bool isEmpty() const; int getLength() const; void insert(int index, const ListItemType& newItem) throw (ListException, ListIndexOutOfRangeException); void remove(int index) throw (ListIndexOutOfRangeException); void retrieve(int index, ListItemType& dataItem) const throw (ListIndexOutOfRangeException); private : //Implementation dependent //See subsequent implementation slides }; // end List class Indicate the possible exceptions Make use of const and & to use less memory
Background image of page 2
List ADT using Array 10 --- 84 [CS1102C AY0708S1 Lecture 3b] Implement List ADT: Using Array ± Array is a prime candidate for implementing the ADT ² Straight forward coding ± Lets study the feasibility of using Array ± Advantage: ² Very fast retrieval n size a 0 a 2 a 3 a n-1 arr :: array [0. .m] of locations
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/02/2009 for the course ECE CS1102C taught by Professor Dr.tansuntuck during the Spring '07 term at National University of Singapore.

Page1 / 21

L3b_-_List_(4_on_1) - Lecture Overview Lecture 3b List ADT...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online