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

Data structure: Nodes; each contains key/value pair and pointer to next node

Typology: Slides

2020/2021

Uploaded on 05/24/2021

eknathia
eknathia 🇺🇸

4.4

(26)

264 documents

1 / 74

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Data Structures and Algorithms!
The material for this lecture is drawn, in part, from!
The Practice of Programming (Kernighan & Pike) Chapter 2!
Jennifer Rexford!
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a

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