Note 6: Structure in Data Manipulation for Application

 

The Concept of a Structure:

A structure is a composite type that can represent several different types of data in a single unit.  For example, we can represent a single record for each student in the class bye the following individual variable:

 

            char   Last [30];

            char   First [30];

            short   Exam1;

            short   Exam2;

            short   Exam3;

            float    Average;

 

A single structure can group these variables using the following definition:

 

            struct   student

            {

                        char   Last [30];

                        char   First [30];

                        short   Exam1;

                        short   Exam2;

                        short   Exam3;

                        float    Average;

            };

 

The Keyword struct specifies a structure definition.  The structure tag, or name of the structure, is student.  After the structure tag, braces surround a description of the members or elements of the structure.  A member of a structure is a single unit, so the structure here has six members.  Some languages call such a member a field.  Every member of a structure must have its own name and type and also could be the struct type.  As with a class specification, the C++ syntax demands that we end the structure definition with a semicolon ( ; ) after the closing brace.

 

 

Declare Structure Variable:

Because student is a new data type, we can declare variable to be that type.

 

Declare single structure variable:  student   record;      

Declare structure array:  student   record [n];

Declare structure pointer:

  1. student *  record;
  2. change data type from student to student * :

      typedef    struct  student  *  student_ptr;

student_ptr   record;

 

Access Member Data in Structure:

Access Member Data Last Name in Structure Table

Structure Type Variable

Access Method

Single Structure Variable

record.Last

Structure Array

record [i].Last

Structure Pointer

record  每 > Last

Structure Pinter to Array

(record + i) 每 > Last

The period and the point to the right arrow are the period operator and structure member operator to access member data in structure.

 

 

Structure Copy and Compare:

In order to copy and compare two structures, both structures must have exactly number of members and type of members.

 

Structure Copy:

Two ways to do structure copy:

  1. Using equal sign ( = ) to copy structure

record [1] = record [2];

  1. Copy each field of member in the structure one by one

record [1].Last = record [2].Last;

record [1].First = record [2].First;

record [1].Exam1 = record [2].Exam1;

record [1].Exam2 = record [2].Exam2;

record [1].Exam3 = record [2].Exam3;

record [1].Average = record [2].Average;

 

Compare Structure:

To compare two structure, you can not use double equal sign ( = = ) for structure name; only way to do comparison is comparing each field of member in structure one by one.

if ( record [1].Last = = record [2].Last              &&

      record [1].First = record [2].First                &&

      record [1].Exam1 = record [2].Exam1        &&

      record [1].Exam2 = record [2].Exam2        &&

      record [1].Exam3 = record [2].Exam3        &&

      record [1].Average = record [2].Average          ) ##..

 

 

Data Search Algorithms:

Sequential Search:

            short   i = 0, find = 0;

            while (i < End)

            {

                        if (value = = Data[i])

                        {

                                    find = 1;

                                    i = End;

                        }

                        else

                                    i ++;

            }

 

 

Binary Search:

            while (Begin <= End)

            {

                        mid = (Begin + End) / 2;

                        if (value > Data[mid])

                                    Begin = mid + 1;

                        else if (value < Data[mid])

                                    End = mid 每 1;

                        else

                                    break;

            }

 

 

Data Sorting Algorithms:

Selection Sort:

            i = 0;

            while (i < N-1)

            {

                        j = i + 1;

                        small = x[i];

                        loc = i;

                        while (j < N)

                        {

                                    if (x[j] < small)

                                    {

                                                small = x[j];

                                                loc = j;

                                    }

                                    j ++;

                        }

                        if (loc != i)

                        {

                                    x[loc] = x[i];

                                    x[i] = small;

                        }

                        i++;

            }

 

 

 

Insertion Sort:

//Element 0 stored smallest value of the data for ascending order, or stored biggest value

//of the data for descending order.

 

            i = 1;

            N_element = 1;

            while ( more value)

            {

                        N_value = x[i];

                        j = N_element 每 1;

                        while (N_value < x[j])

                        {

                                    x[j+1] = x[j];

                                    j --;

                        }

                        x[j + 1] = N_value;

                        N_element ++;

                        i ++;

            }

 

Insertion Sort without moving data by using tag:

//Element 0 stored smallest value of the data for ascending order, or stored biggest value

//of the data for descending order.

 

            i = 1;

            N_element = 1;

            while  (more value)

            {

                        N_value = tag[i];

                        J = N_element 每 1;

                        while (x[N_value] < x[tag[j]])

                        {

                                    tag[j + 1] = tag[j];

                                    j --;

                        }

                        tag[j + 1] = N_value;

                        N_element ++;

                        i ++;

            }