title: Searching in a linked list
body: <p>First I want to thank all of you that will answer this problem.</p>
  
  <p><strong>the problems are on edit</strong></p>
  
  <p>The problem is this, I have to create a C program that generates a maze from a .txt file, inside the .txt file we have the number of rows and columns and the labyrinth scheme (I will append a file as a reference here).</p>
  
  <p>20  // number of rows</p>
  
  <p>20  // number of columns</p>
  
  <p>labyrinth scheme <strong>that can change</strong></p>
  
  <pre><code>XXXXXXXXXXXXXXXXXXXX
  X     X    X       X
  X XXXXX XXXX XXX XXX
  X       X      X X X
  X XXXXXXXXXXXX X X X
  X X   X        X X X
  X XXX XXXXXX XXXXX X
  X XXX    X X X     X
  X    XXX       XXXXX
  X XXX   XXXXXX     X
  X   XXX X X    X X X
  XXX XXX X X XXXX X X
  X     X X   XX X X X
  XXXXX     XXXX X XXX
  X     X XXX    X   X
  X XXXXX X XXXX XXX X
  X X     X  X X     X
  X X XXXXXX X XXXXX X
  X X                X
  XXXXXXXXXXXXXXXXXX X
  </code></pre>
  
  <p>The X's are walls.</p>
  
  <p>The player is * and <strong>it starts in position (1,1) and the exit is always in (18,19)</strong></p>
  
  <p>Inside the code there are 3 structures:</p>
  
  <ol>
  <li>Position represents a position inside the labyrinth grid</li>
  <li>The cell is a structure that describes which character is present in
  position pos of the grid. The structure makes available the visited
  Boolean variable useful when searching for the exit path from the
  maze, the variable expresses the fact that the search has already
  visited or not that particular position of the grid.</li>
  <li>The node structure must be used to create the linked list that
  contains a viable road to exit the maze.</li>
  </ol>
  
  <pre class="lang-c prettyprint-override"><code>typedef struct{
      int x, y;
  } position;
  
  typedef struct{     
      pos;
      char ch;
      bool visited;
  } cell;
  
  typedef struct {
      position pos;
      struct node *next;
  } node;
  </code></pre>
  
  <p>This is the function where I found some problems.</p>
  
  <pre><code>node *search(cell *grid, position pos, position goal, int rows, int columns)
  </code></pre>
  
  <p>This function has to search recursively a viable road to the exit that is in (18,19) using also a function <code>bool isEmpty</code> to see if a node is empty</p>
  
  <pre class="lang-c prettyprint-override"><code>bool isEmpty(node *list) {
      if(list -&gt;  next== NULL) {
          return true;
      } else {
          return false;
      }
  }
  </code></pre>
  
  <p>I thought that this function may be like this</p>
  
  <pre><code>node *search(cell *grid, position pos, position goal, int rows, int columns){
   node* new_node = (node)malloc(sizeof(node));
  
   //if the last element equals goal position the search finishes
   if (new_node-&gt;pos.x == goal.x &amp;&amp; new_node-&gt;pos.y == goal.y ) {
      return new_node;
   } else {
      return  *search(cell *grid, position pos, position goal, int rows, int columns);
   }
  }
  </code></pre>
  
  <p>But when I run the program it crashes. How can I fix this search?</p>
  
  <p><strong>[EDIT]</strong></p>
  
  <p>First of all i cant change the structures because is a project specification.</p>
  
  <p>The function</p>
  
  <pre><code>node *search(cell *grid, position pos, position goal, int rows, int columns)
  </code></pre>
  
  <p>has to a recursive algorithm to identify the road to the cell (18,19) //the exit
  This funtion has to return a linked list of positions that the player (*) must visit in order to exit. The return value (which will be suitably saved in the main) will therefore be the head node of this linked list this funtion uses 2 more funtions:</p>
  
  <p>1)</p>
  
  <pre><code>bool isEmpty(node∗ list) 
  </code></pre>
  
  <p>this funtion returns true if the list is empty otherwise false</p>
  
  <p>2)</p>
  
  <pre><code> node∗ push_list (node∗ list , position pos) 
  </code></pre>
  
  <p>this is a classic push function for the managment of the list. It adds in the head of the list a new node that contains the position pos</p>
  
  <p>Down here there is the code that i have made</p>
  
  <pre><code>node *search(cell *grid, position pos, position goal, int rows, int columns)
  {
  
  //base case
      if(pos.x == goal.x &amp;&amp; pos.y == goal.y)
      {
          //dont know how to return the position cause im in a multidimentional array
  
      }
  
  }
  
  
     bool isEmpty(node *list)
  {
  
      if(list -&gt;  next== NULL)
      {
          return true;
  
      }else{
  
          return false;
      }
  
  }
  </code></pre>
  
  <p>this is how i save the maze </p>
  
  <pre><code>cell *read_grid(char *filename, int *rows, int *columns)
  </code></pre>
  
  <p>{</p>
  
  <pre><code>int i,j;
  
  //steam file
  FILE *fp;
  
  
  
  
  fp = fopen("grid.txt", "r+");
  
  
  fscanf(fp, "%d\n%d\n", rows, columns);
  fprintf(stdout, "%d\n%d\n", *rows, *columns);
  
  cell grid[*rows][*columns];
  cell *temp = (cell*)malloc(sizeof(cell));
  
  assert(*grid != NULL);
  
  if(fp == NULL)
  {
  
      printf("error\n");
      exit(EXIT_FAILURE);
  
  }
  
  
  
  
  for(i = 0; i &lt; *rows; i++)
  {
  
      for(j = 0; j &lt; *columns; j++)
      {
          fscanf(fp, "%c", &amp;(grid[i][j]).ch);
          grid[i][j].pos.x =i;
          grid[i][j].pos.y =j;
  
      }
      fscanf(fp, "\n");
  }
  
  
  
  
  fclose(fp);
  
  temp = *grid;
  return temp;
  </code></pre>
  
  <p>}</p>