here is my effort to put the above into code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Address
{
int door_number;
char *street;
char *city;
struct Address *next;
}Address;
void add_address(Address *first, Address *current);
void print_list(Address *first);
void delete_list(Address *first);
int main()
{
Address *first = NULL, *current = NULL;
add_address(first, current);
add_address(first, current);
add_address(first, current);
add_address(first, current);
add_address(first, current);
print_list(first);
delete_list(first);
return 0;
}
void add_address(Address *first, Address *current)
{
char my_str[20];
Address *p_new_element;
//get a new block of memmory and test the pointer isnt null
p_new_element = malloc(sizeof(Address));
if (!p_new_element)
{
printf("memmory allocation error");
return;
}
//get door number and assign it to the new elements member 'door_number'
printf("Please enter the door number: ");
scanf(" %d", &p_new_element->door_number);
//get street name and assign it to my_str
printf("Please enter street name: ");
scanf(" %s", my_str);
//create memory for the street name and assign the pointer address->street to point to it
p_new_element->street = malloc(strlen(my_str) + 1);
//copy contents of my_str into the memmory location pointed to by address->street
strcpy(p_new_element->street, my_str);
//get city name
printf("Please enter city name: ");
scanf(" %s", my_str);
//create memory for the city name and assign the pointer address->city to point to it
p_new_element->city = malloc(strlen(my_str) + 1);
//copy contents of my_str into the memmory location pointed to by address->city
strcpy(p_new_element->city, my_str);
if (first == NULL) // no list yet so make p_new_element the head
{
first = p_new_element;
first->next = NULL;
current = first;
return;
}
if (first->next == NULL) //only one element in the list
{
first->next = p_new_element;
current = p_new_element;
current->next = NULL;
return;
}
//more than 2 elements
current->next = p_new_element;
current = p_new_element;
current->next = NULL;
}
void print_list(Address *first)
{
int count = 1;
Address *temp = first;
while (temp)
{
printf("address no %d is door number %d street %s city %s\n", count, temp->door_number,
temp->street, temp->city);
count++;
temp = temp->next;
}
}
void delete_list(Address *first)
{
int count = 1;
Address *temp = NULL;
printf("freeing memmory....\n");
while (first)
{
temp = first->next; // set temp to point at the next element in the list
printf("freeing element %d's street name\n", count);
free(first->street); //free the memmory block that holds firsts street name
printf("freeing element %d's city name\n", count);
free(first->city); //free the memmory block that holds firsts city name
printf("freeing struct %d's memmory\n", count);
free(first); //free the memmory block that holds the first element
first = temp; // set first to point at the next memmory block;
count++;
}
printf("finished freeing all allocated memmory\n");
}
of course it doesn't work
i input 5 lots of addresses then it doesn't print any of them then says freeing memory and then jumps straight to memory freed. on stepping through the code first is never changed from being set to null despite add address being passed a pointer to first.
coop