csi 400/500 operating systems spring 2009

12
CSI 400/500 Operating Systems Spring 2009 LAB - Dynamic Allocation in C Using Linked Lists Week of February 24-26

Upload: dane-dodson

Post on 02-Jan-2016

29 views

Category:

Documents


2 download

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 Presentation

TRANSCRIPT

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

8

Record look-ahead

9

Deleting a node

• Find it

• Reset pointers to remove it from list

• Free storage

10

Finding a node

11

List values in list

• Appears in sorted order automatically

12

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