Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stddef.h>
- #include <assert.h>
- typedef struct listNode {
- void *data; /* node's data */
- struct listNode *next; /* next node in list */
- } listNode;
- typedef void (*ListDelF)(void*);
- typedef struct xlist {
- struct listNode *head; /* head of the list */
- struct listNode **tail; /* addr of last node's 'next' ptr */
- struct listIterator *iNext; /* iterator chain for list_destroy() */
- ListDelF fDel; /* function to delete node data */
- int count; /* number of nodes in list */
- } xlist;
- typedef struct xlist * List;
- listNode *createNode(void* data) {
- listNode *node = (listNode*)malloc(sizeof(listNode));
- node->data = data;
- node->next = NULL;
- return node;
- }
- xlist *createList() {
- xlist *list = (xlist*)malloc(sizeof(xlist));
- list->fDel = &free;
- list->iNext = NULL;
- list->count = 0;
- list->head = createNode(NULL);
- list->tail = &list->head->next;
- return list;
- }
- void addNodeToList(xlist *list, listNode* node) {
- assert(!node->next);
- *list->tail = node;
- list->tail = &node->next;
- list->count++;
- }
- int *createInt(int x) {
- int *ptr = (int*)malloc(sizeof(int));
- *ptr = x;
- return ptr;
- }
- int main() {
- int N = 10;
- List mylist = createList();
- for (int i = 0; i < N; i++)
- addNodeToList(mylist, createNode(createInt(i * 71 % 100)));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment