Advertisement
Guest User

Untitled

a guest
May 21st, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. struct node {
  5.   int value;
  6.   struct node *next;
  7. };
  8.  
  9. typedef struct node *tlista;
  10.  
  11. void print_list(tlista head) {
  12.   tlista current = head;
  13.  
  14.   if(current == NULL){
  15.     printf("NULL\n");
  16.   }
  17.  
  18.   while (current != NULL) {
  19.     printf("%d -> ", current->value);
  20.     current = current->next;
  21.     if(!current){
  22.       printf("NULL\n");
  23.     }
  24.   }
  25.   printf("\n");
  26. }
  27.  
  28. void push(tlista head, int val) {
  29.   tlista current = head;
  30.   while (current->next != NULL) {
  31.     current = current->next;
  32.   }
  33.  
  34.   /* now we can add a new variable */
  35.   current->next = (tlista)malloc(sizeof(struct node));
  36.   current->next->value = val;
  37.   current->next->next = NULL;
  38. }
  39.  
  40. void free_list(tlista head) {
  41.   tlista tmp;
  42.  
  43.   while (head != NULL) {
  44.     tmp = head;
  45.     head = head->next;
  46.     free(tmp);
  47.   }
  48. }
  49.  
  50. nt main(){
  51.   tlista l1 = NULL;
  52.   l1 = malloc(sizeof(struct node));
  53.   l1->value = 4;
  54.   l1->next = NULL;
  55.  
  56.   push(l1, 6);
  57.   push(l1, 2);
  58.   push(l1, 6);
  59.   push(l1, 7);
  60.   push(l1, 9);
  61.  
  62.   printf("lista1: ");
  63.   print_list(l1);
  64.  
  65.   free(l1);
  66.  
  67.   return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement