CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1
Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2
A Key Table Class The Key Table as a Dictionary ADT Key Table Key Table Data Members Key Table Operations Class Template Element Specification Key Table Class Definition Key Table Class Implementation Example Application Prof. Amr Goneid, AUC 3
1. The Key Table as a Dictionary A key table is essentially a Dictionary ADT. A form of container that permits access by content. An element (e) has a key part (k) Supports the following main operations: Insert (D, e): Insert item e in dictionary D Delete (D, e): Delete item e from D Search (D, k): search for key k in D Prof. amr Goneid, AUC 4
2. ADT Key Table Key Table: a linear configuration of elements in which we can insert and delete such elements. It also supports search by content (key), and can represent a dictionary ADT. Element: contains one key and associated data item Current Element: special element in the table, indicated by a pointer to the current position. Prof. Amr Goneid, AUC 5
ADT Key Table We will construct a class Ktable whose objects are key tables. A key table will be implemented as a dynamic array. The data members will be the elements and a pointer (index) to these elements. An element contains a key field and a data field. Search is by content (key) The table will have a default maximum size MaxSize of 128 elements. The application program can specify other sizes since the table is dynamic. The application program will also specify the actual types for the key and data fields. Prof. Amr Goneid, AUC 6
3. Key Table Data Members Elements. Each element has: 1. A key of type keytype 2. Data or information field of type datatype Others: T, a pointer to a dynamic array of elements; P, a pointer (index) to the current element; MaxSize, The maximum size (Capacity) of the table N, index of the last element in the table ( N < 0 if the table is empty, and N = Maxsize 1 if the table is full). Prof. Amr Goneid, AUC 7
4. Key Table Operations construct: Create table destruct: Destroy table tableisempty bool : return True if table is empty tableisfull bool : return True if table is full occupancy int : return the current no. of elements in the table Prof. Amr Goneid, AUC 8
Key Table Operations update (d) : to update the data portion of the current element to contain d; assume the current position is nonempty. retrieve (d): to return the data (d) in the current element; assume the current position is nonempty. delete: delete the current element. Assume the current position is nonempty initially. Prof. Amr Goneid, AUC 9
Key Table Operations search (k) bool : Search the table for the slot with key part that matches (k). If found, set p to the slot and return True, else return false insert (k,d) : insert an element at the end of the table. traverse: traverse table to print key and data fields. Prof. Amr Goneid, AUC 10
5. Class Template We will allow the class to receive the type of key and type of data stored in the class via parameters. This is called a class template We achieve this by doing the following: In the header file, declare the template class with its parameters, e.g., template <class keytype, class datatype> class KTable {.. }; Prof. amr Goneid, AUC 11
Class Template In the implementation file, every function is preceded by the class template. In the implementation file, the class name in a function header, is succeeded by the template < >. Example: // return True if table is full template <class keytype, class datatype> bool Ktable <keytype, datatype>::tableisfull() const { return (N == MaxSize-1); } Prof. amr Goneid, AUC 12
6. Element Specification // The element structure can be specified as a Class // in the private part of the main Ktable class. class element // Hidden from user { public: keytype key; // key datatype data; // Data }; // end of class element declaration Prof. Amr Goneid, AUC 13
7. Key Table Class Definition // File: Ktable.h // Definition of Ktable Template Class #ifndef KTABLE_H #define KTABLE_H // Specification of the class template <class keytype, class datatype> class Ktable { public: Prof. Amr Goneid, AUC 14
Key Table Class Definition // Member Functions Ktable(int nelements = 128); ~Ktable(); // Functions Prototype Definitions bool tableisempty() const; bool tableisfull() const; int occupancy() const; void update (const datatype & ); void retrieve (datatype &) const; void delete (); // Constructor // Destructor Prof. Amr Goneid, AUC 15
Key Table Class Definition bool search (const keytype & ); bool insert (const keytype &, const datatype & ); void traverse () const; private: // Element Class class element { public: keytype key; // key datatype data; // Data }; // end of class element declaration Prof. Amr Goneid, AUC 16
Key Table Class Definition element *T; int p; // Pointer to Storage Array // Pointer to current element // Maximum and index of last element int MaxSize, N; }; // end of Ktable Class definition #endif // KTABLE_H #include "Ktable.cpp" Prof. Amr Goneid, AUC 17
8. Key Table Class Implementation // File:Ktable.cpp Class implementation file #include <iostream> using namespace std; // Constructor with argument, size is nelements, default is 128 template <class keytype, class datatype> Ktable <keytype, datatype> ::Ktable(int nelements) { MaxSize = nelements; T = new element[maxsize]; p = -1; N = -1; } Prof. Amr Goneid, AUC 18
Key Table Class Implementation // Destructor template <class keytype, class datatype> Ktable <keytype, datatype> ::~Ktable() { delete [ ] T; } // return True if table is empty template <class keytype, class datatype> bool Ktable <keytype, datatype> ::tableisempty() const { return (N < 0); } Prof. Amr Goneid, AUC 19
Key Table Class Implementation // return True if table is full template <class keytype, class datatype> bool Ktable <keytype, datatype> ::tableisfull() const { return (N == MaxSize - 1); } // return the current occupancy of the table template <class keytype, class datatype> int Ktable <keytype, datatype> ::occupancy() const { return (N+1); } Prof. Amr Goneid, AUC 20
Key Table Class Implementation // to update the data in the current position template <class keytype, class datatype> void Ktable <keytype, datatype>::update (const datatype &d) { if ((p >= 0)&&(p <= N)) T[p].data = d; } // Retrieve the data part of the current position template <class keytype, class datatype> void Ktable<keyType, datatype>::retrieve (datatype &d) const { if ((p >= 0)&&(p <= N)) d = T[p].data; } Prof. Amr Goneid, AUC 21
Key Table Class Implementation // delete the current element. // After deletion, current element becomes empty template <class keytype, class datatype> void Ktable <keytype, datatype> ::delete () { if ((p >= 0)&&(p <= N)) { if (p < N) for (int i = p; i < N; i++) T[i] = T[i + 1]; N--; p = -1; } } Prof. Amr Goneid, AUC 22
Key Table Class Implementation // search the table for the element with key (k). // If found, set p to it and return True, else return false template <class keytype, class datatype> bool Ktable <keytype, datatype> ::search (const keytype &k) { bool found = false; p = -1; if(!tableisempty()) { i = 0; while ((! found) && (i <= N)) if (k == T[i].key) { found = true; p = i ; } else i++; } return found; } Prof. Amr Goneid, AUC 23
Key Table Class Implementation // insert element at the end of the table template <class keytype, class datatype> bool Ktable <keytype, datatype> :: insert (const keytype &k, const datatype &d) { if (!tableisfull()) { N++; T[N].key = k; T[N].data = d; return true ; } else return false; } Prof. Amr Goneid, AUC 24
Key Table Class Implementation // traverse table to print key and data fields template <class keytype, class datatype> void Ktable <keytype, datatype> ::traverse () const { for(int i = 0; i <= N; i++) cout << T[i].key << " " << T[i].data << endl; } Prof. Amr Goneid, AUC 25
9. Example Application Build a list of characters and their frequencies in a string: Given a string, search for every character in a key table. If a character does not exist in the table, insert it with a count of 1, otherwise, increment its count. Prof. Amr Goneid, AUC 26
Character Frequency Table // File: KTabletest.cpp // Applies Ktable Class #include <iostream> #include <string> using namespace std; #include "Ktable.h int main() { Ktable <char, int> ctable (50); string s; char c; int i, count; bool keyfound; Prof. Amr Goneid, AUC 27
Character Frequency Table // Read a string cout << "Enter a string:" << endl; getline(cin,s); cout << s << endl; // display it for (i = 0; i < s.length(); i++) // for every character { c = toupper(s.at(i)); // Search for character in the table keyfound = ctable.search (c); Prof. Amr Goneid, AUC 28
Character Frequency Table if (keyfound) // if found { ctable.retrieve (count); // get data count++; // increment count ctable.update (count); // store back } // Not found, a new element is inserted else ctable.insert(c,1); } Prof. Amr Goneid, AUC 29
Character Frequency Table } // print characters and their frequencies ctable.traverse(); // current table size cout << ctable,occupancy() << endl; // Free Memory ctable. ~Ktable(); return 0; Prof. Amr Goneid, AUC 30
Sample Output Enter a string: The New View The New View T 1 H 1 E 3 2 N 1 W 2 V 1 I 1 8 Press any key to continue Prof. Amr Goneid, AUC 31