I need help completing the code, the missing part was declared by / *, I hope yo
ID: 3555466 • Letter: I
Question
I need help completing the code, the missing part was declared by / *, I hope you can help me.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 102
#define M 8
#define T 1000
struct data{
char regnum[M], name[N], collegecareer[N];
struct data *s, *a;
};
struct leaf{
struct data* data;
struct leaf *i, *d, *p;
};
struct leaf *table[1000];
struct data *lista = NULL;
struct data *add(char *regnum, char *name, char *collegecareer){
/*
This function should add a new node
struct data type to the linked list
and return the address of the new node
*/
}
struct leaf *new(struct data *data, struct leaf *leaf){
/*
This function should create a new leaf of type struct leaf and return address of the new data
*/
}
struct leaf *adjust(struct data *data, struct leaf *leaf, int hk){
/*
accommodates a new leaf in the tree has corresponding
*/
if(table[hk]){//if the tree is not empty
if(strcmp(data->regnum, leaf->data->regnum)<0){//to the left
if(leaf->i){
return adjust(data, leaf->i, hk);
}else{
return leaf->i = new(data, leaf);
}
}else{
if(leaf->d){//to the right
return adjust(data, leaf->d, hk);
}else{
return leaf->d = new(data, leaf);
}
}
}else{//if the tree is empty
return table[hk] = new(data, table[hk]);
}
}
int h(char *k){
/*
creates a hash key for registration number
*/
int i, j, l;
char s[4];
l = strlen(k);
for(i=0, j=l-3; i<=3 && j<=l; j++, i++){
s[i] = k[j];
}
return atoi(s);
}
struct leaf *insert(char *regnum, char *name, char *collegecareer){
int hk = h(regnum);
struct data *data = add(regnum, name, collegecareer);
if(data){
return adjust(data, table[hk], hk);
}
return NULL;
}
void load(void){
/*
load the data coming in the sample file
*/
FILE *input = fopen("datos.txt", "r");
char regnum[M], name[N], collegecareer[N];
int i;
if(input){
for(i=0; i<T; i++){
table[i] = NULL;
}
while(!feof(input)){
fgets(regnum, M, input);
fgets(name, N, input);
fgets(collegecareer, N, input);
insert(regnum, name, collegecareer);
}
fclose(input);
}else{
printf("Unable to read the input file ");
}
}
struct leaf *search_b(char *regnum, struct leaf *leaf){
/*
search inside a binary tree and returns the leaf if it exists
*/
}
struct data *search(char *regnum){
/*
Search the hash table based on enrollment
and returns the node in the linked list, ie
full data
*/
int hk = h(regnum);
struct leaf *leaf = search_b(regnum, table[hk]);
if(leaf){
return leaf->data;
}
return NULL;
}
struct leaf *search_h(char *regnum){
/*
Search the corresponding leaf in the tree (h(m)) corresponding
*/
int hk = h(regnum);
struct leaf *leaf = search_b(regnum, table[hk]);
return leaf;
}
int right(struct leaf *father, struct leaf *son){
return (father->d == son? 1: 0);
}
struct leaf *successor(struct leaf *leaf){
if(leaf->i)
return successor(leaf->i);
return leaf;
}
void erase(struct data *data){
/*
Remove a node from the linked list
*/
if(data->a)
data->a->s = data->s;
if(data->s)
data->s->a = data->a;
free(data);
}
void eliminate(struct leaf* leaf, int hk){
struct leaf *p;
if(leaf){
if(leaf->i && !leaf->d){
p = leaf->i;
erase(leaf->data);
leaf->data = p->data;
leaf->i = p->i;
leaf->d = p->d;
if(leaf->i)
leaf->i->p = leaf;
if(leaf->d)
leaf->d->p = leaf;
free(p);
return;
}
if(!leaf->i && leaf->d){
p = leaf->d;
erase(leaf->data);
leaf->data = p->data;
leaf->i = p->i;
leaf->d = p->d;
if(leaf->i)
leaf->i->p = leaf;
if(leaf->d)
leaf->d->p = leaf;
free(p);
return;
}
if(leaf->i && leaf->d){
p = successor(leaf->d);
erase(leaf->data);
leaf->data = p->data;
eliminate(p, hk);
return;
}
if(leaf->p){
if(right(leaf->p, leaf)){
leaf->p->d = NULL;
}else{
leaf->p->i = NULL;
}
}else{
table[hk] = NULL;
}
erase(leaf->data);
free(leaf);
}
}
int main(int argc, char **args){
char op, regnum[M], name[N], collegecareer[N];
int loaded=0;
struct data *data;
struct leaf *leaf;
load();
do{
system("clear");
printf(" HASH ");
printf("a) New data ");
printf("b) Search data ");
printf("c) Change data ");
printf("d) Eliminate data ");
printf("0) exit ");
op = getchar();
getchar();
switch(op){
case 'a':
case 'A':
printf("New registration number:"); fgets(regnum, M, stdin);
printf("New name: "); fgets(name, N, stdin);
printf("New college career: "); fgets(collegecareer, N, stdin);
if(add(regnum, name, collegecareer)){
printf("ready ");
}else{
printf("It could not be added ");
}
break;
case 'b':
case 'B':
printf("Give me you registration number: "); fgets(regnum, M, stdin);
data = search(regnum);
if(data){
printf("registration number: %s", data->regnum);
printf("Name: %s", data->name);
printf("college career: %s", data->collegecareer);
}else{
printf("Can not be found ");
}
break;
case 'c':
case 'C':
printf("Give me you registration number: "); fgets(regnum, M, stdin);
data = search(regnum);
if(data){
printf("registration number: %s", data->regnum);
printf("Name: %s", data->name);
printf("collegecareer: %s", data->collegecareer);
printf("New Name: "); fgets(data->name, N, stdin);
printf("New college career : "); fgets(data->collegecareer, N, stdin);
}else{
printf("Can not be found ");
}
break;
case 'd':
case 'D':
printf("Give me you registration number: "); fgets(regnum, M, stdin);
leaf = search_h(regnum);
if(leaf){
printf("%s%s ", leaf->data->name, leaf->data->collegecareer);
eliminate(leaf, h(regnum));
}
break;
}
getchar();
}while(op != '0');
printf("Bye ");
return 0;
}
Explanation / Answer
I already have a fully implemented hash table in C so just giving that may be it helps you.....if it helps please rate me..
//hash table with chaining
//code in c
#include
#include
#include
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.