Here is the code we wrote today:
#include <stdio.h> #include <stdlib.h> struct listNode { int value; struct listNode* next; }; typedef struct listNode node; node* addFront(node* list, int val) { //adds value to front of list and returns pointer to new first node node* newNode = (node*) malloc(sizeof(node)); //(*newNode).next = list; newNode -> next = list; newNode -> value = val; return newNode; } void printList(node* list) { //print list contents node* curr = list; while(curr) { printf("%d ", curr->value); curr = curr -> next; } printf("\n"); } void freeList(node* list) { //deallocate all list memory while(list) { node* next = list->next; free(list); list = next; } } int main() { node* list = NULL; list = addFront(list, 214); list = addFront(list, 142); printList(list); freeList(list); }