Hash table that uses the link address method to resolve conflicts

Source: Internet
Author: User

/*
Author: Xu **
Date: 12.29 918
Version: V1.0
Role: simple employee management system
Each employee's information includes: number, name, gender, date of birth,
Education, job title, phone number, address, etc. The system functions include:
1. Query: Search for employees based on specific conditions
2. Modify: Modify the information of an employee by number.
3. insert: new employee information
4. Delete: delete employee information by number
5. Sorting: sort the information of all employees according to specific conditions.
*/
# Include <iostream>
Using namespace STD;
# Include <stdio. h>
# Include <string. h>

# Define hashkey 13
# Define nullkey-1
# Define max 20

Typedef struct employee
{
Int key_code;
Char name [30];
Char sex;
Char borndate [30];
Char education [10];
Char position [15];
Char phone [15];
Char ADDR [30];
Struct employee * next;
} Employee;

Typedef struct hashtable // use the link address method to solve the conflict
{
Int key;
Struct employee * next;
} Hashtable [Max];

/// // Function /////////////////
Int Hash (INT key) // Hash Function
{
Int mode = Key % hashkey;
Return mode;
}
Void hash_init (hashtable HT) // hash table Initialization
{
Int I;
For (I = 0; I <Max; I ++)
{
HT [I]. Key = nullkey;
HT [I]. Next = NULL;
}
}
Int hash_insert (hashtable HT, employee * em)
{
Int key = hash (EM-> key_code );
Employee * P;
If (HT [Key]. Key = nullkey)
{
HT [Key]. Key = key;
HT [Key]. Next = em;
}
Else if (HT [Key]. Key = Key) // if a conflict occurs, use the link address to resolve the conflict.
{
P = HT [Key]. Next;
While (p-> next! = NULL) // find the insert position and insert it to the end of the table Synonym
{
P = p-> next;
}
P-> next = em;
}
Return 1;
}
Employee * hash_search (hashtable HT, int key)
{
Int p0 = hash (key );
If (HT [P0]. Key = nullkey) return NULL;
Else if (HT [P0]. Key = P0) // if a group of the same words is found, search for them in the synonym
{
Employee * P = HT [P0]. Next;
While (P! = NULL)
{
If (p-> key_code = key)
{
Return P;
}
P = p-> next;
}
}
Return NULL;
}

Void modify (employee * em)
{
Int choice = 0;
Cout <"what to modify ====" <Endl;
Cout <"1. Position" <Endl;
Cout <"2. Phone" <Endl;
Cout <"3. ADDR" <Endl;
Cout <Endl <"option --";
Cin> choice;

Switch (choice)
{
Case 1:
Char position [10];
Memset (Position, 0x00, sizeof (position ));
Cout <"change :";
Cin> position;
Strcpy (EM-> position, position );
Cout <"OK! "<Endl;
Break;
Case 2:
Char phone [15];
Memset (phone, 0x00, sizeof (phone ));
Cout <"change :";
Cin> phone;
Strcpy (EM-> Phone, phone );
Cout <"OK! "<Endl;
Break;
Case 3:
Char ADDR [30];
Memset (ADDR, 0x00, sizeof (ADDR ));
Cout <"change :";
Cin> ADDR;
Strcpy (EM-> ADDR, ADDR );
Cout <"OK! "<Endl;
Break;
Default:
Cout <"you have selected an error. Please see the option number ~ "<Endl;
}
}
Int hash_modify (hashtable HT, int key)
{
Int p0 = hash (key );
If (HT [P0]. Key = nullkey) return nullkey;
Else if (HT [P0]. Key = P0)
{
Employee * P = HT [P0]. Next;
While (P! = NULL)
{
If (p-> key_code = key)
{
Modify (P );
Return P0;
}
P = p-> next;
}
}
Return nullkey;
}
Int hash_delete (hashtable HT, int key)
{
Int p0 = hash (key );
If (HT [P0]. Key = nullkey) return nullkey;
Else if (HT [P0]. Key = P0)
{
Employee * P = HT [P0]. Next, * pre = P;
While (P! = NULL)
{
If (p-> key_code = key)
{
If (P = HT [P0]. Next)
{
HT [P0]. Next = p-> next;
}
Else
{
Pre-> next = p-> next;
}
Free (P );
Return P0;
}
Pre = P;
P = p-> next;
}
}
Return nullkey;
}
Void swap (employee * Em1, employee * EM2)
{
Employee em_temp;
Em_temp.key_code = Em1-> key_code;
Em1-> key_code = EM2-> key_code;
EM2-> key_code = em_temp.key_code;
Strcpy (em_temp.addr, Em1-> ADDR );
Strcpy (Em1-> ADDR, EM2-> ADDR );
Strcpy (EM2-> ADDR, em_temp.addr );
Strcpy (em_temp.borndate, Em1-> borndate );
Strcpy (Em1-> borndate, EM2-> borndate );
Strcpy (EM2-> borndate, em_temp.borndate );
Strcpy (em_temp.education, Em1-> Education );
Strcpy (Em1-> Education, EM2-> Education );
Strcpy (EM2-> Education, em_temp.education );
Strcpy (em_temp.name, Em1-> name );
Strcpy (Em1-> name, EM2-> name );
Strcpy (EM2-> name, em_temp.name );
Strcpy (em_temp.phone, Em1-> phone );
Strcpy (Em1-> phone, EM2-> phone );
Strcpy (EM2-> phone, em_temp.phone );
Strcpy (em_temp.position, Em1-> position );
Strcpy (Em1-> position, EM2-> position );
Strcpy (EM2-> position, em_temp.position );
Em_temp.sex = Em1-> sex;
Em1-> sex = EM2-> sex;
EM2-> sex = em_temp.sex;
}
Int hash_sort (hashtable HT)
{
Int K;
For (k = 0; k <Max; k ++)
{
If (HT [K]. Key = nullkey)
Continue;
Else
{
Employee * P = HT [K]. Next, * q = p-> next;
While (P! = NULL)
{
While (Q! = NULL)
{
If (p-> key_code> q-> key_code)
{
Swap (p, q );
}
Q = Q-> next;
}
P = p-> next;
}
}
}
Return 1;
}
Employee * record_input ()
{
Employee * em = (employee *) malloc (sizeof (employee ));
Cout <"/T enter the employee ID :";
Cin> em-> key_code;
Cout <"/T enter the employee's name :";
Cin> em-> name;
Cout <"/T enter the gender (N or F ):";
Cin> em-> sex;
Cout <"/T enter the Date of Birth :";
Cin> em-> borndate;
Cout <"/T enter the education level :";
Cin> em-> education;
Cout <"/T enter the position :";
Cin> em-> position;
Cout <"/T enter the phone number :";
Cin> em-> phone;
Cout <"/T enter the residential address :";
Cin> em-> ADDR;
Em-> next = NULL;

Return em;
}

