27
University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell C Dynamic Data Structures

C Dynamic Data Structures

  • Upload
    hakhanh

  • View
    226

  • Download
    1

Embed Size (px)

Citation preview

Page 1: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

C Dynamic Data Structures

Page 2: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 2

Data Structures

A data structure is a particular organizationof data in memory.

We want to group related items together.We want to organize these data bundles in a way that isconvenient to program and efficient to execute.

An array is one kind of data structure.In this chapter, we look at two more:struct – directly supported by Clinked list – built from struct and dynamic allocation

Page 3: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 3

Structures in CA struct is a mechanism for grouping togetherrelated data items of different types.

Recall that an array groups items of a single type.

Example: We want to represent an airborne aircraft:char flightNum[7];int altitude;int longitude;int latitude;int heading;double airSpeed;

We can use a struct to group these data together for each plane.

Page 4: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 4

Defining a StructWe first need to define a new type for the compilerand tell it what our struct looks like.struct flightType {

char flightNum[7]; /* max 6 characters */int altitude; /* in meters */

int longitude; /* in tenths of degrees */int latitude; /* in tenths of degrees */int heading; /* in tenths of degrees */

double airSpeed; /* in km/hr */

};

This tells the compiler how big our struct is andhow the different data items (“members”) are laid out in memory.But it does not allocate any memory.

Page 5: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 5

Declaring and Using a StructTo allocate memory for a struct,we declare a variable using our new data type.struct flightType plane;

Memory is allocated,and we can accessindividual members of thisvariable:plane.airSpeed = 800.0;plane.altitude = 10000;

A struct’s members are laid outin the order specified by the definition.

plane.flightNum[0]

plane.flightNum[6]plane.altitudeplane.longitudeplane.latitudeplane.headingplane.airspeed

Page 6: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 6

Defining and Declaring at OnceYou can both define and declare a struct at the same time.struct flightType { char flightNum[7]; /* max 6 characters */ int altitude; /* in meters */ int longitude; /* in tenths of degrees */ int latitude; /* in tenths of degrees */ int heading; /* in tenths of degrees */ double airSpeed; /* in km/hr */

} maverick;

And you can use the flightType nameto declare other structs.struct flightType iceMan;

Page 7: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 7

typedef

C provides a way to define a data typeby giving a new name to a predefined type.Syntax: typedef <type> <name>;

Examples: typedef int Color; typedef struct flightType WeatherData; typedef struct ab_type { int a; double b; } ABGroup;

Page 8: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 8

Using typedef

This gives us a way to make code more readableby giving application-specific names to types.

Color pixels[500]; Flight plane1, plane2;

Typical practice:Put typedef’s into a header file, and use type names inmain program. If the definition of Color/Flightchanges, you might not need to change the code in yourmain program file.

Page 9: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 9

Generating Code for Structs

Suppose our program starts out like this:int x;Flight plane;int y;

plane.altitude = 0;

...

LC-3 code for this assignment:

AND R1, R1, #0ADD R0, R5, #-13 ; R0=planeSTR R1, R0, #7 ; 8th word

yplane.flightNum[0]

plane.flightNum[6]plane.altitudeplane.longitudeplane.latitudeplane.headingplane.airspeed

xR5

Page 10: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 10

Array of Structs

Can declare an array of structs: Flight planes[100];

Each array element is a struct (7 words, in this case).To access member of a particular element:

planes[34].altitude = 10000;

Because the [] and . operators are at the same precedence,and both associate left-to-right, this is the same as:

(planes[34]).altitude = 10000;

Page 11: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 11

Pointer to Struct

We can declare and create a pointer to a struct: Flight *planePtr; planePtr = &planes[34];

To access a member of the struct addressed by dayPtr: (*planePtr).altitude = 10000;

Because the . operator has higher precedence than *,this is NOT the same as:

*planePtr.altitude = 10000;

C provides special syntax for accessing a struct memberthrough a pointer:

planePtr->altitude = 10000;

Page 12: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 12

Passing Structs as ArgumentsUnlike an array, a struct is always passed by valueinto a function.

This means the struct members are copied tothe function’s activation record, and changes inside the functionare not reflected in the calling routine’s copy.

Most of the time, you’ll want to pass a pointer to a struct.

int Collide(Flight *planeA, Flight *planeB){ if (planeA->altitude == planeB->altitude) { ... } else return 0;}

Page 13: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 13

Dynamic Allocation

Suppose we want our weather program to handlea variable number of planes – as many as the user wantsto enter.

We can’t allocate an array, because we don’t know themaximum number of planes that might be required.Even if we do know the maximum number,it might be wasteful to allocate that much memorybecause most of the time only a few planes’ worth of data isneeded.

Solution:Allocate storage for data dynamically, as needed.

Page 14: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 14

malloc

The Standard C Library provides a function forallocating memory at run-time: malloc.

void *malloc(int numBytes);

It returns a generic pointer (void*) to a contiguousregion of memory of the requested size (in bytes).The bytes are allocated from a region in memorycalled the heap.

The run-time system keeps track of chunks of memory from theheap that have been allocated.

Page 15: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 15

Using malloc

To use malloc, we need to know how many bytesto allocate. The sizeof operator asks the compiler tocalculate the size of a particular type.

planes = malloc(n * sizeof(Flight));

We also need to change the type of the return valueto the proper kind of pointer – this is called “casting.”

planes = (Flight*)malloc(n*sizeof(Flight));

Page 16: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 16

Example

int airbornePlanes;Flight *planes;

printf(“How many planes are in the air?”);scanf(“%d”, &airbornePlanes);

planes = (Flight*)malloc(sizeof(Flight) * airbornePlanes);if (planes == NULL) { printf(“Error in allocating the data array.\n”); ...}planes[0].altitude = ...

If allocation fails,malloc returns NULL.

Note: Can use array notationor pointer notation.

Page 17: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 17

free

Once the data is no longer needed,it should be released back into the heap for later use.

This is done using the free function,passing it the same address that was returned by malloc.

void free(void*);

If allocated data is not freed, the program might run out ofheap memory and be unable to continue.

Page 18: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 18

The Linked List Data Structure

A linked list is an ordered collection of nodes,each of which contains some data,connected using pointers.

Each node points to the next node in the list.The first node in the list is called the head.The last node in the list is called the tail.

Node 0 Node 1 Node 2

NULL

Page 19: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 19

Linked List vs. Array

A linked list can only be accessed sequentially.To find the 5th element, for instance,you must start from the head and follow the linksthrough four other nodes.Advantages of linked list:

Dynamic sizeEasy to add additional nodes as neededEasy to add or remove nodes from the middle of the list(just add or redirect links)

Advantage of array:Can easily and quickly access arbitrary elements

Page 20: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 20

Example: Car Lot

Create an inventory database for a used car lot.Support the following actions:

Search the database for a particular vehicle.Add a new car to the database.Delete a car from the database.

The database must remain sorted by vehicle ID.Since we don’t know how many cars might be on the lotat one time, we choose a linked list representation.

Page 21: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 21

Car data structureEach car has the following characterics:vehicle ID, make, model, year, mileage, cost.

Because it’s a linked list, we also need a pointer tothe next node in the list:typedef struct carType Car;

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

Page 22: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 22

Scanning the ListSearching, adding, and deleting all require us tofind a particular node in the list. We scan the list untilwe find a node whose ID is >= the one we’re looking for.

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

Page 23: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 23

Adding a Node

Create a new node with the proper info.Find the node (if any) with a greater vehicleID.“Splice” the new node into the list:

Node 0 Node 1 Node 2

NULL

new node

Page 24: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 24

Excerpts from Code to Add a NodenewNode = (Car*) malloc(sizeof(Car));/* initialize node with new car info */...prevNode = ScanList(head, 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);}

Page 25: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 25

Deleting a Node

Find the node that points to the desired node.Redirect that node’s pointer to the next node (orNULL).Free the deleted node’s memory.

Node 0 Node 1 Node 2

NULL

Page 26: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 26

Excerpts from Code to Delete a Node

printf(“Enter vehicle ID of car to delete:\n”);scanf(“%d”, vehicleID);

prevNode = ScanList(head, vehicleID);delNode = prevNode->next;

if ((delNode != NULL) && (delNode->vehicleID == vehicleID)) prevNode->next = delNode->next; free(delNode);}else { printf(“Vehicle not found in database.\n”);}

Page 27: C Dynamic Data Structures

University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell 27

Building on Linked Lists

The linked list is a fundamental data structure.DynamicEasy to add and delete nodes

The concepts described here will be helpfulwhen learning about more elaborate datastructures:

TreesHash TablesDirected Acyclic Graphs...