if i change the return type to linked list and the head_temp to type linked list
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int age;
struct Node *next;
}Node;
typedef struct LinkedList
{
struct Node *head;
struct Node *tail;
}LinkedList;
struct LinkedList *initalize_list(LinkedList *list);
struct LinkedList *create_head(void);
struct Node *get_memory(void);
int get_age(void);
void print_list(LinkedList list);
void delete_list(LinkedList *list);
int main()
{
//declare my list
LinkedList *list = NULL;
list = initalize_list(list);
print_list(*list);
delete_list(list);
return 0;
}
struct LinkedList *initalize_list(LinkedList *list)
{
//list->head = NULL;
//list->tail = NULL;
list->head = create_head();
list->head->next = NULL;
list->tail = list->head;
list->head->age = get_age();
return list;
}
struct LinkedList *create_head(void)
{
LinkedList *head_tmp;
head_tmp = get_memory();
if (!head_tmp)
{
fprintf(stderr, "error allocating memory\n");
exit(EXIT_FAILURE);// if it cant allocate memory for the head node at startup somthing really wrong so bug out
}
return head_tmp;
}
struct Node *get_memory(void)
{
return malloc(sizeof(Node));
}
int get_age(void)
{
int age;
printf("PLease enter an age: ");
scanf(" %d", &age);
return age;
}
void print_list(LinkedList list)
{
int count = 1;
Node *tmp = list.head;
while (tmp)
{
printf("person%d's age is %d\n", count, list.head->age);
count++;
tmp = list.head->next;
}
}
void delete_list(LinkedList *list)
{
int count = 1;
Node *tmp;
printf("freeing memory.....\n");
while (list->head)
{
tmp = list->head->next;
printf("freeing element %d\n", count);
free(list->head);
list->head = tmp;
count++;
}
printf("memory freed\n");
}
i get the following warnings:
Code:
linked list simple/main.c|40|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]|
/linked list simple/main.c|53|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]|