csi 400/500 operating systems spring 2009
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
CSI 400/500 Operating SystemsSpring 2009
LAB - Dynamic Allocation in C Using Linked Lists
Week of February 24-26
2
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
3
Dynamic structures
• Can combine malloc and sizeof to allocate dynamic storage of structures
• Conducive to creating lists of structure elements
4
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
5
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
6
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
7
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