impulse
10/02/2006, 10:45
tablo 7 elemana sahip (m=7). tablonun her bir elemanı (4 haneli pozitif tam sayı) ve (30 karakterlik dizge) bilgilerinden oluşmakta olup, arama alanına göre yapılmaktadır. Doğrama fonksiyonu olarak H(key)=key mod 7 kullanılacaktır. Aynı poziyona rastlayan birden fazla kayıt olması durumunda ise ayrı ayrı zincirleme yöntemi uygulanacaktır.
Programınızın aşağıdaki kayıtları doğrama tablosuna yerleştirmesi istenilmektedir:
1224 Ali Veli
2336 Mehmet Ahmet
1222 Ayşe Veli
2334 Ayşe Fatma
1226 Ahmet Mehmet
2332 Mehmet Ahmet
1228 Ali Ayşe
1230 Ali Mehmet
1232 Ahmet Veli
1234 Ahmet Ayşe
2330 Ayşe Mehmet
1236 Fatma Ali
1238 Mehmet Fatma
ve aşağıdaki sicil-no'ya sahip kayıtları arayarak, varsa bilgilerini ekrana yansıtması beklenmektedir:
1236
1238
2332
2333
Ben bişeyler yaptım ama eksiklerim ve hatalarım var yardımcı olur musunuz.
#include <stdio.h>
#include <stdlib.h>
static PNODE createNode(DATATYPE *pData)
{
PNODE pNode=(PNODE) malloc(sizeof(NODE));
if(pNode ==NULL)
return NULL;
pNode->val=*pData;
return pNode;
}
HHASH OpenHash(size_t tableSize)
{
HHASH hHash;
size_t i;
if((hHash=(HHASH) malloc(sizeof(HASH)))==NULL
return NULL;
hHash->pHashTable=(LLIST *)malloc(sizeof(LLIST) *tableSize);
if(hHash->pHashTable==NULL) {
free(hHash);
return NULL;
}
for(i=0; i<tableSize; ++i)
hHash->pHashTable[i].pHead=NULL;
hHash->tableSize=tableSize;
hHash->memberSize=0;
return hHash;
}
BOOL AddHashItem(HHASH hHash, DATATYPE *pdata)
{
size_t index;
NODE *pHead, *pNode;
if ((pNode=createNode(pData))==NULL)
return FALSE;
index=HashFunc(hHash, KEY(*pData));
pNODE->pNext=hHash->pHashTable[index].pHead;
hHash->pHashTable[index].pHead=pNode;
++hHash->memberSize;
return TRUE;
}
DATATYPE *GetHashItem(HHASH hHash, KEYTYPE key)
{
size_t index;
NODE*pNode;
index=HashFunc(hHash, key);
pNode=hHash->pHashTable[index].pHead;
while(pNode!=NULL) {
if (KEY(pNode->val)==key)
return &pNode->val;
pNode=pNode->pNext;
}
return NULL;
}
void CloseHash(HHASH hHash)
{
size_t i;
NODE *pNode, *pTempNode;
for(i=0; i<hHash->tableSize; ++i) {
pNode=hHash->pHashTable[i].pHead;
while(pNode!=NULL) {
pTempNode=pNode;
pNode=pNode->pNext;
free(pTempNode);
}
}
free(hHash->pHashTable);
free(hHash);
}
size_t GetMemberSize(HHASH hHash)
{
return hHash->memberSize;
}
size_t HashFunc(HHASH hHash, KEYTYPE key)
{
return key %hHash->tableSize;
}
#if 1
#include<conio.h>
#include<alloc.h>
void GetRandomPerson (PERSON*pPerson)
{
int i;
for(i=0; i<30; ++i)
pPerson-> name[i]= rand() %26 +'A';
pPerson->name[i]='\0';
pPerson->idno=rand() % 9999;
}
int main(void)
{
HHASH hHash;
PERSON *pPerson, tempPerson;
int i;
PERSON people[]= { {1224, "Ali Veli"}, {2336, "Mehmet Ahmet"}, {1222, "Ayse Veli"}, {2334, "Ayse Fatma"}, {1226, "Ahmet Mehmet"}, {2332, "Mehmet Ahmet},
{ 1228, "Ali Ayse"}, {1230, "Ali Mehmet"}, {1232, "Ahmet Veli"}, {1234, "Ahmet Ayse"}, {2330, "Ayse Mehmet"}, {1236, "Fatma Ali"}, {1238, "Mehmet Fatma"}, {-1} };
clrscr();
if ((hHash=OpenHash(101)==NULL) {
fprint(stderr, "Cannot open hash table!..\n");
exit(EXIT_FAILURE);
}
for(i=0;people[i].idno!=-1; ++i)
if(!AddHashItem(hHash, &people[i])) {
fprint(stderr, "Cannot add hash table!..\n");
exit(EXIT_FAILURE);
}
for(i=0; i<1000; i++) {
GetRandomPerson(&tempPerson);
if(!AddHashItem(hHash, &tempPerson)) {
fprint(stderr, "Cannot add hash table!..\n");
exit(EXIT_FAILURE);
}
}
printf("Tablodaki Toplam eleman: %u\n", GetMemberSize(hHash));
if((pPerson=GetHashItem(hHash, 2333))==NULL)
printf("Cannot find item!..\n");
else
printf("No:%d sim:%s\n", pPerson->idno, pPerson->name);
CloseHash(hHash);
return 0;
}
#endif
Programınızın aşağıdaki kayıtları doğrama tablosuna yerleştirmesi istenilmektedir:
1224 Ali Veli
2336 Mehmet Ahmet
1222 Ayşe Veli
2334 Ayşe Fatma
1226 Ahmet Mehmet
2332 Mehmet Ahmet
1228 Ali Ayşe
1230 Ali Mehmet
1232 Ahmet Veli
1234 Ahmet Ayşe
2330 Ayşe Mehmet
1236 Fatma Ali
1238 Mehmet Fatma
ve aşağıdaki sicil-no'ya sahip kayıtları arayarak, varsa bilgilerini ekrana yansıtması beklenmektedir:
1236
1238
2332
2333
Ben bişeyler yaptım ama eksiklerim ve hatalarım var yardımcı olur musunuz.
#include <stdio.h>
#include <stdlib.h>
static PNODE createNode(DATATYPE *pData)
{
PNODE pNode=(PNODE) malloc(sizeof(NODE));
if(pNode ==NULL)
return NULL;
pNode->val=*pData;
return pNode;
}
HHASH OpenHash(size_t tableSize)
{
HHASH hHash;
size_t i;
if((hHash=(HHASH) malloc(sizeof(HASH)))==NULL
return NULL;
hHash->pHashTable=(LLIST *)malloc(sizeof(LLIST) *tableSize);
if(hHash->pHashTable==NULL) {
free(hHash);
return NULL;
}
for(i=0; i<tableSize; ++i)
hHash->pHashTable[i].pHead=NULL;
hHash->tableSize=tableSize;
hHash->memberSize=0;
return hHash;
}
BOOL AddHashItem(HHASH hHash, DATATYPE *pdata)
{
size_t index;
NODE *pHead, *pNode;
if ((pNode=createNode(pData))==NULL)
return FALSE;
index=HashFunc(hHash, KEY(*pData));
pNODE->pNext=hHash->pHashTable[index].pHead;
hHash->pHashTable[index].pHead=pNode;
++hHash->memberSize;
return TRUE;
}
DATATYPE *GetHashItem(HHASH hHash, KEYTYPE key)
{
size_t index;
NODE*pNode;
index=HashFunc(hHash, key);
pNode=hHash->pHashTable[index].pHead;
while(pNode!=NULL) {
if (KEY(pNode->val)==key)
return &pNode->val;
pNode=pNode->pNext;
}
return NULL;
}
void CloseHash(HHASH hHash)
{
size_t i;
NODE *pNode, *pTempNode;
for(i=0; i<hHash->tableSize; ++i) {
pNode=hHash->pHashTable[i].pHead;
while(pNode!=NULL) {
pTempNode=pNode;
pNode=pNode->pNext;
free(pTempNode);
}
}
free(hHash->pHashTable);
free(hHash);
}
size_t GetMemberSize(HHASH hHash)
{
return hHash->memberSize;
}
size_t HashFunc(HHASH hHash, KEYTYPE key)
{
return key %hHash->tableSize;
}
#if 1
#include<conio.h>
#include<alloc.h>
void GetRandomPerson (PERSON*pPerson)
{
int i;
for(i=0; i<30; ++i)
pPerson-> name[i]= rand() %26 +'A';
pPerson->name[i]='\0';
pPerson->idno=rand() % 9999;
}
int main(void)
{
HHASH hHash;
PERSON *pPerson, tempPerson;
int i;
PERSON people[]= { {1224, "Ali Veli"}, {2336, "Mehmet Ahmet"}, {1222, "Ayse Veli"}, {2334, "Ayse Fatma"}, {1226, "Ahmet Mehmet"}, {2332, "Mehmet Ahmet},
{ 1228, "Ali Ayse"}, {1230, "Ali Mehmet"}, {1232, "Ahmet Veli"}, {1234, "Ahmet Ayse"}, {2330, "Ayse Mehmet"}, {1236, "Fatma Ali"}, {1238, "Mehmet Fatma"}, {-1} };
clrscr();
if ((hHash=OpenHash(101)==NULL) {
fprint(stderr, "Cannot open hash table!..\n");
exit(EXIT_FAILURE);
}
for(i=0;people[i].idno!=-1; ++i)
if(!AddHashItem(hHash, &people[i])) {
fprint(stderr, "Cannot add hash table!..\n");
exit(EXIT_FAILURE);
}
for(i=0; i<1000; i++) {
GetRandomPerson(&tempPerson);
if(!AddHashItem(hHash, &tempPerson)) {
fprint(stderr, "Cannot add hash table!..\n");
exit(EXIT_FAILURE);
}
}
printf("Tablodaki Toplam eleman: %u\n", GetMemberSize(hHash));
if((pPerson=GetHashItem(hHash, 2333))==NULL)
printf("Cannot find item!..\n");
else
printf("No:%d sim:%s\n", pPerson->idno, pPerson->name);
CloseHash(hHash);
return 0;
}
#endif