I was just trying to implement the linked list i.e. simply just entering the numbers until the EndOfFile using INT_MAX and then printing them
#include <cs50.h>
#include <stdio.h>
typedef struct node
{
int n;
struct node* next;
}node;
node* create(int);
void print(node*);
int main()
{
printf("Enter the numbers\n");
node *first = NULL;
while(true)
{
int x=get_int();
if(x==INT_MAX)
break;
else
{
node *new =create(x);
new->next=first;
first=new;
}
}
printf("Now I think it's the time to print our list\n");
print(first);
}
node* create(int val)
{
node* new = malloc(sizeof(node));
if(new!=NULL)
{
new->n=val;
new->next=NULL;
}
return new;
}
void print(node *first)
{
node *ptr=first;
while(ptr->next!=NULL)
{
printf("%d",ptr->n);
ptr=ptr->next;
}
}
Please also help that as the list.c is closed. Do I always need to free the memory using free to get rid of the memory leaks ?