Written Pointers
Written Pointers
struct student_type
{char first_name[21];
char name[21];
char address[60];
struct student_type *pointer_to_next_record;
struct notes_type *pointer_to_note;
};
struct student_type *position_ptr = NULL;
struct student_type *temp_ptr = NULL;
struct student_type *rota_ptr = NULL;
struct notes_type
{
char name[21];
char date[9];
char note[61];
struct notes_type *pointer_to_next_note;
};
When the first student is added the first thing to happen is the creation of a memory space based on the
Student_type template to be created with its address stored in temp_ptr (lets say 1000) This is unless
There is not enough memory to store the data (very unlikely) where the error routine function would
give you a warning(not enough memory).
void add_student(void)/
{ /*Request space*/
if (!(temp_ptr = (struct student_type*)
malloc(sizeof (struct student_type))))
error_routine();
1000
temp_ptr
position_ptr rota_ptr
NULL NULL
the other structure stay as a possible template
Address Address
NAME DATE NOTE of
next note
Temp_ptr 1000
1000 1000
Position_ptr Rota_ptr
You are then asked to input the data to go into main memory
1000
temp_ptr position_ptr rota_ptr
1000 1000
You can then add a new student to this short list (lets assume the
address of the new space allocated by malloc to be 2000)
else
{/*Add record to other already in the list*/
temp_ptr->pointer_to_next_record = position_ptr->pointer_to_next_record;
position_ptr->pointer_to_next_record = temp_ptr;
position_ptr = temp_ptr;
}
/*Get student details*/
printf("\nKey in student's first name\n"); eg JOHN
scanf("%s", temp_ptr->first_name);
printf("\nKey in student's name\n"); eg ALDER
scanf("%s%*c", temp_ptr->name);
printf("\nKey in student address\n"); eg THE ROAD W3
gets(temp_ptr->address);
}
2000
temp_ptr position_ptr rota_ptr
2000 1000
This can be repeated add infinitum (at least till you run out of memory space)
Lets illustrate one more entry (new address assumed to be 3000)
else
{/*Add record to other already in the list*/
temp_ptr->pointer_to_next_record = position_ptr->pointer_to_next_record;
position_ptr->pointer_to_next_record = temp_ptr;
position_ptr = temp_ptr;
}
/*Get student details*/
printf("\nKey in student's first name\n"); eg CHRIS
scanf("%s", temp_ptr->first_name);
printf("\nKey in student's name\n"); eg SMITH
scanf("%s%*c", temp_ptr->name);
1000
printf("\nKey in student address\n"); NULL SE2
eg THE AVENUE 2000
CHRISTIE IAN
gets(temp_ptr->address); THE DRIVE N1
3000 1000
then a routine to call a function to look for the record to be removed /*search list for
name*/
if (!(temp_ptr = seek (position_ptr, student_name, 'P'))) (Passing 3 parameter; P meaning Previous)
which calls the following function (accepting address of a record, name to be found
and a character and returning address of record if found)
struct student_type * seek (struct student_type *start_ptr, char name_required[],char ind)
Receiving 3 parameter and changing their name and returning a address
{struct student_type *item_ptr, *previous_item_ptr;
( creating memory space to hold addresses of struct student_type while doing search )
if (ind == 'C')
This identify which function called this routine as they require sligtly different result
(remove)student needs address of previous record display student needs address of
record required
return (item_ptr); eg 1. 2000 / 2. 3000
else
return (previous_item_ptr);
}
}while (item_ptr != start_ptr); (Stop search once every record have been checked)
/*Item not found*/
return (NULL); (this initiate the warning in calling function)
}
_______________________________________________________________________________
if the record doesn’t exist then warn user(Receiving NULL from seek function)
{printf(" * Student not in list *\n");
return; }
2000
Temp_ptr
2000 1000
position_ptr rota_ptr
1000
Temp_ptr position_ptr rota_ptr
3000 2000
OPTION 5 Saving data to disk
{
Create 2 pointers to deal with putting records into a central memory space prior to be saved to disk
struct student_type *info;
struct student_type *flag;
FILE *student_file_pointer;
if(!student_file_pointer) {
printf("Cannot open file\n");
exit(1);
}
printf("Saving file to disk");
info = temp_ptr->pointer_to_next_record;
flag = info;
fclose(student_file_pointer);
}
________________________________________________________-
void save(void)
{
struct address *info;
FILE *fp;
fp = fopen("mlist", "wb");
if(!fp) {
printf("Cannot open file.\n");
exit(1);
}
printf("\nSaving File\n");
info = start;
while(info) {
fwrite(info, sizeof(struct address), 1, fp);
info = info->next; /* get next address */
}
fclose(fp);
}
/* Load the address file. */
void load()
{
struct address *info;
FILE *fp;
fp = fopen("mlist", "rb");
if(!fp) {
printf("Cannot open file.\n");
exit(1);
}
printf("\nLoading File\n");
while(!feof(fp)) {
info = (struct address *) malloc(sizeof(struct address));
if(!info) {
printf("Out of Memory");
return;
}
if(1!=fread(info, sizeof(struct address), 1, fp)) break;
dls_store(info, &start, &last);
}
fclose(fp);
}