





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
Material Type: Notes; Professor: Hollingsworth; Class: INTRO TO LOW-LEVEL PROG; Subject: Computer Science; University: University of Maryland; Term: Fall 2005;
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






CMSC 212 – F05 (lect 10)^1
CMSC 212 – F05 (lect 10)^2
typedef struct NODE { struct NODE *next; int value; } Node; Node * root;
root
5 10 15
NULL
CMSC 212 – F05 (lect 10)^3
**Node find(Node current, int val) { while (current && current->value != val) { current = current->next; } return current; }
*int IsIn(Node current, int val) { while ((current != NULL) && ((current->value) != val)) { current = current->next; } if (current) { return 1; } else { return 0; } }
CMSC 212 – F05 (lect 10)^4
head
5 10 15
X
7
X
2
NULL
CMSC 212 – F05 (lect 10)^7
**int delete(Node **head, int new_value) { Node *pred = NULL; Node *current = head; while (current && (current->value != new_value)) { pred = current; current = current->next; } if (!current) { return -1; / not found / } if (pred) { pred->next = current->next; } else { head = current->next; / deleted first item / } free (current); return 0; }
CMSC 212 – F05 (lect 10)^8
typedef struct NODE { struct NODE *next; struct NODE *prev; int value; } Node; root
5 10 15
NULL
NULL
CMSC 212 – F05 (lect 10)^9
Think about 4 cases:
CMSC 212 – F05 (lect 10)^10
**int insert(Node **head, int new_value) { Node *pred = NULL, *newItem; Node *current = *head; while (current && current->value < new_value) { pred = current; current = current->next; } newItem = (Node ) malloc(sizeof(Node)); if (!newItem) return -1; newItem->value = new_value; newItem->next = current; newItem->prev = pred; if (!pred) { head = newItem; } else { pred->next = newItem; } if (current) { current->prev = newItem; } }
CMSC 212 – F05 (lect 10)^13
*int Lookup(Node root, int value) { if (!root) return -1; if (root->value == value) { return 1; } else if (root->value > value) { return Lookup(root->left, value); } else { return Lookup(root->right, value); } }
CMSC 212 – F05 (lect 10)^14
*int insert(Node root, int value) { Node new; if (!(root)) { new = (Node ) malloc(sizefof(Node)); if (!new) return -1; root = new; new->left = new->right = NULL; new->value = value; return 0; } if ((root)->value > value) { return insert(&((root)->left), value); } else { return insert(&((root)->right), value); } }
CMSC 212 – F05 (lect 10)^15
typedef struct ARC { struct NODE *nodePtr; struct ARC *next; } Arc;
typedef struct NODE { Arc *outArcs; int value; } Node;
CMSC 212 – F05 (lect 10)^16
Null Null
Null
Node
Arc
5
7
14