










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
The twelfth lecture notes for the computer science course cmsc 212 at the university of x. The lecture covers various topics related to linked lists, including finding values, inserting and deleting nodes, and doubly linked lists. Additionally, the lecture introduces binary trees and their lookup and insertion. The document also includes examples and code snippets.
Typology: Study notes
1 / 18
This page cannot be seen from the preview
Don't miss anything!











l
l
l
cause you to fail tests
l
l
l
l
struct NODE *next;int value;
} Node;
l
5
10
15 NULL
head
5
10
15
X^7
X 2
NULL
**int insert(Node head, int new_value) {
**Node *current = *head;Node pred = NULL;Node newItem;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;if (!pred) {
*head = newItem;
} else {
pred->next = newItem;
} return 0;
}
**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;
}
l
and
previous element
l
struct NODE *next;struct NODE *prev;int value; } Node; root
5
10
15
NULL
NULL
**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 = next; }
}
**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 / } if (current->next) {
current->next->prev = pred; } free (current);return 0;
}
l
l
l
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); } }
**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);*
}
}
Null
Null
Null
Node^ Arc
5
7
14
*Node AddNode(int value) {
**Node new;new = (Node ) malloc(sizeof(Node));new->value = value;new->outArcs = NULL;return new; } You need to store this node into some type of structure so you don’t lose it
linked list of nodesarray of nodestree of nodesetc.