Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #ifndef NNODES
- #define NNODES 10
- #endif
- /*
- * non-list misc functions
- */
- /** rand_int for use with shuffle */
- static int rand_int (int n)
- {
- int limit = RAND_MAX - RAND_MAX % n, rnd;
- rnd = rand();
- for (; rnd >= limit; )
- rnd = rand();
- return rnd % n;
- }
- /** shuffle integer array of size 'n'
- * (using fisher-yates method)
- */
- void shuffle (int *a, int n)
- {
- int i, tmp;
- while (n-- > 1) {
- i = rand_int (n + 1);
- tmp = a[i];
- a[i] = a[n];
- a[n] = tmp;
- }
- }
- /*
- * list structs and functions
- */
- typedef struct node_t { /* list node */
- int data;
- struct node_t *next;
- } node_t;
- typedef struct { /* list wrapper with head & tail pointers */
- node_t *head, *tail;
- } list_t;
- /** add node at end of list, update tail to end */
- node_t *add (list_t *l, int v)
- {
- node_t *node = malloc (sizeof *node); /* allocate node */
- if (!node) { /* validate allocation */
- perror ("malloc-node");
- return NULL;
- }
- node->data = v; /* initialize members values */
- node->next = NULL;
- if (!l->head) /* if 1st node, node is head/tail */
- l->head = l->tail = node;
- else { /* otherwise */
- l->tail->next = node; /* add at end, update tail pointer */
- l->tail = node;
- }
- return node; /* return new node */
- }
- /** add node in-order */
- node_t *add_ordered (list_t *l, int v)
- {
- node_t **ppn = &l->head; /* pointer to pointer */
- node_t *pn = l->head; /* pointer to node */
- node_t *node = malloc (sizeof *node); /* allocate node */
- if (!node) { /* validate allocation */
- perror ("malloc-node");
- return NULL;
- }
- node->data = v; /* initialize members values */
- node->next = NULL;
- if (!l->head) { /* if 1st node, node is head/tail */
- l->head = l->tail = node;
- return l->tail;
- }
- while (pn) { /* iterate over list */
- if (v < pn->data) { /* if new node goes before current */
- *ppn = node; /* set current address to node */
- node->next = pn; /* set next to current */
- return node;
- }
- ppn = &pn->next; /* get address of next node */
- pn = pn->next; /* get next node pointer */
- }
- l->tail->next = node; /* node goes at end, update tail pointer */
- l->tail = node;
- return node;
- }
- /** print all nodes in list */
- void prn (list_t *l)
- {
- if (!l->head) {
- puts ("list-empty");
- return;
- }
- for (node_t *n = l->head; n; n = n->next)
- printf (" %d", n->data);
- putchar ('\n');
- }
- /** delete node with value v from list (for loop) */
- void del_node (list_t *l, int v)
- {
- node_t **ppn = &l->head; /* pointer to pointer */
- node_t *pn = l->head; /* pointer to node */
- for (; pn; ppn = &pn->next, pn = pn->next) {
- if (pn->data == v) {
- *ppn = pn->next; /* set address to next */
- free (pn);
- break;
- }
- }
- }
- /** delete all nodes in list */
- void del_list (list_t *l)
- {
- node_t *n = l->head;
- while (n) {
- node_t *victim = n;
- n = n->next;
- free (victim);
- }
- }
- int main (void) {
- list_t l = { NULL, NULL }; /* initialize list pointers NULL */
- int a[NNODES]; /* array to shuffle */
- srand (time (NULL)); /* seed random number generator */
- for (int i = 0; i < NNODES; i++) { /* fill array 1 - 10 */
- // add (&l, i+1);
- a[i] = i+1;
- }
- shuffle (a, NNODES); /* shuffle array */
- for (int i = 0; i < NNODES; i++) { /* add shuffled values in order */
- #ifdef DEBUG
- printf ("adding : %d\n", a[i]);
- #endif
- add_ordered (&l, a[i]);
- }
- add (&l, NNODES + 1); /* add at end for tail-pointer test */
- prn (&l); /* print list */
- del_node (&l, NNODES + 1); /* delete tail node */
- shuffle (a, NNODES); /* shuffle array again */
- for (int i = 0; i < NNODES; i++) { /* remove nodes in random order */
- #ifdef DEBUG
- printf ("deleting: %d\n", a[i]);
- #endif
- del_node (&l, a[i]);
- }
- prn (&l); /* print list ("list-empty") */
- del_list (&l); /* delete list (does nothing list-empty) */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement