#include "node.h" #include #include int length (nodeptr head); int length2 (nodeptr head); int main () { nodeptr mt = 0; nodeptr list1 = calloc(1, sizeof(node)); if (! list1) exit(-1); list1->first = 7; list1->rest = mt; nodeptr list2 = calloc(2, sizeof(node)); if (! list2) exit(-1); list2->first = 12; list2->rest = list1; nodeptr list3 = calloc(2, sizeof(node)); if (! list3) exit(-1); list3->first = -97; list3->rest = list2; printf ("list1->first = %d\n", list1->first); printf ("list2->first = %d\n", list2->first); printf ("length(mt) = %d (should be 0)\n", length(mt)); printf ("length(list1) = %d (should be 1)\n", length(list1)); printf ("length(list2) = %d (should be 2)\n", length(list2)); printf ("length(list3) = %d (should be 3)\n", length(list3)); printf ("length2(mt) = %d (should be 0)\n", length2(mt)); printf ("length2(list1) = %d (should be 1)\n", length2(list1)); printf ("length2(list2) = %d (should be 2)\n", length2(list2)); printf ("length2(list3) = %d (should be 3)\n", length2(list3)); return 0; } int length (nodeptr head) { int count = 0; while (head) { head = head->rest; ++count; } return count; } int length2 (nodeptr head) { if (head) return 1 + length2(head->rest); else return 0; }