Docsity

Data Structures and Algorithms: Linked Lists in C, Slides of Data Structures and Algorithms

Partial preview of the text

Download Data Structures and Algorithms: Linked Lists in C and more Slides Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms!

The material for this lecture is drawn, in part, from!

The Practice of Programming (Kernighan & Pike) Chapter 2!

Jennifer Rexford!

Motivating Quotations!

“Every program depends on algorithms and data

structures, but few programs depend on the

invention of brand new ones.”!

-- Kernighan & Pike!

“I will, in fact, claim that the difference between a

bad programmer and a good one is whether he

considers his code or his data structures more

important. Bad programmers worry about the

code. Good programmers worry about data

structures and their relationships.”!

-- Linus Torvalds!

A Common Task!

• Maintain a table of key/value pairs!

• Each key is a string; each value is an int

• Unknown number of key-value pairs!

• Examples!

• (student name, grade)!

• (“john smith”, 84), (“jane doe”, 93), (“bill clinton”, 81)!

• (baseball player, number)!

• (“Ruth”, 3), (“Gehrig”, 4), (“Mantle”, 7)!

• (variable name, value)!

• (“maxLength”, 2000), (“i”, 7), (“j”, -10)!

• For simplicity, allow duplicate keys (client responsibility)!

• In Assignment #3, must check for duplicate keys!!

Data Structures and Algorithms!

• Data structures!

• Linked list of key/value pairs!

• Hash table of key/value pairs!

• Algorithms!

• Create: Create the data structure!

• Add: Add a key/value pair!

• Search: Search for a key/value pair, by key!

• Free: Free the data structure!

Linked List: Data Structure!

struct Node { const char *key; int value; struct Node *next; }; struct Table { struct Node *first; }; 4 "Gehrig" 3 "Ruth" NULL

struct!

Table!

struct!

Node!

struct!

Node!

Linked List: Create (1)!

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(); …

Linked List: Add (1)!

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!

Linked List: Add (2)!

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!

Linked List: Add (4)!

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!

Linked List: Add (5)!

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!

Linked List: Search (2)!

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!

Linked List: Search (3)!

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!

Linked List: Search (5)!

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!

Linked List: Search (6)!

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