Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAXC 1024 /* if you need a constant, #define one (or more) */
- typedef struct node_t { /* list node */
- char *s;
- struct node_t *next;
- } node_t;
- /** helper function to allocate node, and storage for string.
- * copies string to node and initializes node->next pointer NULL.
- * returns pointer to allocated node on success, NULL otherwise.
- */
- node_t *create_node (const char *s)
- {
- size_t len = strlen (s); /* get string length */
- node_t *node = malloc (sizeof *node); /* allocate node */
- if (!node) { /* validate EVERY allocation */
- perror ("create_node: malloc node");
- return NULL;
- }
- if (!(node->s = malloc (len + 1))) { /* allocate for string */
- perror ("create_node: malloc node->s");
- free (node); /* on failure, free node before returning NULL */
- return NULL;
- }
- memcpy (node->s, s, len+1); /* copy string to node */
- node->next = NULL; /* initialze next NULL */
- return node; /* return allocated & initialized node */
- }
- /** add node in sort order to list.
- * returns pointer to new node on success, NULL otherwise.
- */
- node_t *add_node (node_t **head, const char *s)
- {
- node_t *node = create_node (s); /* allocate/initialze node */
- if (!node) /* validate */
- return NULL;
- node_t **ppn = head, /* ptr2ptr to node - current */
- *pn = *head; /* pointer to iterate - next */
- /* iterate over each node checking sort order */
- while (pn && strcmp (node->s, pn->s) > 0) {
- ppn = &pn->next; /* ppn to address of next */
- pn = pn->next; /* advance pointer to next */
- }
- node->next = pn; /* set node->next to next */
- *ppn = node; /* set current to node */
- return node; /* return node */
- }
- /** print all nodes in list */
- void prn_list (node_t *head)
- {
- if (!head) { /* check if list is empty */
- puts ("list-empty");
- return;
- }
- for (node_t *pn = head; pn; pn = pn->next) /* iterate over each node */
- puts (pn->s); /* printing string */
- }
- /** delete all nodes in list */
- void del_list (node_t *head)
- {
- node_t *pn = head; /* pointer to iterate */
- while (pn) { /* iterate over each node */
- node_t *victim = pn; /* set victim to current */
- pn = pn->next; /* advance pointer to next */
- free (victim->s); /* free current string */
- free (victim); /* free current node */
- }
- }
- int main (int argc, char **argv) {
- char buf[MAXC]; /* read buffer */
- node_t *list = NULL; /* pointer to list (must initialize NULL */
- /* use filename provided as 1st argument (stdin by default) */
- FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
- if (!fp) { /* validate file open for reading */
- perror ("file open failed");
- return 1;
- }
- while (fgets (buf, MAXC, fp)) {
- buf[strcspn(buf, "\r\n")] = 0;
- add_node (&list, buf);
- }
- if (fp != stdin) /* close file if not stdin */
- fclose (fp);
- prn_list (list); /* pring contents of list */
- del_list (list); /* free all list memory */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement