drout

Algoritmo de lista encadeada em C

Feb 8th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. typedef struct cel {
  2.     int info;
  3.     struct cel *pn, *ps, *pw, *pe,
  4.         *pno, *pne, *pso, *pse;
  5. } CEL;
  6.  
  7. typedef struct line {
  8.     int ncols;
  9.     CEL *pcels;
  10. } LINE;
  11.  
  12. typedef struct matrix {
  13.     int nlines;
  14.     LINE *plines;
  15. } MATRIX;
  16.  
  17. struct resultado {
  18.     int linha, coluna;
  19.     resultado *next;
  20. };
  21.  
  22. resultado * find_cells_with_value(MATRIX m, int valor)
  23. {
  24.     resultado * RESULTADO = NULL;
  25.     resultado * PROX_RESULTADO = RESULTADO;
  26.  
  27.     for (int lin = 0; lin < m.nlines; lin++)
  28.     {
  29.         LINE linha = m.plines[lin];
  30.  
  31.         for (int col = 0; col < linha.ncols; col++)
  32.             if (linha.pcels[col].info == valor)
  33.             {
  34.                 resultado *NOVO = (resultado*)malloc(sizeof(resultado));
  35.  
  36.                 NOVO->linha = lin;
  37.                 NOVO->coluna = col;
  38.                 NOVO->next = NULL;
  39.  
  40.                 if (!RESULTADO)
  41.                     RESULTADO = NOVO;
  42.                 else
  43.                     PROX_RESULTADO->next = NOVO;
  44.  
  45.                 PROX_RESULTADO = NOVO;
  46.             }
  47.     }
  48.     return RESULTADO;
  49. }
  50.    
  51.  
  52. //====================================
  53. void main(int argc, char* argv)
  54. {
  55.     int input[] = {
  56.         1, 4, 7,  8, 44,
  57.         2, 3, 0,  9,  4,
  58.         9, 5, 8, 44, 12,
  59.         2, 4, 3,  9,  8 };
  60.  
  61.     int lins = 4;
  62.     int cols = 5;
  63.  
  64.     MATRIX mat;
  65.     mat.nlines = lins;
  66.     mat.plines = (line*)malloc(lins * sizeof(line));
  67.  
  68.     for (int i = 0; i < lins; i++)
  69.     {
  70.         mat.plines[i].ncols = cols;
  71.         mat.plines[i].pcels = (cel*)malloc(cols * sizeof(cel));
  72.  
  73.         for (int j = 0; j < cols; j++)
  74.         {
  75.             CEL *c = (cel*) malloc(sizeof(cel));
  76.             c->info = input[i * cols + j];
  77.  
  78.             mat.plines[i].pcels[j] = *c;
  79.         }
  80.     }
  81.  
  82.     resultado *res = find_cells_with_value(mat, 8);
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment