120 likes | 259 Views
CSI 400/500 Operating Systems Spring 2009. LAB - Dynamic Allocation in C Using Linked Lists Week of February 24-26. Creating structures in C. Use struct keyword Create new variable class using typedef Can nest structures Simple way to gather data that defines a particular data item.
E N D
CSI 400/500 Operating SystemsSpring 2009 LAB - Dynamic Allocation in C Using Linked Lists Week of February 24-26
Creating structures in C • Use struct keyword • Create new variable class using typedef • Can nest structures • Simple way to gather data that defines a particular data item
Dynamic structures • Can combine malloc and sizeof to allocate dynamic storage of structures • Conducive to creating lists of structure elements
Dynamic Linked List • Linear chain of structures created during execution • Definitions: • NODE : individual item on chain • FIELD : individual data item in node • HEAD : start of chain • TAIL : end of chain • NEXT : pointer to next node • NULL : no node at end of pointer
Linked List implementation in C • Most linked lists require two structures: • One for data contained in node • One for node • Nodes created by obtaining storage, setting data value, and initializing *next and *prev to 0
Linked List functions • So now we have one -- what do we do with it? • Insert new node • Remove existing node • Find a given node • List all nodes
Inserting a node • Receive key value of new node • Obtain storage • Check if list exists • Find location in list • Do not add if already exists • Add to list
Deleting a node • Find it • Reset pointers to remove it from list • Free storage
List values in list • Appears in sorted order automatically
Why discuss lists in an Operating Systems course? • Many operating systems use lists as their queues • Others use more complex structures like trees and hash tables, which will be discussed later in this course