Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class."— Presentation transcript:

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

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

3 Prof. Amr Goneid, AUC3 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

4 Prof. amr Goneid, AUC4 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

5 Prof. Amr Goneid, AUC5 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.

6 Prof. Amr Goneid, AUC6 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.

7 Prof. Amr Goneid, AUC7 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).

8 Prof. Amr Goneid, AUC8 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

9 Prof. Amr Goneid, AUC9 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.

10 Prof. Amr Goneid, AUC10 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.

11 Prof. amr Goneid, AUC11 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 KTable { …….. };

12 Prof. amr Goneid, AUC12 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 bool Ktable ::tableIsFull() const { return (N == MaxSize-1); }

13 Prof. Amr Goneid, AUC13 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

14 Prof. Amr Goneid, AUC14 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 Ktable { public:

15 Prof. Amr Goneid, AUC15 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 ();

16 Prof. Amr Goneid, AUC16 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

17 Prof. Amr Goneid, AUC17 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"

18 Prof. Amr Goneid, AUC18 8. Key Table Class Implementation // File:Ktable.cpp Class implementation file #include using namespace std; // Constructor with argument, size is nelements, default is 128 template Ktable ::Ktable(int nelements) { MaxSize = nelements; T = new element[MaxSize]; p = -1; N = -1; }

19 Prof. Amr Goneid, AUC19 Key Table Class Implementation // Destructor template Ktable ::~Ktable() { delete [ ] T; } // return True if table is empty template bool Ktable ::tableIsEmpty() const {return (N < 0); }

20 Prof. Amr Goneid, AUC20 Key Table Class Implementation // return True if table is full template bool Ktable ::tableIsFull() const {return (N == MaxSize - 1); } // return the current occupancy of the table template int Ktable ::occupancy() const {return (N+1); }

21 Prof. Amr Goneid, AUC21 Key Table Class Implementation // to update the data in the current position template void Ktable ::update (const dataType &d) { if ((p >= 0)&&(p <= N)) T[p].data = d; } // Retrieve the data part of the current position template void Ktable ::retrieve (dataType &d) const { if ((p >= 0)&&(p <= N)) d = T[p].data; }

22 Prof. Amr Goneid, AUC22 Key Table Class Implementation // delete the current element. // After deletion, current element becomes empty template void Ktable ::delete () { if ((p >= 0)&&(p <= N)) { if (p < N) for (int i = p; i < N; i++) T[i] = T[i + 1]; N--; p = -1; }

23 Prof. Amr Goneid, AUC23 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 bool Ktable ::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; }

24 Prof. Amr Goneid, AUC24 Key Table Class Implementation // insert element at the end of the table template bool Ktable :: insert (const keyType &k, const dataType &d) { if (!tableIsFull()) { N++; T[N].key = k; T[N].data = d; return true ; } else return false; }

25 Prof. Amr Goneid, AUC25 Key Table Class Implementation // traverse table to print key and data fields template void Ktable ::traverse () const { for(int i = 0; i <= N; i++) cout << T[i].key << " " << T[i].data << endl; }

26 Prof. Amr Goneid, AUC26 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.

27 Prof. Amr Goneid, AUC27 Character Frequency Table // File: KTabletest.cpp // Applies Ktable Class #include using namespace std; #include "Ktable.h“ int main() { Ktable ctable (50); string s; char c; int i, count; bool keyfound;

28 Prof. Amr Goneid, AUC28 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);

29 Prof. Amr Goneid, AUC29 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); }

30 Prof. Amr Goneid, AUC30 Character Frequency Table // print characters and their frequencies ctable.traverse(); // current table size cout << ctable,occupancy() << endl; // Free Memory ctable. ~Ktable(); return 0; }

31 Prof. Amr Goneid, AUC31 Sample Output Enter a string: The New View T 1 H 1 E 3 2 N 1 W 2 V 1 I 1 8 Press any key to continue


Download ppt "Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class."

Similar presentations


Ads by Google