eduardovp97

list.c

Nov 14th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "list.h"
  4.  
  5. void initList(TList* l){
  6.     l->start = NULL;
  7.     l->end = NULL;
  8.     l->nElem = 0;
  9. }
  10.  
  11. void finalizeList(TList* l){
  12.     TNode *n = l->start;
  13.     TNode *aux;
  14.     while(n != NULL){
  15.         aux = n;
  16.         n = n->next;
  17.         free(aux);
  18.     }
  19. }
  20.  
  21. void insertList(TList* l, TInfo x){
  22.     TNode *n = malloc(sizeof(TNode));
  23.     n->value = x;
  24.     n->next = NULL;
  25.     if(isEmptyList(l)){
  26.         l->start = n;
  27.         l->end = n;
  28.     }else{
  29.         l->end->next = n;
  30.         l->end = n;
  31.     }
  32.     l->nElem++;
  33. }
  34.  
  35. void deleteList(TList *l, TInfo x){
  36.     TNode *n = l->start;
  37.     TNode *p;
  38.     if(!isEmptyList(l)){
  39.         if(n->value == x){
  40.             p = n;
  41.             n = n->next;
  42.             free(p);
  43.             l->start = n;
  44.             l->nElem--;
  45.         }else{
  46.             while(n->next !=NULL && n->value != x){
  47.                 p = n;
  48.                 n = n->next;
  49.             }
  50.             if(n->value == x){
  51.                 if(n->next == NULL)
  52.                     l->end = p;
  53.                 p->next = n->next;
  54.                 free(n);
  55.                 l->nElem--;
  56.             }
  57.         }
  58.     }
  59. }
  60.  
  61. void printList(TList *l){
  62.     TNode *n = l->start;
  63.     while(n != NULL){
  64.         printf("%d ",n->value);
  65.         n = n->next;
  66.     }
  67.     printf("\n");
  68. }
  69.  
  70. int isEmptyList(TList *l){
  71.     if(l->start == NULL)
  72.         return 1;
  73.     return 0;
  74. }
  75.  
  76. int sizeList(TList *l){
  77.     return l->nElem;
  78. }
  79.  
  80. int memberList(TList* l, TInfo x){
  81.     TNode *n = l->start;
  82.     while(n != NULL){
  83.         if(n->value == x)
  84.             return 1;
  85.         n = n->next;
  86.     }
  87.     return 0;
  88. }
Add Comment
Please, Sign In to add comment