Int hash_create (hashtable HT)
{
Int COUNT = 1;
Employee * em;
Hash_init (HT );
// For (COUNT = 0; count <3; count ++) // create records for three employees first
Cout <"/T ================= create an employee information table ======================" <Endl;
While (count! = 0)
{
Em = record_input ();

If (hash_search (HT, Em-> key_code) = NULL)
Hash_insert (HT, EM );
Else
{
Count --;
Cout <"No. cannot be repeated ~ -_-"<Endl;
Cout <"is C re-input? 1 or 0, choose :";
Cin> count;
If (count! = 0)
Continue;
Else
Break;
}
Cout <"enter an employee again? 1 or 0, choose :";
Cin> count;
}

Printf ("/ncreate successfully! /N ");
Return 1;
}

Int menu ()
{
Int choice = 0;
Cout <"/T ========================= employee management system ================= ====== "<Endl;
Cout <"/T1, query: Search for employees by specific conditions (by number)" <Endl;
Cout <"/T2, modify: Modify an employee's information by number (by number)" <Endl;
Cout <"/T3, insert: new employee information" <Endl;
Cout <"/T4, delete: delete employee information by number" <Endl;
Cout <"/T5, sorting: sort the information of all employees according to specific conditions" <Endl;
Cout <"/T0, exit: exit the system" <Endl;
Cout <"/T ========================================= ===================" <Endl;
Cout <Endl <"/toption --";
Cin> choice;
Return choice;
}
//////////////////////////////////////// ////////
Int main ()
{
Int choice = 0, key = 0;
Employee * em;
Hashtable HT;
// Hash_init (HT );
Hash_create (HT );
Cout <"------------------ welcome to use this system ~ -------------------- "<Endl;
While (choice = menu ())
{
Switch (choice)
{
Case 1:
Cout <"/T enter employee ID :";
Cin> key;
Em = hash_search (HT, key );
If (em! = NULL)
{
Cout <"/T employee No.:" <em-> key_code <Endl ;;
Cout <"/T employee name:" <em-> name <Endl ;;
Cout <"/T gender (N or F):" <em-> sex <Endl;
Cout <"/T Date of Birth:" <em-> borndate <Endl;
Cout <"/T education level:" <em-> Education <Endl;
Cout <"/t position:" <em-> position <Endl;
Cout <"/T phone number:" <em-> phone <Endl;
Cout <"/T residential address:" <em-> ADDR <Endl;
}
Else
Cout <"/t does not have this employee ~ -_-"<Endl;
Break;
Case 2:
Cout <"/T enter the employee ID to be modified :";
Cin> key;
If (hash_search (HT, key )! = NULL)
If (-1! = Hash_modify (HT, key ))
Cout <"/T modified successfully! Performance_^ "<Endl;
Else
Cout <"/t does not have this employee ~ -_-"<Endl;
Break;
Case 3:
Em = record_input ();
If (hash_search (HT, Em-> key_code) = NULL)
If (hash_insert (HT, EM) cout <"/T inserted successfully! Performance_^ "<Endl;
Else
Cout <"/T already has this employee! Insertion failed! -_-"<Endl;
Break;
Case 4:
Cout <"/T enter the employee ID to delete :";
Cin> key;
If (-1! = Hash_delete (HT, key ))
Cout <"/T deleted successfully! Performance_^ "<Endl;
Else
Cout <"/T has not changed employee! -_-"<Endl;
Break;
Case 5:
Hash_sort (HT );
Cout <"/t finished sorting ~ Performance_^ "<Endl;
Break;
Default:
Cout <"/t you have selected an error. Please see the number of options ~ "<Endl;
}
}
Cout <"/T ---------- thanks for using this system! --------- "<Endl;
Cout <"/t" <Endl;
Return 0;
}

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.