Linked Data Structure - Lecture Slides | CMSC 212, Study notes of Computer Science

Material Type: Notes; Professor: Hollingsworth; Class: INTRO TO LOW-LEVEL PROG; Subject: Computer Science; University: University of Maryland; Term: Fall 2005;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-ft2-1
koofers-user-ft2-1 🇺🇸

4

(1)

10 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
1
CMSC 212 – F05 (lect 10)
Announcements
Exam #1
Thursday, March 8, 6:00 – 7:30pm in ARM 0126
Reading
Chapter 12, 10.2
2
CMSC 212 – F05 (lect 10)
Linked Data Structures
Like classes with refs to same class in Java
Have pointers to a struct of the same type
Example Declaration:
typedef struct NODE {
struct NODE *next;
int value;
} Node;
Node * root;
next and root are pointers to Nodes
root
5 10 15
NULL
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Linked Data Structure - Lecture Slides | CMSC 212 and more Study notes Computer Science in PDF only on Docsity!

CMSC 212 – F05 (lect 10)^1

Announcements

 Exam

  • Thursday, March 8, 6:00 – 7:30pm in ARM 0126

 Reading

  • Chapter 12, 10.

CMSC 212 – F05 (lect 10)^2

Linked Data Structures

 Like classes with refs to same class in Java

 Have pointers to a struct of the same type

 Example Declaration:

typedef struct NODE { struct NODE *next; int value; } Node; Node * root;

 next and root are pointers to Nodes

root

5 10 15

NULL

CMSC 212 – F05 (lect 10)^3

Finding a value in a Linked Structure

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

Tracing Through Insert

head

5 10 15

X

7

Insert 7

X

2

Insert 2

NULL

CMSC 212 – F05 (lect 10)^7

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

CMSC 212 – F05 (lect 10)^8

Doubled Linked Lists

 Each nodes

  • contains a value
  • a pointer the next and previous element

 Typical Declaration:

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

5 10 15

NULL

NULL

CMSC 212 – F05 (lect 10)^9

Insert into a doubly Linked list

 Think about 4 cases:

  • Middle of the list
    • Update previous element's next pointer
      • new node’s previous pointer set to this node
    • Update next element’s previous pointer
      • new node’s next point set to that node
  • End of the list
    • Update previous element's next pointer
      • new node’s previous pointer set to this node
      • new node’s next pointer set to NULL
  • Start of the list
    • Update head of list
    • Update old head of list's previous element
      • new node’s next pointer set to this node
      • new node’s previous pointer set to NULL
  • An initially empty list
    • Update head of the list
      • new node’s next and previous pointer both NULL

CMSC 212 – F05 (lect 10)^10

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 = newItem; } }

CMSC 212 – F05 (lect 10)^13

Lookup In A Binary Search Tree

 If the element is less than the current node

  • Look in the left child tree

 If the element is greater than current node

  • Look in the right child tree

 Example:

*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

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

CMSC 212 – F05 (lect 10)^15

Graphs - Directed

 In a Graph

  • there can be an arbitrary number of nodes
  • each node can have an arbitrary number of out arcs

 Declarations

typedef struct ARC { struct NODE *nodePtr; struct ARC *next; } Arc;

typedef struct NODE { Arc *outArcs; int value; } Node;

CMSC 212 – F05 (lect 10)^16

Graphs

Null Null

Null

Node

Arc

5

7

14

key