Advertisement
Guest User

Untitled

a guest
Jan 11th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct List {
  5.     int value;
  6.     struct List *next;
  7. };
  8.  
  9. void insert(struct List **root, int value) {
  10.     if (*root == NULL) {
  11.         *root = (struct List*)malloc(sizeof(struct List));
  12.         (*root)->value = value;
  13.         (*root)->next = NULL;
  14.     } else {
  15.         struct List *tmp = (struct List*)malloc(sizeof(struct List));
  16.         tmp->value = value;
  17.         tmp->next = *root;
  18.         *root = tmp;
  19.     }
  20. }
  21.  
  22. void print(struct List *root) {
  23.     while (root->next != NULL) {
  24.         printf("%d, ", root->value);
  25.         root = root->next;
  26.     }
  27.  
  28.     printf("%d\n", root->value);
  29. }
  30.  
  31. int main() {
  32.     struct List *list = NULL;
  33.  
  34.     for (int i = 0; i < 10; i++) {
  35.         insert(&list, i);
  36.     }
  37.  
  38.     print(list);
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement