Advertisement
ProToTN

Linked List

Feb 26th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "header.h"
  5.  
  6. typedef struct element
  7. {
  8.     int x;
  9.     struct element* nxt;
  10. }element;
  11. typedef element* llist;
  12.  
  13.  
  14.  
  15.  
  16. void head(llist *liste, int i)
  17. {
  18.     llist petit = malloc(sizeof(element));
  19.     petit->x = i;
  20.     petit->nxt = *liste;
  21.     *liste = petit;
  22. }
  23.  
  24.  
  25. void print(llist liste)
  26. {
  27.     llist temp = liste;
  28.     while(!temp == NULL)
  29.     {
  30.         printf("%d ",temp->x);
  31.         temp = temp->nxt;
  32.     }
  33. }
  34.  
  35.  
  36. void tail(llist liste, int i)
  37. {
  38.     llist petit = malloc(sizeof(element));
  39.     llist temp = liste;
  40.     while(!temp->nxt == NULL)
  41.     {
  42.         temp = temp->nxt;
  43.     }
  44.     petit->x = i;
  45.     temp->nxt = petit;
  46.     petit->nxt = NULL;
  47. }
  48.  
  49.  
  50.  
  51. void removeI(llist *liste, int j)
  52. {
  53.     llist temp = *liste;
  54.     llist temp2 = temp;
  55.     int y = 0;
  56.     if(liste == NULL) *liste = NULL;
  57.     else if((*liste)->nxt == NULL)
  58.     {
  59.         if((*liste)->x == j)
  60.         {
  61.             free(temp);
  62.             *liste = NULL;
  63.         }
  64.     }
  65.     else if((*liste)->x == j)
  66.     {
  67.         *liste = (*liste)->nxt;
  68.         free(temp);
  69.         removeI(liste, j);
  70.     }
  71.     else
  72.     {
  73.         while((temp != NULL)&&(y == 0))
  74.         {
  75.             if(temp->x != j)
  76.             {
  77.                 temp2 = temp;
  78.                 temp = temp->nxt;
  79.             }
  80.             else y=1;
  81.         }
  82.         if(y == 1)
  83.         {
  84.             temp2->nxt = temp->nxt;
  85.             free(temp);
  86.             removeI(liste, j);
  87.         }
  88.     }
  89. }
  90.  
  91.  
  92. void main()
  93. {
  94.     llist liste = NULL;
  95.     int i;
  96.     for(i=0; i<=10; i++)
  97.     {
  98.         head(&liste, i);
  99.     }
  100.     for(i=0; i<=10; i++)
  101.     {
  102.         tail(liste, i);
  103.     }
  104.     removeI(liste, 10);
  105.     print(liste);
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement