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

Slides:



Advertisements
Similar presentations
Linear Lists – Array Representation
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Introduction to Stacks & Queues.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11a. The Vector Class.
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Data Structures Using C++ 2E
Review of C++ Programming Part II Sheng-Fang Huang.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
1 Object-Oriented Programming Using C++ CLASS 27.
Templates Zhen Jiang West Chester University
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
Data Structures.  Consider storing data for 100 employees by their Social Security Numbers (SSN)  SSN range: –  A fast search:
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R2. Binary Search Trees.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11. The Struct Data Type.
Lists. Container Classes Many applications in Computer Science require the storage of information for collections of entities e.g. a student registration.
Pointers OVERVIEW.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
CSCE 210 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
CS505 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
CSCI 210 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Pointers & Dynamic Data Structures
CS410 – Software Engineering Lecture #5: C++ Basics III
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

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

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

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

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

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.

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.

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

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

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.

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.

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 { …….. };

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); }

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

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:

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 ();

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

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"

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; }

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); }

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); }

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; }

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; }

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; }

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; }

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; }

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.

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;

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);

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); }

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; }

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