sombriks

lista.c

Dec 12th, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct lista{
  5.     int info;
  6.     struct lista *prox;
  7. }Lista;
  8.  
  9. Lista *lst_cria(){
  10.     return NULL;
  11. }
  12.  
  13. Lista *lst_insere (Lista *l, int i ){
  14.     Lista *novo = (Lista*) malloc(sizeof(Lista));
  15.     novo->info = i;
  16.     novo->prox = l;
  17.     return novo;
  18. }
  19.  
  20. void lst_imprime(Lista* l){
  21.     Lista* p = l;
  22.     for (p=l; p!=NULL; p=p->prox){
  23.             printf("info = %d\n", p->info);
  24. }
  25. }
  26.  
  27. int maiores (Lista* lst, int n){
  28.     Lista* p;
  29.     int maior=0;
  30.     for (p=lst; p!=NULL;p=p->prox){
  31.         if(p->info>n){
  32.             maior++;
  33.         }
  34.     }
  35.     return maior;
  36.  }
  37.  
  38.  
  39. Lista* retira_n(Lista* lst, int n){
  40.  
  41.     Lista* p=lst;
  42.     Lista* ant=NULL;
  43.     if(p==NULL)
  44.       { return lst;}
  45.     if(ant==NULL)
  46.           {lst=p->prox;}
  47.     else
  48.     {
  49.        for (p=lst;p!=NULL;p->prox){
  50.         ant=p;
  51.         p=p->prox;
  52.         if(p->info==n);
  53.            {  ant->prox = p->prox;
  54.               p=ant;  }
  55.          }
  56.     free(p);
  57.     return lst;
  58.     }
  59. }
  60.  
  61.  
  62.  
  63. int main(void){
  64. Lista *l;
  65. Lista *m;
  66. int aux;
  67. l= lst_cria();
  68. m=lst_cria();
  69. l= lst_insere(l, 10);
  70. l= lst_insere(l, 54);
  71. l= lst_insere(l, 23);
  72. lst_imprime(l);
  73.  
  74.  
  75.  
  76. printf("\n");
  77. printf("retirei o 23\n");
  78. printf("\n");
  79.  
  80.  
  81. l=retira_n(l, 23);
  82. lst_imprime(l);
  83.  
  84. aux=maiores(l, 3);
  85. printf("maior: %i\n",aux);
  86. lst_imprime(l);
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment