Advertisement
eduardovp97

ListaCircular.c

Dec 2nd, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "listaC.h"
  5.  
  6. void crearLista(TListaC *lc){
  7.     lc->first = NULL;
  8.     lc->elem = 0;
  9.     lc->last = NULL;
  10. }
  11.  
  12. void insertar(TListaC *lc, TInfo x){
  13.     TNode *node = malloc(sizeof(TNode));
  14.     strcpy(node->info,x);
  15.  
  16.     if(lc->elem == 0){
  17.         lc->first = node;
  18.         lc->last = node;
  19.         node->next = node;
  20.     }else{
  21.         node->next = lc->first;
  22.         lc->last->next = node;
  23.         lc->last = node;
  24.     }
  25.  
  26.     lc->elem++;
  27. }
  28.  
  29. void printList(TListaC *lc){
  30.     TNode *ptr = lc->first;
  31.     int i;
  32.     while(ptr != lc->last){
  33.         for(i=0; i<strlen(ptr->info); i++)
  34.             printf("%c",ptr->info[i]);
  35.         printf("\n");
  36.         ptr = ptr->next;
  37.     }
  38.     for(i=0; i<strlen(ptr->info); i++)
  39.         printf("%c",ptr->info[i]);
  40.     printf("\n");
  41.  
  42. }
  43.  
  44. void mueranTodos(TListaC *lc, TInfo x, int k){
  45.     int count = 0;
  46.     TNode *ptr = lc->first;
  47.     TNode *aux;
  48.     if(lc->elem == 0)
  49.         return;
  50.  
  51.     while(strcmp(ptr->info,x) != 0)
  52.         ptr = ptr->next;
  53.    
  54.     int i;
  55.     int n = lc->elem;
  56.     for(count = 0; count < n-1; count++){
  57.        
  58.         for(i=0; i<k-1; i++)
  59.             ptr = ptr->next;
  60.  
  61.         if(ptr->next == lc->first)
  62.             lc->first = ptr;
  63.         if(ptr->next == lc->last)
  64.             lc->last = ptr;
  65.  
  66.         ptr->next = ptr->next->next;
  67.  
  68.         lc->elem--;
  69.  
  70.         if(lc->elem != 1)
  71.             ptr = ptr->next;
  72.     }
  73.  
  74.     for(int i=0; i<strlen(ptr->info); i++)
  75.         printf("%c",ptr->info[i]);
  76.     printf("\n");
  77.    
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement