Académique Documents
Professionnel Documents
Culture Documents
Node 0
Node 1
Node 2
CIT593
Advantage of array
Easy/fast element access
CIT593
CIT593
1 2 3 4 5 6 7 8 9 10
CIT593
Deallocate all memory used by list //Deallocate item * temp = head; while(head != NULL){ head= head->next; free(temp); temp = head; }
Goal
Make sure before you delete the node you have reference to its next node
Create inventory database for used car lot Support the following operations Search d t b S h database for a particular vehicle f ti l hi l Add new vehicle to database Delete vehicle from database
Implementation
Since we dont know how many cars might be on lot at one time, we choose a linked list representation h li k d li t t ti In order to have faster search, the database must remain sorted by vehicle ID
CIT593
CIT593
Adding a Node
void addCar(CarNode ** head, CarNode * newNode){..} Steps
Create new node with proper info (via malloc) p p ( ) 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
Deleting a Node
Steps
Find the node that points to the desired node (via ScanList) Redirect that nodes pointer to the next node (or NULL) Free the deleted nodes memory
Trees Hierarchical structure with a set of linked nodes A node may contain a value or a condition or y represent a separate data structure A node has zero or more child nodes
Node 0
Node 1
Node 2
Most common type is Binary tree with each node having a left and right child g g
NULL
CIT593
13
CIT593
14
Binary Tree
Node 0
struct tnode{ int l i t val; struct tnode * left; struct tnode * right; }; yp typedef struct tnode node;
Node 1
NULL
Node 2
NULL
Node 3
NULL NULL
Node 4
NULL NULL
Implications
Faster lookup Slower insertion (cant just prepend)
Optimizations
Balancing Redistributing
CIT593
15
CIT593
16