


































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Data structure: Nodes; each contains key/value pair and pointer to next node
Typology: Slides
1 / 74
This page cannot be seen from the preview
Don't miss anything!



































































struct Node { const char *key; int value; struct Node *next; }; struct Table { struct Node *first; }; 4 "Gehrig" 3 "Ruth" NULL
t! struct Table *Table_create(void) { struct Table t; t = (struct Table) malloc(sizeof(struct Table)); t->first = NULL; return t; } struct Table *t; … t = Table_create(); …
void Table_add(struct Table *t, const char *key, int value) { struct Node p = (struct Node)malloc(sizeof(struct Node)); p->key = key; p->value = value; p->next = t->first; t->first = p; } struct Table *t; … Table_add(t, "Ruth", 3); Table_add(t, "Gehrig", 4); Table_add(t, "Mantle", 7); … These are pointers to! strings! 4 "Gehrig" 3 "Ruth" NULL t!
void Table_add(struct Table *t, const char *key, int value) { struct Node p = (struct Node)malloc(sizeof(struct Node)); p->key = key; p->value = value; p->next = t->first; t->first = p; } struct Table *t; … Table_add(t, "Ruth", 3); Table_add(t, "Gehrig", 4); Table_add(t, "Mantle", 7); … 4 "Gehrig" 3 "Ruth" NULL t! p!
void Table_add(struct Table *t, const char *key, int value) { struct Node p = (struct Node)malloc(sizeof(struct Node)); p->key = key; p->value = value; p->next = t->first; t->first = p; } struct Table *t; … Table_add(t, "Ruth", 3); Table_add(t, "Gehrig", 4); Table_add(t, "Mantle", 7); … 4 "Gehrig" 3 "Ruth" NULL
"Mantle" t! p!
void Table_add(struct Table *t, const char *key, int value) { struct Node p = (struct Node)malloc(sizeof(struct Node)); p->key = key; p->value = value; p->next = t->first; t->first = p; } struct Table *t; … Table_add(t, "Ruth", 3); Table_add(t, "Gehrig", 4); Table_add(t, "Mantle", 7); … 4 "Gehrig" 3 "Ruth" NULL
"Mantle" t! p!
int Table_search(struct Table *t, const char *key, int *value) { struct Node *p; for (p = t->first; p != NULL; p = p->next) if (strcmp(p->key, key) == 0) { *value = p->value; return 1; } return 0; } struct Table *t; int value; int found; … found = Table_search(t, "Gehrig", &value); … t! 4 "Gehrig" 3 "Ruth" NULL
"Mantle" p!
int Table_search(struct Table *t, const char *key, int *value) { struct Node *p; for (p = t->first; p != NULL; p = p->next) if (strcmp(p->key, key) == 0) { *value = p->value; return 1; } return 0; } struct Table *t; int value; int found; … found = Table_search(t, "Gehrig", &value); … t! 4 "Gehrig" 3 "Ruth" NULL
"Mantle" p!
int Table_search(struct Table *t, const char *key, int *value) { struct Node *p; for (p = t->first; p != NULL; p = p->next) if (strcmp(p->key, key) == 0) { *value = p->value; return 1; } return 0; } struct Table *t; int value; int found; … found = Table_search(t, "Gehrig", &value); … t! 4 "Gehrig" 3 "Ruth" NULL
"Mantle" p!
int Table_search(struct Table *t, const char *key, int *value) { struct Node *p; for (p = t->first; p != NULL; p = p->next) if (strcmp(p->key, key) == 0) { *value = p->value; return 1; } return 0; } struct Table *t; int value; int found; … found = Table_search(t, "Gehrig", &value); … t! 4 "Gehrig" 3 "Ruth" NULL
"Mantle" p! 4