Advertisement
Guest User

Untitled

a guest
May 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 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 stampa_ar(tlista l1, tlista l2){
  12.   if(l1->next == l2){
  13.     printf("%d -> ", l1->value);
  14.     return;
  15.   }
  16.  
  17.   stampa_ar(l1->next, l2);
  18.   printf("%d -> ", l1->value);
  19. }
  20.  
  21. void print_list(tlista head) {
  22.   tlista current = head;
  23.  
  24.   if(current == NULL){
  25.     printf("NULL\n");
  26.   }
  27.  
  28.   while (current != NULL) {
  29.     printf("%d -> ", current->value);
  30.     current = current->next;
  31.     if(!current){
  32.       printf("NULL\n");
  33.     }
  34.   }
  35.   printf("\n");
  36. }
  37.  
  38. void pushLast(tlista head){
  39.   tlista current = head;
  40.   while (current->next != NULL){
  41.     current = current->next;
  42.   }
  43.  
  44.   current->next = head;
  45. }
  46.  
  47. void push(tlista head, int val) {
  48.   tlista current = head;
  49.   while (current->next != NULL) {
  50.     current = current->next;
  51.   }
  52.  
  53.   /* now we can add a new variable */
  54.   current->next = (tlista)malloc(sizeof(struct node));
  55.   current->next->value = val;
  56.   current->next->next = NULL;
  57. }
  58.  
  59. void free_list(tlista head) {
  60.   tlista tmp;
  61.  
  62.   while (head != NULL) {
  63.     tmp = head;
  64.     head = head->next;
  65.     free(tmp);
  66.   }
  67. }
  68.  
  69. int main(){
  70.   tlista l1 = NULL;
  71.   l1 = malloc(sizeof(struct node));
  72.   l1->value = 4;
  73.   l1->next = NULL;
  74.  
  75.   push(l1, 6);
  76.   push(l1, 2);
  77.   push(l1, 6);
  78.   push(l1, 7);
  79.   push(l1, 9);
  80.   printf("lista1: ");
  81.   print_list(l1);
  82.   pushLast(l1);
  83.  
  84.   stampa_ar(l1, l1);
  85.  
  86.   free(l1);
  87.  
  88.   return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement