48 lines
610 B
C
48 lines
610 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));
|
|
n->number = x;
|
|
n->next = list;
|
|
|
|
list = n;
|
|
|
|
if (n == NULL)
|
|
{
|
|
printf("Malloc failed. \n");
|
|
return 1;
|
|
|
|
}
|
|
|
|
for (node *ptr = list; ptr !=NULL; ptr = ptr->next)
|
|
{
|
|
printf("%i\n", ptr->number);
|
|
}
|
|
|
|
node *ptr = list;
|
|
while(ptr !=NULL)
|
|
{
|
|
node *tmp = ptr;
|
|
ptr = ptr->next;
|
|
free(tmp);
|
|
}
|
|
|
|
}
|
|
|
|
}
|