Advertisement
utroz

Linked List

Dec 30th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <unistd.h>
  5.  
  6. typedef struct node {
  7.         int data;
  8.         struct node *next;
  9. } node_t;
  10.  
  11. void insert(node_t *head, int data)
  12. {
  13.         node_t *new, *p;
  14.        
  15.         new = malloc(sizeof(node_t));
  16.         if(new == NULL) {
  17.     perror("memory can't be allocated");
  18.     EXIT(EXIT_FAILURE);
  19.  
  20.         new->data = data;
  21.        
  22.         new->next = NULL;
  23.         for(p = head; p->next != NULL; p = p->next)
  24.     ;
  25.         p->next = new;
  26. }
  27.  
  28. void print_list(node_t *head)
  29. {
  30.         node_t *p = head;
  31.        
  32.         while(p->next != NULL) {
  33.     p = p->next;
  34.     fprintf(stdout, "(PID: %d) Memory: %p | Data = %d | Next: %p\n", getpid(), p, p->data, p->next);
  35.         }
  36. }
  37.  
  38. int main(void)
  39. {
  40.         node_t *head;
  41.         pid_t pid;
  42.        
  43.         head = malloc(sizeof(node_t));
  44.         if(new == NULL) {
  45.     perror("memory can't be allocated");
  46.     EXIT(EXIT_FAILURE);
  47.         }
  48.    
  49.         head->next = NULL;
  50.         head->data = 0;
  51.        
  52.         insert(head, 1);
  53.         insert(head, 2);
  54.        
  55.         print_list(head);
  56.    
  57.         free(head);        
  58.         return EXIT_SUCCESS;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement