Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //HashTable
- #pragma once
- #include "HashObj.h"
- class HashTable {
- protected:
- friend class HashObject;
- int length;
- int count;
- HashObject* niz;
- int f(char* x) {
- int i = 0;
- while (x[i + 1] != 0) {
- i++;
- }
- int rez = x[0] * 13 + x[i] * 13;
- return rez % length;
- }
- int g(int x, char* s) {
- int z = x + 4;
- while (niz[z].getStatus() != 0 && z != x && strcmp(niz[z].getKey(), s) != 0) {
- z = z + 4;
- }
- if (z == x)
- return -1;
- return z;
- }
- int h(char* x) {
- if (niz[f(x)].getStatus() == 0 || strcmp(niz[f(x)].getKey(), x) == 0 ){
- return f(x);
- }
- if (g(f(x), x) < 0) {
- return -1;
- }
- return g(f(x), x);
- }
- public:
- HashTable() {
- length = 150;
- count = 0;
- niz = new HashObject[length];
- }
- int getLength() {
- return length;
- }
- int getLoadFactor() {
- return count / length;
- }
- void add(char* s) {
- if (h(s) > 0) {
- niz[h(s)].changeValue(s);
- count++;
- }
- else cout << "Sva mesta zauzeta\n";
- }
- void del(char* s) {
- if (count == 0) {
- cout << "Tablica je prazna";
- return;
- }
- int x = h(s);
- if (x < 0)
- cout << "Taj el. ne postoji";
- niz[x].brisi();
- count--;
- }
- void stampaj() {
- for (int i = 0; i < length; i++) {
- if (niz[i].getStatus() == 1) cout << niz[i] << " mesto:" << i << endl;
- }
- }
- ~HashTable() {
- if (niz)
- delete[] niz;
- }
- };
- //HashObject
- #ifdef _MSC_VER
- #define _CRT_SECURE_NO_WARNINGS
- #endif
- #pragma once
- #include <iostream>
- using namespace std;
- class HashObject {
- public:
- char* key;
- int record;
- int status;
- public:
- HashObject() {
- key = 0;
- record = -1;
- status = 0;
- }
- HashObject(char* s) {
- strcpy(key, s);
- cout << "Unesite record:\n";
- cin >> record;
- status = 1;
- }
- HashObject(char* s1, int x) {
- strcpy(key, s1);
- record = x;
- status = 1;
- }
- void changeValue(char* s) {
- if (key != 0)
- delete[] key;
- key = new char[strlen(s) + 1];
- strcpy(key, s);
- cout << "Unesite record:\n";
- cin >> record;
- status = 1;
- }
- char* getKey() {
- if (key != 0)
- return key;
- else
- return " ";
- }
- int getRecord() {
- return record;
- }
- int getStatus() {
- return status;
- }
- void brisi() {
- delete[] key;
- key = 0;
- record = -1;
- status = -1;
- }
- friend ostream& operator<<(ostream& str, HashObject& h) {
- str << h.getKey() << "\t" << h.getRecord();
- return str;
- }
- };
- // mejn
- #pragma once
- #include "HashTable.h"
- void main() {
- HashTable tabla;
- tabla.add("public");
- tabla.add("protected");
- tabla.add("const");
- tabla.add("concept");
- tabla.add("pc");
- tabla.stampaj();
- cout << endl;
- tabla.del("pc");
- tabla.stampaj();
- cout << endl;
- tabla.del("const");
- tabla.stampaj();
- cout << endl;
- tabla.del("concept");
- tabla.stampaj();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement