Lecture 12:  Tools for Creating data structures

 

Objectives of this lecture

 

q       Introduce Data structures

q       Study the tools for creating data structure – enum, typedef, struct & union (struct & union will be discussed in next lecture)

 

What is data structure?

q       Data structure is any programming construct that is used to represent a collection of data

q       It is another very important tool (the other being algorithm) used in problem solving

q       The study of data structure involves constructing models to represent data and its associated operations for various (abstract) programming situation.  Such models are called Abstract Data Types – ADTs.

q       We shall study the logical design and physical implementation of two ATDs  -- Stack and Queue

q       We start by learning the tools available in C that are used to construct ADTs.  enum, typedef, struct and union.

 

The enum specifier:

q       C provides only a few basic data type – int, float, char, poiter and void.

q       These data types and their associated operators are used to solve problems including those problems that are not directly related to these data type.

q       Example: A problem involving days of week may be solved by using int values 1, 2, . . . 7 to represent Monday, Tuesday, . . ., Sunday.

q       To assign Monday to an int variable day for example, we may write day=1;

q       Clearly a program involving the above problem will be more transparent if the data can be represented in its natural form as: Monday, Tuesday, . . ., Sunday and the assignment be made as day=Monday.

 

 

q       C recognizes this fact, and has provided a facility for constructing user-defined types to represent the data for any particular problem.

q       The specification is done using the enum specifier. This allow a finite set of identifies to be declared by enumerating them in braces --  such set is called enumerated type.    

 

Examples:

eum boolean {FALSE, TRUE};

enum user_status {STUDENT, STAFF, FACULTY};

enum book_status {AVAILABLE, BORROWED, RESERVED};

enum direction {NORTH, WEST, SOUTH, EAST}

enum weekday {MONDAY, TUESDAY, WEDNESDAY,

                        THURSDAY, FRIDAY, SATURDAY, SUNDAY}

 

q       Internally, the identifies are stored as integer constants with the first having a default value of zero and each succeeding one has the next value.

q       Notice that it is common to list the identifies in upper case since they are constants.

q       The identifier given after the word enum is called the tag and must be unique for different types.  Tags are not given any memory storage until variables of such type are declared.

q       After an enumerated type is specified, then variable of such type may be declared in a similar manner we declare normal variable except that enum must precede each declaration.

 

e.g. enum weekday day;

       enum boolean flag;

 

q       The first example declares a variable day of type enum weekday and can be assigned any of the listed values of this type.

 

e.g.  day=MONDAY;

          flag=FALSE;

 

q       In addition to the clarity and documentation the above example provides, It also forces the compiler to do type checking.  For example, the following is illegal.

Day=flag;             

q       Since the identifies of enum type are ordered, It is possible to compare them.  For example, the following is a valid C code.

 

Switch (day)

{  case MONDAY: printf(“Today is Monday\n”); break;

    case TUESDAY: printf(“Today is Tuesday\n”); break;

    case WEDNESDAY: printf(“Today is Wednesday\n”) ;break;

    case THURSDAY: printf(“Today is Thursday\n”); break;

    case FRIDAY: printf(“Today is Friday\n”); break;

    case SATURDAY: printf(“Today is Saturday\n”); break;

    case SUNDAY: printf(“Today is Sunday\n”); break;

        }

q       However, we cannot do direct I/O with enumerated type.  We have to use the %d specifier which means they will be read and printed as integers.

e.g.   printf(“%d”,day);          prints 0.

 

The typedef specifier

q       This is used to give identifies to data types.  It can be used both for basic and enumerated types.

E.g.   typedef int INTEGER;

          typedef float REAL;

 

q       Once this is specified, these names can be used to declare variables.

E.g.   INTEGER count;

          REAL rate;

 

q       Again note that it is common to use upper case with typedef identifies.

 

q       typedef can also be used to avoid the use of enum in declaring variables of enumerated type.

 

e.g.   typedef enum {FALSE, TRUE} BOOLEAN;/* directly*/

typedef enum weekday WEEKDAY; /*through enum weekday*/

 

with this, we can declare a Boolean variable as follows:

 

BOOLEAN flag;

WEEKDAY day;

q       typedef can also be used with arrays to simplify there declarations.

 

e.g.  typedef int GRADE_LIST[10];

 

q       Notice that GRADE_LIST is only a tag.  Memory is assigned only to its variables.  We can declare a variable of type GRADE_LIST as:

 

GRADE_LIST grade;

 

q       Declaration of strings can be done more intuitively using type def as shown below:

typedef char STRING[81];

With this,  the function header: 

void(char name[], int grade[], char letter_grade[])

Can be done as:  

void(STRING name, GRADE_LIST grade,

STRING letter_grade)

 

q       The following function returns the next day given a day.

WEEKDAY next_day(WEEKDAY day)

{   WEEKDAY next_day;

    switch(day)

    {   case MONDAY:

                next_day=TUESDAY; break;

        case TUESDAY:

                next_day=WEDNESDAY; break;

       case WEDNESDAY:

                next_day=THURSDAY; break;

       case THURSDAY:

                next_day=FRIDAY; break;

       case FRIDAY:

                next_day=SATURDAY; break;

       case SATURDAY:

                next_day=SUNDAY; break;

       case SUNDAY:

                next_day=MONDAY; break;

   }

    return next_day;

}