/* Sandra N. Kardaras-Flick 03/18/05 LINKED LISTS A program which reads a list of integers from the keyboard, creates a linked list out of them, and prints the result. */ #include #include /*Global Declarations */ typedef int KEY_TYPE; typedef struct { KEY_TYPE key; /* Other data goes here */ } DATA; typedef struct nodeTag { DATA data; struct nodeTag *link; } NODE; /*Prototype Declarations */ void printList (NODE * pList); int main (void) { /*Local Definitions */ int num; NODE *pList = NULL; NODE *pNew; NODE *pRear = NULL; /*Statements */ /* Insert first node into linked list */ printf ("\n\n\nEnter a list of numbers for a linked list.\nEnd your list with EOF:\n\n"); while (scanf ("%d", &num) == 1) { pNew = (NODE *) malloc (sizeof (NODE)); if (!pNew) { printf ("\aCan not allocate node.\n"); return 100; } /* if */ pNew->data.key = num; pNew->link = NULL; if (pList == NULL) /* first node */ pList = pRear = pNew; else /* other nodes */ { pRear->link = pNew; pRear = pNew; } /* else */ } /* while */ printf ("\nLinked list complete.\n"); printList (pList); return 0; }/* main */ /*================== printList ===================== Traverse and print the keys from a linked list Pre: pList is a valid linked list Post: Keys of list have been printed */ void printList (NODE *pList) { /*Local Definitions */ NODE *pWalker; int lineCount = 0; /*Statements */ pWalker = pList; printf ("\nList contains:\n"); while (pWalker) { if (++lineCount > 10) { lineCount = 1; printf ("\n"); } /* if */ printf ("%d => ", pWalker->data.key); pWalker = pWalker->link; } /* while */ printf ("\n\n"); return; }/* printList */