Here is the list code as we left it today:
#include <stdlib.h> #include <stdio.h> struct listnode { int value; struct listnode* next; }; typedef struct listnode node; node* addFront(node* list, int newValue) { //returns list with newValue added to the front node* newNode = (node*) malloc(sizeof(node)); newNode -> value = newValue; newNode -> next = list; return newNode; } void print(node* list) { while(list) { printf("%d ", list->value); list = list->next; } printf("\n"); } node* find(node* list, int val) { //return node containing first occurrence of val; return NULL if not there while(list) { if(list -> value == val) return list; list = list -> next; } return NULL; //traversed entire list and didn't find it } node* add(node* list, int val) { //add val to the end of list; return pointer to beginning of resulting list node* first = list; while(list) { if(list -> next == NULL) { //node* newNode = (node*) malloc(sizeof(node)); //newNode -> value = newValue; //newNode -> next = list; //list -> next = newNode; list -> next = addFront(list->next, val); return first; } list = list -> next; } //base case: adding to empty list return addFront(list, val); } int main() { node* list = NULL; list = addFront(list, 42); list = addFront(list, 214); list = add(list, 100); list = add(list, 142); print(list); if(find(list, 42)) printf("found it!\n"); else printf("didn't find it =(\n"); }