Download presentation
Presentation is loading. Please wait.
Published byΧλόη Οικονόμου Modified over 5 years ago
1
Abstract Data Types ADT: A set of objects and a set of operations on those objects. examples: integers: +, - , *, … Collection, insert, remove, … set: union, intersection, complement, size, … In C++, the set of operations roughly corresponds to the public member functions of a class. Oct 12, 2001 CSE 373, Autumn 2001
2
ADTs continued Implementation details can be hidden: member functions access private data members and member functions. Functions outside the ADT can only access the public data members and functions. difference between software engineering and hacking: finding the balance between functionality, simplicity, flexibility, and performance. ADT Oct 12, 2001 CSE 373, Autumn 2001
3
List ADT list of size N: A1, A2, A3, …, AN The position of Ai is i.
A list of size 0 is the empty list. operations : printList ( ) makeEmpty ( ) insert (x, pos) remove (x) findkth (k) … Oct 12, 2001 CSE 373, Autumn 2001
4
Array Implementation printList makeEmpty insert remove findkth
Problems? A1 A2 A3 … AN Oct 12, 2001 CSE 373, Autumn 2001
5
Linked List Impl. insert delete A1 A2 A3 A4 x A1 A2 A3 A4 Oct 12, 2001
CSE 373, Autumn 2001
6
Sentinels Use a header or dummy node to simplify the code. header
empty list A1 A2 A3 Oct 12, 2001 CSE 373, Autumn 2001
7
Linked Lists in C++ Weiss uses three classes to implement linked lists. IntListItr IntList A1 A2 A3 IntListNode Oct 12, 2001 CSE 373, Autumn 2001
8
IntListNode IntListNode represents one node of a list.
class IntListNode { private: IntListNode (const int theData = 0, IntListNode * n = NULL) : data(theData), next(n) } int data; IntListNode * next; friend class IntList; friend class IntListItr; }; IntListNode represents one node of a list. Oct 12, 2001 CSE 373, Autumn 2001
9
IntListItr class IntListItr { public: IntListItr() : current(NULL) { }
bool isPastEnd() const return (current == NULL); } void advance() if (!isPastEnd()) current = current->next; const int retrieve() const if (isPastEnd()) throw BadIterator(); return current->data; Oct 12, 2001 CSE 373, Autumn 2001
10
IntListNode * current; IntListItr(IntListNode * theNode)
private: IntListNode * current; IntListItr(IntListNode * theNode) : current(theNode) { } friend class IntList; } // class IntListItr class IntList { public: // constructors ... bool isEmpty() const; void makeEmpty(); IntListItr zeroth() const; ... void insert (const int x, const IntListItr & p); IntListNode *header; } Oct 12, 2001 CSE 373, Autumn 2001
11
IntList functions IntList::IntList() { header = new IntListNode; }
bool IntList::isEmpty() const return (header->next == NULL); IntListItr IntList::first() const return IntListItr(header->next); IntListItr IntList::find(const int x) const IntListNode *itr = header->next; while ((itr != NULL) && (itr->data != x)) itr = itr->next; return IntListItr(itr); Oct 12, 2001 CSE 373, Autumn 2001
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.