L3b_-_List_(1_on_1) - Lecture 3b List ADT and its...

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

View Full Document Right Arrow Icon
Lecture 3b List ADT and its implementations
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 --- 76 [CS1102C AY0809S2 Lecture 3b] Lecture Overview ± List ADT ² Specification ± Implementation for List ADT ² Array Based ² Linked List Based ± Variation of Linked Lists
Background image of page 2
List ADT Your name is not on the guest list!
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 --- 76 [CS1102C AY0809S2 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 4
5 --- 76 [CS1102C AY0809S2 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 facilitate 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
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 --- 76 [CS1102C AY0809S2 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
Background image of page 6
7 --- 76 [CS1102C AY0809S2 Lecture 3b] List ADT: C++ Exception class ListIndexOutOfRangeException { public : ListIndexOutOfRangeException () : message(“Index Exception") { } ListIndexOutOfRangeException (string msg) : message(msg) { } string getMessage() const { return message; } private : string message; }; ± Definition for class ListException is similar
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 --- 76 [CS1102C AY0809S2 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 8
List ADT using Array
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 --- 76 [CS1102C AY0809S2 Lecture 3b] Implement List ADT: Using Array ± Array is a prime candidate for implementing the ADT ² Straight forward coding ± Advantage: ² Very fast retrieval n size a 0 a 2 a 3 a n-1 arr :: array [0. .m] of locations 01 2 n-1 m Internal of the list ADT, Array Version Unused Spaces
Background image of page 10
11 --- 76
Background image of page 11

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

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

Page1 / 76

L3b_-_List_(1_on_1) - Lecture 3b List ADT and its...

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

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