topic10_OrderedLists

topic10_OrderedLists - Ordered Lists CS 1037a Topic 10...

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

View Full Document Right Arrow Icon
Ordered Lists CS 1037a – Topic 10
Background image of page 1

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

View Full DocumentRight Arrow Icon
Ordered List ADT • A list that is maintained in sorted order relies upon the relational operators “ <= ”, “ == ”, etc., being properly defined for the items in the list; this may require operator overloading in the item class • Most ordered list operations will be the similar to those for the List ADT ( isFull , remove , retrieve , getLength , etc) • No replace operation: it could ruin list order 10-2
Background image of page 2
Ordered List ADT • We’ll need a new insert function that stores an element in the proper place in the list so that ordering is maintained • We’ll also include a function find that returns the position of the first occurrence of a given item • We’ll use an array-based list member variable to store the list contents 10-3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Operations on OrderedList version 1 OrderedList Construct an empty ordered list of objects isEmpty Determine whether or not the list is empty getLength Get the current size of the list remove Remove value at a given position from the list retrieve Get the Item at a particular location in the list insert Add an object to the list at the appropriate place find Return the position of a particular object in the list 10-4
Background image of page 4
UML Diagram for OrderedList version 1 OrderedList -m_container: List +isEmpty: boolean {query} +getLength: integer {query} +remove(integer pos): Item +retrieve(integer pos): Item {query} +insert(Item item): void +find(Item item): integer {query} 10-5
Background image of page 5

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

View Full DocumentRight Arrow Icon
OrderedList.h (version 1) #include “List.h” template <class Item> class OrderedList { public: OrderedList(unsigned int capacity = DEFAULT_LIST); // constructor for // an empty ordered list that can hold up to capacity items; default // size is defined in List.h ~OrderedList( ); // destructor bool isEmpty( ) const; // true if list is empty, false otherwise 10-6
Background image of page 6
OrderedList.h (version 1) int getLength( ) const; // returns the current size of the list // remove the value at location pos , and return it; // precondition : pos must be a legal list position Item remove (unsigned int pos); // return value at pos without modifying the list; // precondition : pos must be a legal list position Item retrieve (unsigned int pos) const; // cont’d. . 10-7
Background image of page 7

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

View Full DocumentRight Arrow Icon
OrderedList.h (version 1) // insert item at appropriate pos’n in list void insert (Item item); // return pos’n of first occurrence of item, or -1 if item isn’t found int find (Item item) const; private: List<Item> m_container; // to hold the list of Items }; #include “OrderedList.template” // end of header file 10-8
Background image of page 8
#include <stdlib.h> template <class Item> OrderedList<Item> :: OrderedList (unsigned int capacity ) : m_container(capacity) { } template <class Item> OrderedList<Item> :: ~OrderedList ( ) { } // cont’d. . 10-9
Background image of page 9

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

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

This note was uploaded on 10/26/2009 for the course CS cs1037 taught by Professor Yuri during the Fall '07 term at UWO.

Page1 / 50

topic10_OrderedLists - Ordered Lists CS 1037a Topic 10...

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

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