1 / 31

CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++

Learn about implementing a key table class as a dictionary ADT with insert, delete, and search operations. Includes data members, operations, and template usage.

fcastaneda
Download Presentation

CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCE 110PROGRAMMINGFUNDAMENTALSWITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC

  2. Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC

  3. 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

  4. 1. The Key Table as a Dictionary • A key table is essentially a DictionaryADT. • 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

  5. 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 • CurrentElement: special element in the table, indicated by a pointer to the currentposition. Prof. Amr Goneid, AUC

  6. 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

  7. 3. Key Table Data Members • Elements. Each element has: • A key of type keyType • 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

  8. 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

  9. Key Table Operations • update (d) : to update the data portion of the currentelement to contain d; assume the current position is nonempty. • retrieve (d): to return the data (d) in the currentelement; assume the current position is nonempty. • delete: delete the currentelement. Assume the current position is nonempty initially. Prof. Amr Goneid, AUC

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. Key Table Class Definition // Member Functions Ktable(int nelements = 128); // Constructor ~Ktable(); // Destructor // Functions Prototype Definitions bool tableIsEmpty() const; bool tableIsFull() const; int occupancy() const; void update (const dataType & ); void retrieve (dataType &) const; void delete (); Prof. Amr Goneid, AUC

  16. 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

  17. Key Table Class Definition element *T; // Pointer to Storage Array int p; // 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

  18. 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(intnelements) { MaxSize = nelements; T = new element[MaxSize]; p = -1; N = -1; } Prof. Amr Goneid, AUC

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

More Related