0% found this document useful (0 votes)
6 views8 pages

Written Pointers

Uploaded by

totot5242
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Written Pointers

Uploaded by

totot5242
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

After compilation ?

or after start of program


The only thing happening is the creation of 2 templates defined by

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;
};

struct notes_type *temp_note_ptr = NULL;


struct notes_type *first_note_ptr = NULL;

and the creation of several pointers to these template

struct student_type *position_ptr = NULL;


struct student_type *temp_ptr = NULL;
struct student_type *rota_ptr = NULL;

struct notes_type *temp_note_ptr = NULL;


struct notes_type *first_note_ptr = NULL;

which are all set to nothing.(NULL).

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 Address Address of


NAME 1st name ADDRESS of note next record

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

The pointers to that template are set to NULL.

struct notes_type *temp_note_ptr = NULL;


struct notes_type *first_note_ptr = NULL;

Option 1 Enter at least one record to carry on

/*Link new record space into list*/


if (position_ptr == NULL)
{/*Add first record to list*/
position_ptr = temp_ptr;
position_ptr->pointer_to_next_record = position_ptr;
/*Set a pointer to keep track of rota order*/
rota_ptr = temp_ptr;
temp_ptr->pointer_to_note = NULL;
}

resulting into this configuration

1000 NULL 1000


NAME 1st name ADDRESS

Temp_ptr 1000

1000 1000
Position_ptr Rota_ptr

You are then asked to input the data to go into main memory

/*Get student details*/


printf("\nKey in student's first name\n"); eg IAN
scanf("%s", temp_ptr->first_name);
printf("\nKey in student's name\n"); eg CHRISTIE
scanf("%s%*c", temp_ptr->name);
printf("\nKey in student address\n"); eg THE DRIVE N1
gets(temp_ptr->address);
}
1000 NULL 1000
CHRISTIE IAN THE DRIVE N1

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);
}

which would give the following set up

1000 NULL 2000


CHRISTIE IAN THE DRIVE N1

2000 NULL 1000


ALDER JOHN THE ROAD W3

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

2000 NULL 3000


ALDER JOHN THE ROAD W3

3000 NULL 1000


SMITH CHRIS THE AVENUE SE2

temp_ptr 3000 position_ptr rota_ptr

3000 1000

Option 2 is to remove a record. The first thing is to specify


which record will be removed

First is a routine to make sure the list is not empty


void remove_student(void)
{char student_name[15];
if(position_ptr == NULL){
printf(" * No student to remove (List empty)*\n");
}
else{

then a routine to let you specify which record to remove


/*Get name of student to remove from class list*/
printf("Name of student to remove ?\n"); eg 1.SMITH /2.CHRISTIE
scanf("%s", student_name);

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)

with a error checking part.


printf (“Student not in list);

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 )

/*Set item_ptr to start of search*/


item_ptr = start_ptr;
( start_ptr has the address received which was position_ptr or 3000)It could be
anything as it is just a mark to know when to hand the search.

/*Carry out search*/


This compare the required name with each record’s name in turn
do
{ /*Move to next record*/
previous_item_ptr = item_ptr;
item_ptr = item_ptr->pointer_to_next_record;
/*Check name against required name*/
if(!strcmp(name_required, item_ptr->name))
{/*Item found - return pointer*/

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; }

if record found (Receiving address of record just before record to remove as


temp_ptr)
position_ptr = temp_ptr; (transferring value of temp_ptr to position_ptr)

if (position_ptr == position_ptr->pointer_to_next_record) (in case only one record)


{/*Remove only record from list*/
free(position_ptr); (Releasing memory space now redundant and resetting pointer
to NULL)
position_ptr = NULL;}
else(
{/*Remove record if other in list*/
if (positionp_ptr->pointer_to_next_record = rota_ptr){ eg 1. N / 2. Y
(If the student removed is next in rota set rota_ptr to next student in rota)
rota_ptr = position_ptr->pointer_to_next_record->pointer_to_next_record;} eg 1. 1000 / 2. 2000

temp_ptr = position_ptr->pointer_to_next_record; eg 1. 3000 / 2. 1000


position_ptr->pointer_to_next_record = temp_ptr->pointer_to_next_record; eg 1. 1000 / 2. 2000
free(temp_ptr);
} }}
asking for SMITH to be removed would leave the following setup
1000 NULL 2000
CHRISTIE IAN THE DRIVE N1

2000
Temp_ptr

2000 NULL 1000


ALDER JOHN THE ROAD W3

2000 1000
position_ptr rota_ptr

asking for CHRISTIE to be removed would leave

2000 NULL 3000


ALDER JOHN THE ROAD W3

3000 NULL 2000


SMITH CHRIS THE AVENUE W3

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;

Error routine to warn user if no record are in central memory


/*if(position_ptr == NULL && temp_ptr == NULL );
{printf("\nNO DATA TO SAVE\n\n");
return;
} */

FILE *student_file_pointer;

student_file_pointer = fopen("student" ,"wb");

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;

fwrite(info, sizeof(struct student_type), 1, student_file_pointer);


info = info->pointer_to_next_record;
while(info != flag) {
fwrite(info, sizeof(struct student_type), 1, student_file_pointer);
info = info->pointer_to_next_record;
}

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);
}

/* free any previously allocated memory */


while(start) {
info = start->next;
free(info);
start = info;
}

/* reset top and bottom pointers */


start = last = NULL;

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);
}

You might also like