1100.  Suppose we wish to link records together. EXPLAIN how we would go about doing it.

 

1.       We would get the first address:

 

newdata = (struct mydata *) malloc(sizeof(struct mydata));

 

2.       We would check to see that we actually got the memory we needed:

 

            If (newdata == NULL)

            {

                        cout << “Memory Allocation Failed – Program Aborted\n”;

                        return(0);

            }

 

3.       We would store the address of the first record in our first record pointer (so that we will be able to retrieve it later:

 

firstdata = newdata;

 

4.       We would enter the data and save the address in our previous pointer (so that we will be able to link this record to the next record:

 

lastdata = newdata;

 

5.       We would get the next record’s address (Checking to make sure we got it)

 

lastdata = newdata;

If (newdata == NULL)

          {

              cout << “Memory Allocation Failed – Program Aborted\n”;

              return(0);

}

 

6.       We would enter the data and link the previous record to this one:

 

lastdata -> next = newdata;

 

7.       We would again save the address in our previous pointer (so that we will be able to link this record to the next record:

 

lastdata = newdata;

 

8.       We would repeat steps 5, 6, and 7 until we entered the data into our last record.

 

9.       For the last record on our list, we will have it point to nothing:

 

newdata -> next = NULL;