Files
CS50---Harvard/Hayet_Classwork/list.c
T
2026-06-05 01:28:27 +00:00

53 lines
728 B
C

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
typedef struct node
{
int number;
struct node *next;
} node;
int main(void)
{
node *list = NULL;
// Create list
for (int i = 0; i < 3; i++)
{
int x = get_int("Number: ");
node *n = malloc(sizeof(node));
if (n == NULL)
{
return 1;
}
n->number = x;
n->next = list;
list = n;
}
// Print list
node *ptr = list;
while (ptr != NULL)
{
printf("%i\n", ptr->number);
ptr = ptr->next;
}
// Free memory
ptr = list;
while (ptr != NULL)
{
node *tmp = ptr;
ptr = ptr->next;
free(tmp);
}
}