Advertisement
Guest User

list.c

a guest
Nov 14th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Node{
  5.     int val;
  6.     struct Node *next;
  7. };
  8.  
  9. void insert(struct Node* head, int value);
  10. void print(struct Node *head);
  11.  
  12. int main(){
  13.     struct Node *head;
  14.  
  15.     head = (struct Node*)malloc(sizeof(struct Node));
  16.  
  17.     int i = 0;
  18.  
  19.     for(i = 0; i<10; i++)
  20.         insert(head, i*2);
  21.  
  22.     print(head);
  23.    
  24.     return 0;
  25. }
  26.  
  27. void insert(struct Node* head, int value){
  28.     if(head == NULL){
  29.         printf("NULL");
  30.         head->val = value;
  31.         head->next = NULL;
  32.     }
  33.     else {
  34.         printf("nULL2\n");
  35.     struct Node *tmp = (struct Node*)malloc(sizeof(struct Node));
  36.     tmp->val = value;
  37.     tmp->next = NULL;
  38.  
  39.     while(head->next != NULL){
  40.         head = head->next;
  41.     }
  42.  
  43.     head->next = tmp;
  44.     }
  45. }
  46.  
  47. void print(struct Node *head){
  48.     struct Node *current = head;
  49.     while(current != NULL){
  50.         printf("%d\n", current->val);
  51.         current = current->next;
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement