นี่คือโปรแกรม C++ สำหรับใช้งานตารางที่อยู่ตรง (Direct Addressing Tables) ใช้เมื่อแต่ละองค์ประกอบมีคีย์มาจากเซตอวกาศ S = {0, 1, ..., n-1} โดยที่ n ไม่ใหญ่เกินไป และแต่ละคีย์ไม่ซ้ำกัน ช่วยให้การแทรก ค้นหา และลบทำงานได้เร็ว
ฟังก์ชันและ Pseudocode
Begin
insert():
รับตัวแปรตาราง word และ key
T[x.key] = x โดย x คือค่าของ key
delete():
รับตัวแปรตาราง word และ key
T[x.key] = tab(0, "")
search():
คืนค่า T[key]
End
ตัวอย่างโค้ด
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstdio>
using namespace std;
struct tab {
string word;
int key;
tab() {}
tab(int k, string w) {
word = w;
key = k;
}
};
void INSERT(tab T[], tab x) {
T[x.key] = x;
}
void DELETE(tab T[], tab x) {
T[x.key] = tab(0, "");
}
tab SEARCH(tab T[], int key) {
return T[key];
}
int main() {
int i, k, c;
string str;
tab T[65536];
tab x;
for (i = 0; i < 65536; i++)
T[i] = tab(0, "");
while (1) {
cout << "1.Insert element into the key" << endl;
cout << "2.Delete element from the table" << endl;
cout << "3.Search element into the table" << endl;
cout << "4.Exit" << endl;
cout << "Enter your Choice: ";
cin >> c;
switch (c) {
case 1: {
string str1 = "";
cout << "Enter the key value: ";
cin >> k;
cout << "Enter the string to be inserted: ";
cin.ignore();
getline(cin, str);
INSERT(T, tab(k, str));
break;
}
case 2:
cout << "Enter the key of element to be deleted: ";
cin >> k;
x = SEARCH(T, k);
DELETE(T, x);
break;
case 3:
cout << "Enter the key of element to be searched: ";
cin >> k;
x = SEARCH(T, k);
if (x.key == 0) {
cout << "No element inserted at the key" << endl;
continue;
}
cout << "Element at key " << k << " is-> " << "\"" << x.word << "\"" << endl;
break;
case 4:
exit(1);
default:
cout << "Wrong Choice" << endl;
}
}
return 0;
}
ผลลัพธ์
1.Insert element into the key 2.Delete element from the table 3.Search element into the table 4.Exit Enter your Choice: 1 Enter the key value: 1 Enter the string to be inserted: hi 1.Insert element into the key 2.Delete element from the table 3.Search element into the table 4.Exit Enter your Choice: 1 Enter the key value: 2 Enter the string to be inserted: tutorials 1.Insert element into the key 2.Delete element from the table 3.Search element into the table 4.Exit Enter your Choice: 1 Enter the key value: 3 Enter the string to be inserted: point 1.Insert element into the key 2.Delete element from the table 3.Search element into the table 4.Exit Enter your Choice: 3 Enter the key of element to be searched: 1 Element at key 1 is-> "hi" 1.Insert element into the key 2.Delete element from the table 3.Search element into the table 4.Exit Enter your Choice: 3 Enter the key of element to be searched: 4 No element inserted at the key 1.Insert element into the key 2.Delete element from the table 3.Search element into the table 4.Exit Enter your Choice: 2 Enter the key of element to be deleted: 1 1.Insert element into the key 2.Delete element from the table 3.Search element into the table 4.Exit Enter your Choice: 3 Enter the key of element to be searched: 1 No element inserted at the key 1.Insert element into the key 2.Delete element from the table 3.Search element into the table 4.Exit Enter your Choice: 4