stgatilov

C Template List GDB printer: V1 C

Aug 8th, 2026
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stddef.h>
  3. #include <assert.h>
  4.  
  5. typedef struct listNode {
  6.     void                 *data;         /* node's data                       */
  7.     struct listNode      *next;         /* next node in list                 */
  8. } listNode;
  9.  
  10. typedef void (*ListDelF)(void*);
  11.  
  12. typedef struct xlist {
  13.     struct listNode      *head;         /* head of the list                  */
  14.     struct listNode     **tail;         /* addr of last node's 'next' ptr    */
  15.     struct listIterator  *iNext;        /* iterator chain for list_destroy() */
  16.     ListDelF              fDel;         /* function to delete node data      */
  17.     int                   count;        /* number of nodes in list           */
  18. } xlist;
  19.  
  20. typedef struct xlist * List;
  21.  
  22.  
  23. listNode *createNode(void* data) {
  24.     listNode *node = (listNode*)malloc(sizeof(listNode));
  25.     node->data = data;
  26.     node->next = NULL;
  27.     return node;
  28. }
  29. xlist *createList() {
  30.     xlist *list = (xlist*)malloc(sizeof(xlist));
  31.     list->fDel = &free;
  32.     list->iNext = NULL;
  33.     list->count = 0;
  34.     list->head = createNode(NULL);
  35.     list->tail = &list->head->next;
  36.     return list;
  37. }
  38. void addNodeToList(xlist *list, listNode* node) {
  39.     assert(!node->next);
  40.     *list->tail = node;
  41.     list->tail = &node->next;
  42.     list->count++;
  43. }
  44. int *createInt(int x) {
  45.     int *ptr = (int*)malloc(sizeof(int));
  46.     *ptr = x;
  47.     return ptr;
  48. }
  49.  
  50. int main() {
  51.     int N = 10;
  52.     List mylist = createList();
  53.     for (int i = 0; i < N; i++)
  54.         addNodeToList(mylist, createNode(createInt(i * 71 % 100)));
  55.  
  56.     return 0;
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment