Docsity

Linked Lists and Data Structures: Lecture 12 in CMSC 212 - Prof. Jandelyn Dawn Plane, Study notes of Computer Science

Toggle sidebar
bg1
1
CMSC 212 – S05 (lect 12)
Announcements
●Exam #1 and Project #1
– if you have not picked something up, see me after class
●Exam #1 Re-grade Requests
– due by end of class today (both electronic and paper)
●Program #2
– Due on March 15 at 8:00 pm (Tuesday)
– Don’t free NULL pointers – will be flagged as an error and
cause you to fail tests
– createTable – confusion about interpretation
• passing in a “size” just needs to be in range
• does not need to be exactly one of the primes in the list
●Reading
– Chapter 12, 10.2 (Today)
– Notes (Tuesday)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Linked Lists and Data Structures: Lecture 12 in CMSC 212 - Prof. Jandelyn Dawn Plane and more Study notes Computer Science in PDF only on Docsity!

Announcements

l

Exam #1 and Project #1– if you have not picked something up, see me after class

l

Exam #1 Re-grade Requests– due by end of class today (both electronic and paper)

l

Program #2– Due on March 15 at 8:00 pm (Tuesday)– Don’t free NULL pointers – will be flagged as an error and

cause you to fail tests

  • createTable – confusion about interpretation
    • passing in a “size” just needs to be in range• does not need to be exactly one of the primes in the list

l

Reading– Chapter 12, 10.2 (Today)– Notes (Tuesday)

Linked Data Structures

l

Like classes with refs to same class in Java

l

Have pointers to a struct of the same time

l

Example Declaration:typedef struct NODE {

struct NODE *next;int value;

} Node;

l

Next is a pointer to another Node root

5

10

15 NULL

Tracing Through Insert

head

5

10

15

X^7

Insert 7

X 2

Insert 2

NULL

Inserting Into a Linked List

**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;

}

Deleting From A Single Linked List

**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;

}

Doubled Linked Lists

l

Each nodes– contains a value– a pointer the next

and

previous element

l

Typical Declaration:typedef struct NODE {

struct NODE *next;struct NODE *prev;int value; } Node; root

5

10

15

NULL

NULL

Inserting Into A Doubly Linked List

**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; }

}

Deleting from a Doubly Linked List

**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;

}

Lookup In A Binary Search Tree

l

If the element is less than the current node– Look in the left child tree

l

If the element is greater than current node– Look in the right child tree

l

Example (assumes values >= 0):^ 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); } }

Insert Into a Binary SearchTree

**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);*

}

}

Graphs

Null

Null

Null

Node^ Arc

5

7

14

key

Adding Node To a Graph

*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.