193030

Linked List with void * data

Nov 25th, 2021 (edited)
774
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Example program
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. struct NodeList
  5. {
  6.     void* data;
  7.     struct NodeList* next;
  8. } *head = NULL;
  9.  
  10. void prnint(void* p) {
  11.     printf("%c ", *(char*)p);
  12. }
  13.  
  14. void createList(char a[], int n, struct NodeList** r)
  15. {
  16.     struct NodeList* root;
  17.     root = (struct NodeList*)malloc(sizeof(struct NodeList));
  18.  
  19.     void* p = malloc(sizeof(a[0]));
  20.     *(char*)p = a[0];
  21.     root->data = p;
  22.     root->next = NULL;
  23.    
  24.  
  25.  
  26.     struct NodeList* last = root;
  27.     for (int i = 1; i < n; i++)
  28.     {
  29.         struct NodeList* t = (struct NodeList*)malloc(sizeof(struct NodeList));
  30.         last->next = t;
  31.         void* p2 = malloc(sizeof(char));
  32.         *(char*)p2 = a[i];
  33.         t->data = p2;
  34.         t->next = NULL;
  35.         last = t;
  36.     }
  37.  
  38.     (*r) = root;
  39.     //   prnint(head->data); // принтираме стойността помощната функция
  40.  
  41.  
  42. }
  43.  
  44. void printList(struct NodeList** Head)
  45. {
  46.     struct NodeList* p = *Head;
  47.     while (p)
  48.     {
  49.         prnint(p->data);
  50.         p = p->next;
  51.     }
  52. }
  53.  
  54.  
  55. int main()
  56. {
  57.     char a[] = { 'a','b','c' };
  58.     createList(a, sizeof(a) / sizeof(a[0]), &head);
  59.     //prnint(head->data);
  60.     printList(&head);
  61. }
RAW Paste Data