title: Topologic graph algorithm with queue structure does not return the right order
body: <p>After a few hours of trying to debug this, I'm coming here looking for kind souls who'd happen to be willing to dive into my issue. I'm making a program that returns/prints the topological order of a graph, but it won't print the right order. I might have an issue when deleting nodes from the predecessors list. </p>
  
  <p>My graph has 6 nodes (0 to 5) and 5 arcs as follows:</p>
  
  <pre><code>5 -&gt; 1
  5 -&gt; 0
  3 -&gt; 5
  4 -&gt; 3
  2 -&gt; 3
  </code></pre>
  
  <p>And I get by initializing the graph the following successors and predecessors queues:</p>
  
  <pre><code>Successors of 0: EMPTY
  Successors of 1: EMPTY
  Successors of 2: 3
  Successors of 3: 5
  Successors of 4: 3
  Successors of 5: 1 0
  Predecessors of 0: 5
  Predecessors of 1: 5
  Predecessors of 2: EMPTY
  Predecessors of 3: 4 2
  Predecessors of 4: EMPTY
  Predecessors of 5: 3
  </code></pre>
  
  <p>These are initialized by the loadingGraph() function (which works fine since it returns the right result without issue).</p>
  
  <p>Here's the graph structure I am using. I do malloc and all accordingly, of course. </p>
  
  <pre><code>typedef struct {
     int content;
     struct cell* next;
  } cell;
  
  typedef struct {
     cell* start;
     cell* end;
  } queue;
  
  typedef struct {
     int numberNode;
     int numberArc;
  
     queue *successors;
     queue *predecessors;
  
  } graph;
  </code></pre>
  
  <p>And this is the main program:</p>
  
  <pre><code>int main() {
      int i, j, k;
      int loopTime = 0;
  
      queue *nodeTopologicalOrder = malloc(sizeof (queue));
      graph *g = malloc(sizeof (graph));
      int *done = calloc(g-&gt;numberNode, sizeof (int));
  
      loadingGraph(g);
  
      while (loopTime &lt; 2) {
  
          for (i = 0; i &lt; g-&gt;numberNode; i++) {
              // If the node i does not have predecessors then
              if (((g-&gt;predecessors[i]).start == NULL) &amp;&amp; (done[i] == 0)) {
                  //  It is deleted from the graph and from the nodes having i as predecessor in their predecessors queues
                  for (j = 0; j &lt; g-&gt;numberNode; j++) {         
                      deleteElement(&amp;(g-&gt;predecessors[j]), i);
                  }
  
                  // The node i is added to the topological queue and won't be re-processed.
                  done[i] = 1;
                  add(i, nodeTopologicalOrder);
              }
          }
          loopTime++;    
      }
      printf("\nTopological Order :");
      display(*nodeTopologicalOrder);
  
      return 0;
  }
  </code></pre>
  
  <p>Nevertheless, I get the following result, which is erroned (should be 2 4 3 5 0 1):</p>
  
  <pre><code>Topological Order :2 3 4 5 0 1
  </code></pre>
  
  <p>It seems my issue is in my deleteElement() function but when I test it, it is just fine! It deletes the element if there's a matching one in the queue, else it returns the queue as it is. But when I use it in the main function, it seems like it deletes the whole queue, or maybe is there an affectation issue, but I can't grasp it. Here are the other function I am using (Sorry for so much code):</p>
  
  <pre><code>void add(int a, queue* pQ) {
     cell* new = malloc(sizeof(cell));
  
     (*new).content = a;
     (*new).next = NULL;
  
     if (empty(*pQ)){
        pQ-&gt;start = new;
     } else {
        (pQ-&gt;end)-&gt;next = new;
     }
     pQ-&gt;end = new;
  }
  
  int pop_first_cell(queue* pQ) {
     int deleted_queue_content = -1;
  
     if (pQ == NULL){
        exit(EXIT_SUCCESS);
     } 
     if (pQ-&gt;start != NULL) {
        cell* temp = pQ-&gt;start;
        deleted_queue_content = temp-&gt;content;
        pQ-&gt;start = temp-&gt;next;
        free(temp);
     }
     return(deleted_queue_content);
  }
  
  void deleteElement(queue* pQ, int element) {
  
      int contentTemp;
      queue *queueTemp = malloc(sizeof (queue));
      *queueTemp = create();
  
      while (contentTemp != -1) {
          contentTemp = pop_first_cell(pQ);
  
          if (contentTemp != element &amp;&amp; contentTemp != -1) {
              add(contentTemp, queueTemp);
          }
      }
  
      *pQ = *queueTemp;
  }
  </code></pre>
  
  <p>I'd really be grateful if someone could help, I know it's a lot of code, but when I don't give it fully, people usually say that some code is missing... But I'm scared that too much code would just scare you guys away ^^'</p>
  
  <p>Thanks so much!</p>