Don't like ads? PRO users don't see any ads ;-)

MATRIZESPARSALINHA

By: PedroHMM on Apr 29th, 2012  |  syntax: C++  |  size: 1.32 KB  |  hits: 37  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <malloc.h>
  5.  
  6. typedef struct estrutura
  7. {
  8.     int valor;
  9.     int col;
  10.     int lin;
  11.     estrutura *prox;    
  12. }NO;
  13.  
  14. void inicializar(NO** inicio)
  15. {
  16.      *inicio =NULL;
  17. }
  18.  
  19. bool inserir (NO** inicio, int col, int lin, int valor)
  20. {
  21.      NO* novo =(NO*) malloc(sizeof(NO));
  22.      novo->valor = valor;
  23.      novo->col = col;
  24.      novo->lin = lin;
  25.      
  26.      if(*inicio == NULL)
  27.      {
  28.                 novo->prox = *inicio;
  29.                 *inicio = novo;
  30.                 return true;
  31.      }
  32.      
  33.      NO* p = *inicio;
  34.      while(p->lin <= lin && p->col < col && p->prox != NULL)
  35.      {
  36.                   p = p->prox;
  37.      }
  38.      if(p == *inicio)
  39.      {
  40.           novo->prox = *inicio;
  41.           *inicio = novo;
  42.           return true;  
  43.      }
  44.      
  45.      novo->prox = p;
  46.      p->prox = novo;
  47.      return true;
  48.      
  49. }
  50.  
  51.  
  52. void imprimir(NO**inicio)
  53. {
  54.      NO* p = *inicio;
  55.      while(p)
  56.      {
  57.              printf("%i\t", p->valor);
  58.              p = p->prox;
  59.      }
  60. }
  61.  
  62. main()
  63. {
  64.       NO* inicio;
  65.       inicializar(&inicio);
  66.       inserir(&inicio, 6,5, 15);
  67.       inserir(&inicio, 4,5, 10);
  68.       inserir(&inicio, 1,1, 5);
  69.       inserir(&inicio, 1,2, 6); //==> aqui tá o problema
  70.       imprimir(&inicio);
  71.       getch();
  72. }