Hash Table application (C ++ implementation)

Source: Internet
Author: User
Problem description: design a hash table to query telephone numbers and implement the following functions:
(1) assume that each record has the following data items: phone number, user name, and address.
(2) In the hosts file and displayed on the screen, the number of records should not be less than 30, and a hash table is created with the telephone number and user name as keywords respectively.
(3) use pseudo-random detection and re-hash to resolve conflicts.
(4) search for and display the records of the given phone number; search for and display the records of the given user name.
(5) Save the unfound result to out.txt. A prompt statement is required before the search result is displayed.

Code:

// Myhashtable. cpp: defines the entry point of the console application. /// Design a hash table to query telephone numbers. // Note: first, the data read from the old.txt file is created before the program is run. // second, the data is randomly generated by the system, when the program runs, it is generated by the random number generator and the generated records are saved to the new.txt file. // Problem: Garbled characters are displayed when randomly generated files are used. # include "stdafx. H "# include <fstream> // file stream # include <iostream> # include <string> using namespace STD; const int d [] = {3, 5, 8, 11, 13, 14, 19,21}; // pre-scheduled random number const int hash_maxsize = 50; // hash table length // record information type class datainfo {public: datainfo (); // default constructor friend ostream & operator <(ostream & out, const datainfo & datainfo); // overload output operator // friend class hashtable; // Private: string name; // name string phone; // phone number S Tring address; // address char sign; // The identifier of the conflict. '1' indicates a conflict, and '0' indicates no conflict.}; datainfo: datainfo (): name (""), phone (""), address (""), sign ('0') {} ostream & operator <(ostream & out, const datainfo & datainfo) // reload the output operator {cout <"name:" <datainfo. name <"Phone:" <datainfo. phone <"Address:" <datainfo. address <Endl; Return out;} // class hashtable {public: hashtable (); // default constructor ~ Hashtable (); // destructor int random (INT key, int I); // handle conflicting void hashname (datainfo * datainfo) by means of pseudo-random number detection and hash ); // create a hash table int rehash (INT key, string Str) with the name as the keyword ); // use the hash method to handle conflicts. Pay attention to the handling of conflicts and void hashphone (datainfo * datainfo) such as the link address method. // create a hash table void Hash (char * fname, int N); // create a hash table // fname is the name of the data storage file, used for input data, and N is the user-selected search method int findname (string name ); // search for the key code int findphone (string phone) corresponding to the record in the hash table by name; // search for the key code void outhas corresponding to the record in the hash table by phone H (INT key); // output the void OUTFILE (string name, int key) record corresponding to the keyword code in the hash table ); // output the unfound record void rafile () When not found; // generate a random file and save the file in the new.txt file void writemedildtxt (); // write data before running // Private: datainfo * value [hash_maxsize]; int length; // hash table length}; hashtable: hashtable (): length (0) // default constructor {// memset (value, null, hash_maxsize * sizeof (datainfo *); For (INT I = 0; I 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.