Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node_t { /* list node */
- int data;
- struct node_t *next;
- } node_t;
- /** add node at end of list, update tail to end */
- node_t *add (node_t **head, int v)
- {
- node_t **ppn = head, /* pointer to pointer to head */
- *pn = *head, /* pointer to head */
- *node = malloc (sizeof *node); /* allocate new node */
- if (!node) { /* validate allocation */
- perror ("malloc-node");
- return NULL;
- }
- node->data = v; /* initialize members values */
- node->next = NULL;
- while (pn) {
- ppn = &pn->next;
- pn = pn->next;
- }
- return *ppn = node; /* add & return new node */
- }
- /** insert nodes in linked-list in ascending order of data. */
- node_t *addinorder (node_t **head, int v)
- {
- node_t **ppn = head, /* pointer to pointer to head */
- *pn = *head, /* pointer to head */
- *node = malloc (sizeof *node); /* allocate new node */
- if (!node) { /* validate allocation */
- perror ("malloc-node");
- return NULL;
- }
- node->data = v; /* initialize members values */
- node->next = NULL;
- while (pn && pn->data <= node->data) { /* loop until sort location */
- ppn = &pn->next; /* advance pointer-to-pointer */
- pn = pn->next; /* advance pointer */
- }
- node->next = pn; /* set node->next to pointer to current */
- return *ppn = node; /* update pointer at current addr to node */
- }
- /** insertion sort of linked list.
- * re-orders list in sorted order.
- */
- void insertionsort (node_t **head)
- {
- if (!*head || !(*head)->next) /* if list empty or 1-node, return */
- return;
- node_t *sorted = *head, /* initialize sorted list to 1st node */
- *pn = (*head)->next; /* advance original list node to next */
- sorted->next = NULL; /* initialize sorted->next to NULL */
- while (pn) { /* iterate over existing from 2nd node */
- node_t **pps = &sorted, /* ptr-to-ptr to sorted list */
- *ps = *pps, /* ptr to sorted list */
- *next = pn->next; /* save list next as separate pointer */
- while (ps && ps->data <= pn->data) { /* loop while sorted <= node */
- pps = &ps->next; /* get address of next node */
- ps = ps->next; /* get next node pointer */
- }
- *pps = pn; /* insert existing in sort order as current */
- pn->next = ps; /* set next as sorted next */
- pn = next; /* reinitialize existing pointer to next */
- }
- *head = sorted; /* update head to sorted head */
- }
- /** print all nodes in list */
- void prn (node_t *l)
- {
- if (!l) {
- puts ("list-empty");
- return;
- }
- for (node_t *n = l; n; n = n->next)
- printf (" %d", n->data);
- putchar ('\n');
- }
- /** delete node with value v from list (for loop) */
- void del_node (node_t **l, int v)
- {
- node_t **ppn = l; /* pointer to pointer */
- node_t *pn = *l; /* pointer to node */
- for (; pn; ppn = &pn->next, pn = pn->next) {
- if (pn->data == v) {
- *ppn = pn->next; /* set node at address to next */
- free (pn);
- break;
- }
- }
- }
- /** delete all nodes in list */
- void del_list (node_t *l)
- {
- node_t *n = l;
- while (n) {
- node_t *victim = n;
- n = n->next;
- free (victim);
- }
- }
- int main (void) {
- int a[] = {12, 11, 10, 7, 4, 14, 8, 16, 20, 19,
- 2, 9, 1, 13, 17, 6, 15, 5, 3, 18};
- size_t asz = sizeof a / sizeof *a;
- node_t *list = NULL;
- for (size_t i = 0; i < asz; i++)
- #ifdef ADDORDER
- addinorder (&list, a[i]);
- #else
- add (&list, a[i]);
- #endif
- prn (list);
- #ifndef ADDORDER
- insertionsort (&list);
- prn (list);
- #endif
- del_list (list);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement