week 6
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
#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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user