Pointers: Beyond the Basics 427
17: struct list *next_rec; 15
18: };
19:
20: /* Typedefs for the structure and pointer. */
21: typedef struct list LIST;
22: typedef LIST *LISTPTR;
23:
24: /* Function prototypes. */
25: LISTPTR add_to_list( int, LISTPTR );
26: void show_list(LISTPTR);
27: void free_memory_list(LISTPTR);
28:
29: int main( void )
30: {
31: LISTPTR first = NULL; /* head pointer */
32: int i = 0;
33: int ch;
34: char trash[256]; /* to clear stdin buffer. */
35:
36: while ( i++ < 5 ) /* build a list based on 5 items given */
37: {
38: ch = 0;
39: printf(“\nEnter character %d, “, i);
40:
41: do
42: {
43: printf(“\nMust be a to z: “);
44: ch = getc(stdin); /* get next char in buffer */
45: gets(trash); /* remove trash from buffer */
46: } while( (ch < ‘a’ || ch > ‘z’) && (ch < ‘A’ || ch > ‘Z’));
47:
48: first = add_to_list( ch, first );
49: }
50:
51: show_list( first ); /* Dumps the entire list */
52: free_memory_list( first ); /* Release all memory */
53: return 0;
54: }
55:
56: /*========================================================*
57: * Function: add_to_list()
58: * Purpose : Inserts new link in the list
59: * Entry : int ch = character to store
60: * LISTPTR first = address of original head pointer
61: * Returns : Address of head pointer (first)
62: *========================================================*/
63:
64: LISTPTR add_to_list( int ch, LISTPTR first )
65: {
LISTING15.13 continued
25 448201x-CH15 8/13/02 11:13 AM Page 427