e08-01.cpp

来自「游戏开发数据结构Data Structures for Game Program」· C++ 代码 · 共 83 行

CPP
83
字号
// =======================================================
//  Chapter 8, Example 1
//  Using the Hash Table
// =======================================================
#include "HashTable.h"
#include <iostream.h>


unsigned long int Hash( int k )
{
    return k;
}


void main()
{
    int size;
    int choice = 1;
    int key;
    int data;

    cout << "Enter the size of the table: ";
    cin >> size;

    HashTable<int, int> table( size, Hash );
    HashEntry<int, int>* entry;

    while( choice != 0 )
    {
        cout << endl;
        cout << "0 - Quit" << endl;
        cout << "1 - Insert data" << endl;
        cout << "2 - Find key" << endl;
        cout << "3 - Remove key" << endl;
        cout << "Your Choice: ";
        cin >> choice;
        cout << endl;

        switch( choice )
        {
        case 1:
            cout << "Key: ";
            cin >> key;
            cout << "Data: ";
            cin >> data;
            table.Insert( key, data );
            cout << "Inserted (" << key << ", " << data << ")" << endl;
            break;
        case 2:
            cout << "Key: ";
            cin >> key;
            entry = table.Find( key );
            if( entry == 0 )
            {
                cout << "Key not found!" << endl;
            }
            else
            {
                cout << "Data Found: (" << entry->m_key << ", ";
                cout << entry->m_data << ")" << endl;
            }
            break;
        case 3:
            cout << "Key: ";
            cin >> key;
            entry = table.Find( key );
            if( entry == 0 )
            {
                cout << "Key not found!" << endl;
            }
            else
            {
                cout << "Data Removed: (" << entry->m_key << ", ";
                cout << entry->m_data << ")" << endl;
            }
            table.Remove( key );
            break;
        }
    }



}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?