fork download
  1. #include <stdlib.h> //NULL
  2. #include <stdio.h> //printf
  3.  
  4. typedef struct Node {
  5. int val;
  6. struct Node* next;
  7. } node;
  8.  
  9. node* make_ll(int len){
  10. node* head = malloc(sizeof(node));
  11. node* cur = head;
  12. for (int i = 0; i < len; i++) {
  13. cur->val = i;
  14. cur->next = (i < (len - 1)) ? malloc(sizeof(node)) : NULL;
  15. cur = cur->next;
  16. }
  17. return head;
  18. }
  19.  
  20. void print_ll(node* head){
  21. node* cur = head;
  22. while (cur != NULL) {
  23. printf("Node: %d @ %p\n", cur->val, cur);
  24. cur = cur->next;
  25. }
  26. }
  27.  
  28. int main(){
  29. node* ll = make_ll(4);
  30. print_ll(ll);
  31. free(ll);
  32. }
Success #stdin #stdout 0s 2288KB
stdin
Standard input is empty
stdout
Node: 0 @ 0x8a0d008
Node: 1 @ 0x8a0d018
Node: 2 @ 0x8a0d028
Node: 3 @ 0x8a0d038