Created
September 28, 2014 01:14
-
-
Save Prince781/8ba33490bcbfde550e67 to your computer and use it in GitHub Desktop.
Linked List generator in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "genlist.h" | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| void init_list(struct list **p, unsigned tail, unsigned loop) { | |
| if (*p != NULL) { | |
| fprintf(stderr, "init_list: base pointer not NULL\n"); | |
| return; | |
| } | |
| int n; | |
| struct list **tail_end = NULL; | |
| for (n=0; n<tail; ++n) { | |
| *p = malloc(sizeof(*p)); | |
| (*p)->n = n; | |
| (*p)->next = NULL; | |
| p = &(*p)->next; | |
| } | |
| tail_end = p; | |
| for (; n<tail+loop; ++n) { | |
| *p = malloc(sizeof(*p)); | |
| (*p)->n = n; | |
| (*p)->next = *tail_end; | |
| p = &(*p)->next; | |
| } | |
| } | |
| void printl(struct list *b, unsigned length) { | |
| int n; | |
| printf("linked list (size=%d):\n", length); | |
| for (n=0; n<length && b != NULL; ++n, b = b->next) | |
| if (b->next) | |
| printf("\tp_%u->next = p_%d\n", b->n, b->next->n); | |
| else | |
| printf("\tp_%u->next = NULL\n", b->n); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef GENLIST_H_ | |
| #define GENLIST_H_ | |
| struct list { | |
| struct list *next; /* pointer to next item */ | |
| unsigned n; /* number (for debug) */ | |
| }; | |
| /* | |
| * Initializes a list with a specified tail and loop length | |
| * @param p pointer to base pointer (assumed to be NULL) | |
| * @param tail length of tail | |
| * @param loop length of loop | |
| */ | |
| void init_list(struct list **p, unsigned tail, unsigned loop); | |
| /* | |
| * Prints out a linked list (looped or unlooped) with length. | |
| * @param b base pointer | |
| * @param length absolute length of list | |
| */ | |
| void printl(struct list *b, unsigned length); | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment