Advertisement
Guest User

list.c

a guest
Feb 23rd, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "list_2.h"
  4.  
  5. struct nodo{
  6.     int key;
  7.     link pred, next;
  8. };
  9.  
  10. void swap(list l, int k){
  11.     int i = 0, tmp;
  12.     link h = l.head;
  13.     link t = l.tail;
  14.     if(k < 0) //aggiunto controllo
  15.         return;
  16.     if(h == NULL)
  17.         return;
  18.     for( ; i < k ; t = t->pred, h =  h->next){
  19.         if(t == h)
  20.             return;
  21.         i++;
  22.     }
  23.     tmp = h->key;
  24.     h->key = t->key;
  25.     t->key = tmp;
  26. }
  27.  
  28. void initList(list *l){
  29.     l->tail = NULL;
  30.     l->head = NULL;
  31.  
  32. }
  33.  
  34. void insertHead(list *l, int val){
  35.     if(l->head == NULL){
  36.         l->head = malloc(sizeof(struct nodo));
  37.         l->head->pred = NULL;
  38.         l->head->key = val;
  39.         l->head->next = NULL;
  40.         l->tail = l->head;
  41.         return;
  42.     }
  43.     link x = malloc(sizeof(link *));
  44.     if(x == NULL){
  45.         printf("errore nell'allocazione del nodo\n");
  46.         return;
  47.     }
  48.     x->key = val;
  49.     x->pred = NULL;
  50.     x->next = l->head;
  51.     l->head->pred = x;
  52.     l->head = x;
  53. }
  54.  
  55.  
  56. void insertTail(list *l, int val){
  57.     if(l->tail == NULL){
  58.         l->head = malloc(sizeof(struct nodo));
  59.         l->head->pred = NULL;
  60.         l->head->key = val;
  61.         l->head->next = NULL;
  62.         l->tail = l->head;
  63.         return;
  64.     }
  65.     link x = malloc(sizeof(link *));
  66.     if(x == NULL){
  67.         printf("errore nell'allocazione del nodo\n");
  68.         return;
  69.     }
  70.     x->key = val;
  71.     x->pred = l->tail;
  72.     x->next = NULL;
  73.     l->tail->next = x;
  74.     l->tail = x;
  75. }
  76.  
  77.  
  78.  
  79. void delKey(list *l, int k){
  80.     link x = l->head;
  81.     link y;
  82.     if(x == NULL)
  83.         return;
  84.  
  85.     for( ; x != NULL ; y = x, x = x->next){
  86.         if(x->key == k){
  87.             if(x == l->head){
  88.                 l->head = l->head->next;
  89.                 free(x);
  90.                 return;
  91.             }
  92.             if(x == l->tail){
  93.                 l->tail = l->tail->pred;
  94.                 free(x);
  95.                 return;
  96.             }
  97.             y->next = x->next;
  98.             x->next->pred = x->pred;
  99.             free(x);
  100.             return;
  101.         }
  102.     }
  103.  
  104. }
  105.  
  106. void printList(FILE *fp, list *l){
  107.     link x = l->head;
  108.     if(x == NULL)
  109.         return;
  110.     for( ; x != NULL ; x = x->next)
  111.         printf(" %d ", x->key);
  112.     printf("\n");
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement