/**************************************************************************/ /* LAB52.C: Recursion and Linked Lists */ #include #define LISTSIZE 10 typedef struct node_tag { int number; struct node_tag *next; }node_type; /*********************** function build_list *************************/ /* Preconditions: none */ /* Postconditions: List points to a linked list of integers */ /* If insufficient memory exists the function returns NULL*/ /* List values are read from the file list.dat and added to end of list */ node_type *build_list() { FILE *indatpt; /* file pointer variable for input data file */ node_type *p, /* p is the pointer to the first node in the list */ *q; /* q points to each node as it is created */ int i; /* Open input file */ if ((indatpt = fopen("list.dat", "r")) == NULL) { printf("Error in opening list.dat. Terminate program\n"); exit(1); } /* Initialize first list node */ p = (node_type *)malloc(sizeof(node_type)); fscanf(indatpt, "%d", &p->number); q = p; /* q points to first list node, too */ for (i = 2; (i <= LISTSIZE && q != NULL); ++i) { /* add nodes */ /* Allocate memory for next node, attach to list, store data in it */ q->next = (node_type *)malloc(sizeof(node_type)); q = q->next; /* advance pointer to next node */ if (q != NULL) { /* Be sure malloc succeeded */ fscanf(indatpt, "%d", &q->number); /* Get next data */ } /* end if q != NULL */ } /* end for loop to build the list */ fclose(indatpt); if (q == NULL) p = NULL; /* not enough memory for list; set list pointer to NULL */ else q->next = NULL; /* be sure to set pointer in last node to NULL */ return p; } /********************** function print_list ****************************/ /* Preconditions: none */ /* Postconditions: The list pointed to by p is printed; */ void print_list(node_type *p) { node_type *temp; int i = 1; printf("The contents of the list:\n"); for (temp = p; temp != NULL; temp = temp->next) { printf("%d ", temp->number); ++i; } printf("\n"); return; } /********************* reverse_print ***********************/ /* Prints a list in reverse order by making repeated recursive calls */ void reverse_print(node_type *list) { if (list != NULL) { reverse_print(list->next); printf("%d ", list->number); } /* terminating case is list == NULL; no action necessary */ return; } /********************* function add_node ***********************/ /* Adds a node to the end of list. Successive recursive calls are used /* /* to locate the last node in the list, which is identified by the NULL */ /* pointer. */ void add_node(node_type **list, int new_val) { printf("This is a stub for function add_node. Insert your code here\n"); return; } /********************* main function **********************/ int main(void) { node_type *List; int N; printf("\nBEGIN\n"); List = NULL; List = build_list(); print_list(List); printf("The contents of the list in reverse order:\n"); reverse_print(List); printf("\n"); printf("Enter an integer to add to the list.\n"); scanf("%d", &N); add_node(&List, N); reverse_print(List); return(0); }