Data Structures Linked List & Binary Tree

Linked List Data Structure • Sequence of nodes, each containing arbitrary data fields • One or two references ("links") pointing to the next and/or d/...
Author: Donna Wells
1 downloads 1 Views 56KB Size
Linked List Data Structure • Sequence of nodes, each containing arbitrary data fields • One or two references ("links") pointing to the next and/or d/ previous i nodes d

Data Structures Linked List & Binary Tree

Node 0

Node 1

Node 2

Single link linked list

Based on slides © McGraw-Hill Additional material © 2004/2005 Lewis/Martin

CIT593

Link List Implementation in C

Linked List vs. Array Advantage of linked list

struct listNode { int val; struct listNode * next; };

• Dynamic size (easy to add or remove nodes)

Advantage of array • Easy/fast element access

Element access for a list

typedef struct listNode item;

• Linked list elements can only be accessed sequentially e.g., to find the 5th element, you must start from head and follow the links through four other nodes

CIT593

2

3

CIT593

4

1

Create a list of 10 elements item * curr, * head, *tail; int i; head = tail = NULL; for(i=1;ival = i; curr->next = NULL; if(head == NULL){ Note: head is pointing head = curr; to first ever node. Tail tail = head; points to the last item } added else{ tail->next = curr; tail = tail->next; } CIT593 }

Print the linked list element //reassign curr to head curr = head; while(curr != NULL) { printf("%d\n", curr->val); curr = curr->next ; }

5

CIT593

CIT593

6

Sorted List Example: Car Lot

Deallocate all memory used by list //Deallocate item * temp = head; while(head != NULL){ head= head->next; free(temp); temp = head; }

1 2 3 4 5 6 7 8 9 10

Goal • Create inventory database for used car lot • Support the following operations ¾S ¾Search h database d t b for f a particular ti l vehicle hi l ¾Add new vehicle to database ¾Delete vehicle from database

Make sure before you delete the node you have reference to its next node

Implementation • Since we don’t know how many cars might be on lot at one time, we choose h a linked li k d list li t representation t ti • In order to have “faster” search, the database must remain sorted by vehicle ID

7

CIT593

8

2

CarNode

Scanning the List Scanning

Each vehicle has the following characteristics

• Searching, adding, and deleting require us to find a particular node in the list we’re re looking for • Scan list until we find node whose ID is >= one we

• Vehicle ID, make, model, year, mileage, cost • (And… pointer to next node) typedef struct car_node CarNode;

CarNode *ScanList(CarNode *head, int searchID) { CarNode *previous, *current; previous = head; current = head->next; /* Traverse until ID >= searchID */ while (( ((current!=NULL) ) && (current->vehicleID < searchID)) { previous = current; current = current->next; } return previous; }

struct car_node { int vehicleID; char make[20]; char model[20]; int year; int mileage; double cost; CarNode *next; /* ptr to next car in list */ } CIT593

9

CIT593

Adding a Node

10

Excerpts from Code to Add a Node

void addCar(CarNode ** head, CarNode * newNode){..} newNode = (CarNode*) malloc(sizeof(CarNode)); /* initialize node with new car info */ ... prevNode = ScanList(*head ScanList( head, newNode->vehicleID); newNode >vehicleID); nextNode = prevNode->next; ... if ((nextNode == NULL) || (nextNode->vehicleID != newNode->vehicleID)) prevNode->next = newNode; newNode->next = nextNode; } else { printf(“Car already exists in database.”); free(newNode); }

Steps • Create new node with p proper p info (via ( malloc)) • Find node (if any) with a greater vehicleID (via ScanList) • “Splice” the new node into the list (update next fields) new node

Node 0

Node 1

Node 2

NULL CIT593

11

CIT593

12

3

Trees

Deleting a Node

• Hierarchical structure with a set of linked nodes

Steps • Find the node that points to the desired node (via ScanList)

• A node may y contain a value or a condition or represent a separate data structure

• Redirect that node’s pointer to the next node (or NULL) • Free the deleted node’s memory

• A node has zero or more child nodes Node 0

Node 1

• Most common type is Binary tree with each node having g a left and right g child

Node 2

NULL

CIT593

13

CIT593

E.g. Sorted Binary Tree

Binary Tree Node 0

struct tnode{ i t val; int l struct tnode * left; struct tnode * right; }; yp struct tnode node; typedef

Node 1

NULL

Structure • At each node, value of the left child is less than value at node • Also, value of the right child is greater than the value at node

Node 2

NULL

Node 3 NULL

14

NULL

Node 4 NULL

Implications • Faster lookup • Slower insertion (can’t just prepend)

NULL

Optimizations • Balancing • Redistributing

CIT593

15

CIT593

16

4

Inserting an element to keep tree sorted

Inserting an element to keep tree sorted (contd..)

node * root = NULL; //global ptr void insert(node * item){ if(root == NULL){ roott = it item; } else{ node * curr = root; //curr is your reference that keeps moving while(1){ if(item->val < curr->val){ if(curr->left == NULL){ curr->left l ft = item; it break; } else{ curr = curr->left; } CIT593 }

else if(item ->val > curr->val){ if(curr->right == NULL){ curr->right = item; break; } else{ curr = curr->right; } }//end else if else{ //duplicate break; } }//end while }//end else }//end insert 17

CIT593

18

5

Suggest Documents