stgatilov

C Template List GDB printer: V2 C

Aug 8th, 2026
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 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.     const char           *dataType;     /* string literal: type of node data */
  19. } xlist;
  20.  
  21. typedef struct xlist * List;
  22.  
  23.  
  24. listNode *createNode(void* data) {
  25.     listNode *node = (listNode*)malloc(sizeof(listNode));
  26.     node->data = data;
  27.     node->next = NULL;
  28.     return node;
  29. }
  30. xlist *createList(const char *dataType) {
  31.     xlist *list = (xlist*)malloc(sizeof(xlist));
  32.     list->fDel = &free;
  33.     list->iNext = NULL;
  34.     list->count = 0;
  35.     list->head = createNode(NULL);
  36.     list->tail = &list->head->next;
  37.     list->dataType = dataType;
  38.     return list;
  39. }
  40. void addNodeToList(xlist *list, listNode* node) {
  41.     assert(!node->next);
  42.     *list->tail = node;
  43.     list->tail = &node->next;
  44.     list->count++;
  45. }
  46. int *createInt(int x) {
  47.     int *ptr = (int*)malloc(sizeof(int));
  48.     *ptr = x;
  49.     return ptr;
  50. }
  51.  
  52. int main() {
  53.     int N = 10;
  54.     List mylist = createList("int");
  55.     for (int i = 0; i < N; i++)
  56.         addNodeToList(mylist, createNode(createInt(i * 71 % 100)));
  57.  
  58.     return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment