Building BFS with an implemented queue in C [on hold]












0












$begingroup$


I'm implementing a graph traversal breadth-first search that I found here. However, their implementation involves integers and without any linked list. I was playing around with it a little bit I have no luck in getting any results because it doesn't seem to work as intended.



This is the code that I currently have:



(main.c)



int main(void)
{
t_graph *graph;
graph = create_graph(6);
add_edge(graph, 0, 1);
add_edge(graph, 0, 2);
add_edge(graph, 2, 4);
bfs(graph, 0);
return (0);
}


(bfs.c)



typedef struct      s_list
{
struct s_list *next;
void *content;
size_t content_size;
} t_list;


typedef struct s_queue
{
t_list *first;
t_list *last;
} t_queue;

t_queue *init_queue(void);
void enqueue(t_queue *queue, void *content);
void *dequeue(t_queue *queue);
void *peek_queue(t_queue *queue);
int is_empty(t_queue *queue);
void my_print_queue(t_queue *queue);

void my_print_queue(t_queue *queue)
{
if (is_empty(queue))
my_putstr("Empty queuen");
else
{
while (!is_empty(queue))
{
my_putstr((char*)queue->first->content);
my_putstr(" ");
dequeue(queue);
}
}
}

t_queue *init_queue(void)
{
t_queue *node;
node = (t_queue *)my_memalloc(sizeof(t_queue));
node->first = NULL;
node->last = NULL;
return (node);
}

void enqueue(t_queue *queue, void *content)
{
t_list *node;
node = (t_list *)my_memalloc(sizeof(t_list));
node->content = content;
node->next = NULL;
if (!queue->last)
{
queue->last = node;
queue->first = node;
}
else
{
queue->last->next = node;
queue->last = queue->last->next;
}
return ;
}

void *dequeue(t_queue *queue)
{
t_list *tmp;

tmp = queue->first;
if (!tmp)
return (NULL);
else
{
queue->first = tmp->next;
return (tmp->content);
}

}

void *peek_queue(t_queue *queue)
{
if (queue->first == NULL)
return (NULL);
return (queue->first->content);
}

int is_empty(t_queue *queue)
{
return (queue->first == NULL);
}

t_node *create_node(int val)
{
t_node *new_node;

new_node = (t_node*my_memalloc(sizeof(t_node));
new_node->vertex = val;
new_node->next = NULL;
return (new_node);
}

t_graph *create_graph(int vertices)
{
int i;
t_graph *graph;

i = 0;
graph = my_memalloc(sizeof(t_graph));
graph->total_vertices = vertices;
printf("graph->total_vertices: %dn", vertices);
graph->adj_lists = (t_node**)my_memalloc(sizeof(t_node));
graph->visited = my_memalloc(sizeof(int) * vertices);
while (i < vertices)
{
graph->adj_lists[i] = NULL;
graph->visited[i] = 0;
i++;
}
return (graph);
}

void add_edge(t_graph *graph, int src, int dst)
{
t_node *new_node;

new_node = create_node(dst);
new_node->next = graph->adj_lists[src];
graph->adj_lists[src] = new_node;

new_node = create_node(src);
new_node->next = graph->adj_lists[dst];
graph->adj_lists[dst] = new_node;
}

void bfs(t_graph *graph, int start_vertex)
{
t_queue *queue;

queue = init_queue();
graph->visited[start_vertex] = 1;
enqueue(queue, &start_vertex);
while (!is_empty(queue))
{
my_print_queue(queue);
int current_vertex;
t_node *tmp;

current_vertex = (int)dequeue(queue);
printf("Visited %dn", current_vertex);
tmp = graph->adj_lists[current_vertex];
while (tmp)
{
int adj_vertex;

adj_vertex = tmp->vertex;
if (graph->visited[adj_vertex] == 0)
{
graph->visited[adj_vertex] = 1;
enqueue(queue, &adj_vertex);
}
tmp = tmp->next;
}
}
}


I did some research like type-casting a void pointer to make it into a char or int, or any other data type. What happens is that the create graph does it's creation after calling it; but, when it comes to the bfs, it doesn't show the correct output after. I'm getting a result of



Visited: 0


The expected output should be something like this:



Queue contains
0 Resetting queueVisited 0

Queue contains
2 1 Visited 2

Queue contains
1 4 Visited 1

Queue contains
4 Resetting queueVisited 4


While posting this, I will keep debugging on my side and see what it does with couple printf statements.










share|improve this question







New contributor




Zeid Tisnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



put on hold as off-topic by Jamal 59 mins ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Jamal

If this question can be reworded to fit the rules in the help center, please edit the question.





















    0












    $begingroup$


    I'm implementing a graph traversal breadth-first search that I found here. However, their implementation involves integers and without any linked list. I was playing around with it a little bit I have no luck in getting any results because it doesn't seem to work as intended.



    This is the code that I currently have:



    (main.c)



    int main(void)
    {
    t_graph *graph;
    graph = create_graph(6);
    add_edge(graph, 0, 1);
    add_edge(graph, 0, 2);
    add_edge(graph, 2, 4);
    bfs(graph, 0);
    return (0);
    }


    (bfs.c)



    typedef struct      s_list
    {
    struct s_list *next;
    void *content;
    size_t content_size;
    } t_list;


    typedef struct s_queue
    {
    t_list *first;
    t_list *last;
    } t_queue;

    t_queue *init_queue(void);
    void enqueue(t_queue *queue, void *content);
    void *dequeue(t_queue *queue);
    void *peek_queue(t_queue *queue);
    int is_empty(t_queue *queue);
    void my_print_queue(t_queue *queue);

    void my_print_queue(t_queue *queue)
    {
    if (is_empty(queue))
    my_putstr("Empty queuen");
    else
    {
    while (!is_empty(queue))
    {
    my_putstr((char*)queue->first->content);
    my_putstr(" ");
    dequeue(queue);
    }
    }
    }

    t_queue *init_queue(void)
    {
    t_queue *node;
    node = (t_queue *)my_memalloc(sizeof(t_queue));
    node->first = NULL;
    node->last = NULL;
    return (node);
    }

    void enqueue(t_queue *queue, void *content)
    {
    t_list *node;
    node = (t_list *)my_memalloc(sizeof(t_list));
    node->content = content;
    node->next = NULL;
    if (!queue->last)
    {
    queue->last = node;
    queue->first = node;
    }
    else
    {
    queue->last->next = node;
    queue->last = queue->last->next;
    }
    return ;
    }

    void *dequeue(t_queue *queue)
    {
    t_list *tmp;

    tmp = queue->first;
    if (!tmp)
    return (NULL);
    else
    {
    queue->first = tmp->next;
    return (tmp->content);
    }

    }

    void *peek_queue(t_queue *queue)
    {
    if (queue->first == NULL)
    return (NULL);
    return (queue->first->content);
    }

    int is_empty(t_queue *queue)
    {
    return (queue->first == NULL);
    }

    t_node *create_node(int val)
    {
    t_node *new_node;

    new_node = (t_node*my_memalloc(sizeof(t_node));
    new_node->vertex = val;
    new_node->next = NULL;
    return (new_node);
    }

    t_graph *create_graph(int vertices)
    {
    int i;
    t_graph *graph;

    i = 0;
    graph = my_memalloc(sizeof(t_graph));
    graph->total_vertices = vertices;
    printf("graph->total_vertices: %dn", vertices);
    graph->adj_lists = (t_node**)my_memalloc(sizeof(t_node));
    graph->visited = my_memalloc(sizeof(int) * vertices);
    while (i < vertices)
    {
    graph->adj_lists[i] = NULL;
    graph->visited[i] = 0;
    i++;
    }
    return (graph);
    }

    void add_edge(t_graph *graph, int src, int dst)
    {
    t_node *new_node;

    new_node = create_node(dst);
    new_node->next = graph->adj_lists[src];
    graph->adj_lists[src] = new_node;

    new_node = create_node(src);
    new_node->next = graph->adj_lists[dst];
    graph->adj_lists[dst] = new_node;
    }

    void bfs(t_graph *graph, int start_vertex)
    {
    t_queue *queue;

    queue = init_queue();
    graph->visited[start_vertex] = 1;
    enqueue(queue, &start_vertex);
    while (!is_empty(queue))
    {
    my_print_queue(queue);
    int current_vertex;
    t_node *tmp;

    current_vertex = (int)dequeue(queue);
    printf("Visited %dn", current_vertex);
    tmp = graph->adj_lists[current_vertex];
    while (tmp)
    {
    int adj_vertex;

    adj_vertex = tmp->vertex;
    if (graph->visited[adj_vertex] == 0)
    {
    graph->visited[adj_vertex] = 1;
    enqueue(queue, &adj_vertex);
    }
    tmp = tmp->next;
    }
    }
    }


    I did some research like type-casting a void pointer to make it into a char or int, or any other data type. What happens is that the create graph does it's creation after calling it; but, when it comes to the bfs, it doesn't show the correct output after. I'm getting a result of



    Visited: 0


    The expected output should be something like this:



    Queue contains
    0 Resetting queueVisited 0

    Queue contains
    2 1 Visited 2

    Queue contains
    1 4 Visited 1

    Queue contains
    4 Resetting queueVisited 4


    While posting this, I will keep debugging on my side and see what it does with couple printf statements.










    share|improve this question







    New contributor




    Zeid Tisnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$



    put on hold as off-topic by Jamal 59 mins ago


    This question appears to be off-topic. The users who voted to close gave this specific reason:


    • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Jamal

    If this question can be reworded to fit the rules in the help center, please edit the question.



















      0












      0








      0





      $begingroup$


      I'm implementing a graph traversal breadth-first search that I found here. However, their implementation involves integers and without any linked list. I was playing around with it a little bit I have no luck in getting any results because it doesn't seem to work as intended.



      This is the code that I currently have:



      (main.c)



      int main(void)
      {
      t_graph *graph;
      graph = create_graph(6);
      add_edge(graph, 0, 1);
      add_edge(graph, 0, 2);
      add_edge(graph, 2, 4);
      bfs(graph, 0);
      return (0);
      }


      (bfs.c)



      typedef struct      s_list
      {
      struct s_list *next;
      void *content;
      size_t content_size;
      } t_list;


      typedef struct s_queue
      {
      t_list *first;
      t_list *last;
      } t_queue;

      t_queue *init_queue(void);
      void enqueue(t_queue *queue, void *content);
      void *dequeue(t_queue *queue);
      void *peek_queue(t_queue *queue);
      int is_empty(t_queue *queue);
      void my_print_queue(t_queue *queue);

      void my_print_queue(t_queue *queue)
      {
      if (is_empty(queue))
      my_putstr("Empty queuen");
      else
      {
      while (!is_empty(queue))
      {
      my_putstr((char*)queue->first->content);
      my_putstr(" ");
      dequeue(queue);
      }
      }
      }

      t_queue *init_queue(void)
      {
      t_queue *node;
      node = (t_queue *)my_memalloc(sizeof(t_queue));
      node->first = NULL;
      node->last = NULL;
      return (node);
      }

      void enqueue(t_queue *queue, void *content)
      {
      t_list *node;
      node = (t_list *)my_memalloc(sizeof(t_list));
      node->content = content;
      node->next = NULL;
      if (!queue->last)
      {
      queue->last = node;
      queue->first = node;
      }
      else
      {
      queue->last->next = node;
      queue->last = queue->last->next;
      }
      return ;
      }

      void *dequeue(t_queue *queue)
      {
      t_list *tmp;

      tmp = queue->first;
      if (!tmp)
      return (NULL);
      else
      {
      queue->first = tmp->next;
      return (tmp->content);
      }

      }

      void *peek_queue(t_queue *queue)
      {
      if (queue->first == NULL)
      return (NULL);
      return (queue->first->content);
      }

      int is_empty(t_queue *queue)
      {
      return (queue->first == NULL);
      }

      t_node *create_node(int val)
      {
      t_node *new_node;

      new_node = (t_node*my_memalloc(sizeof(t_node));
      new_node->vertex = val;
      new_node->next = NULL;
      return (new_node);
      }

      t_graph *create_graph(int vertices)
      {
      int i;
      t_graph *graph;

      i = 0;
      graph = my_memalloc(sizeof(t_graph));
      graph->total_vertices = vertices;
      printf("graph->total_vertices: %dn", vertices);
      graph->adj_lists = (t_node**)my_memalloc(sizeof(t_node));
      graph->visited = my_memalloc(sizeof(int) * vertices);
      while (i < vertices)
      {
      graph->adj_lists[i] = NULL;
      graph->visited[i] = 0;
      i++;
      }
      return (graph);
      }

      void add_edge(t_graph *graph, int src, int dst)
      {
      t_node *new_node;

      new_node = create_node(dst);
      new_node->next = graph->adj_lists[src];
      graph->adj_lists[src] = new_node;

      new_node = create_node(src);
      new_node->next = graph->adj_lists[dst];
      graph->adj_lists[dst] = new_node;
      }

      void bfs(t_graph *graph, int start_vertex)
      {
      t_queue *queue;

      queue = init_queue();
      graph->visited[start_vertex] = 1;
      enqueue(queue, &start_vertex);
      while (!is_empty(queue))
      {
      my_print_queue(queue);
      int current_vertex;
      t_node *tmp;

      current_vertex = (int)dequeue(queue);
      printf("Visited %dn", current_vertex);
      tmp = graph->adj_lists[current_vertex];
      while (tmp)
      {
      int adj_vertex;

      adj_vertex = tmp->vertex;
      if (graph->visited[adj_vertex] == 0)
      {
      graph->visited[adj_vertex] = 1;
      enqueue(queue, &adj_vertex);
      }
      tmp = tmp->next;
      }
      }
      }


      I did some research like type-casting a void pointer to make it into a char or int, or any other data type. What happens is that the create graph does it's creation after calling it; but, when it comes to the bfs, it doesn't show the correct output after. I'm getting a result of



      Visited: 0


      The expected output should be something like this:



      Queue contains
      0 Resetting queueVisited 0

      Queue contains
      2 1 Visited 2

      Queue contains
      1 4 Visited 1

      Queue contains
      4 Resetting queueVisited 4


      While posting this, I will keep debugging on my side and see what it does with couple printf statements.










      share|improve this question







      New contributor




      Zeid Tisnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I'm implementing a graph traversal breadth-first search that I found here. However, their implementation involves integers and without any linked list. I was playing around with it a little bit I have no luck in getting any results because it doesn't seem to work as intended.



      This is the code that I currently have:



      (main.c)



      int main(void)
      {
      t_graph *graph;
      graph = create_graph(6);
      add_edge(graph, 0, 1);
      add_edge(graph, 0, 2);
      add_edge(graph, 2, 4);
      bfs(graph, 0);
      return (0);
      }


      (bfs.c)



      typedef struct      s_list
      {
      struct s_list *next;
      void *content;
      size_t content_size;
      } t_list;


      typedef struct s_queue
      {
      t_list *first;
      t_list *last;
      } t_queue;

      t_queue *init_queue(void);
      void enqueue(t_queue *queue, void *content);
      void *dequeue(t_queue *queue);
      void *peek_queue(t_queue *queue);
      int is_empty(t_queue *queue);
      void my_print_queue(t_queue *queue);

      void my_print_queue(t_queue *queue)
      {
      if (is_empty(queue))
      my_putstr("Empty queuen");
      else
      {
      while (!is_empty(queue))
      {
      my_putstr((char*)queue->first->content);
      my_putstr(" ");
      dequeue(queue);
      }
      }
      }

      t_queue *init_queue(void)
      {
      t_queue *node;
      node = (t_queue *)my_memalloc(sizeof(t_queue));
      node->first = NULL;
      node->last = NULL;
      return (node);
      }

      void enqueue(t_queue *queue, void *content)
      {
      t_list *node;
      node = (t_list *)my_memalloc(sizeof(t_list));
      node->content = content;
      node->next = NULL;
      if (!queue->last)
      {
      queue->last = node;
      queue->first = node;
      }
      else
      {
      queue->last->next = node;
      queue->last = queue->last->next;
      }
      return ;
      }

      void *dequeue(t_queue *queue)
      {
      t_list *tmp;

      tmp = queue->first;
      if (!tmp)
      return (NULL);
      else
      {
      queue->first = tmp->next;
      return (tmp->content);
      }

      }

      void *peek_queue(t_queue *queue)
      {
      if (queue->first == NULL)
      return (NULL);
      return (queue->first->content);
      }

      int is_empty(t_queue *queue)
      {
      return (queue->first == NULL);
      }

      t_node *create_node(int val)
      {
      t_node *new_node;

      new_node = (t_node*my_memalloc(sizeof(t_node));
      new_node->vertex = val;
      new_node->next = NULL;
      return (new_node);
      }

      t_graph *create_graph(int vertices)
      {
      int i;
      t_graph *graph;

      i = 0;
      graph = my_memalloc(sizeof(t_graph));
      graph->total_vertices = vertices;
      printf("graph->total_vertices: %dn", vertices);
      graph->adj_lists = (t_node**)my_memalloc(sizeof(t_node));
      graph->visited = my_memalloc(sizeof(int) * vertices);
      while (i < vertices)
      {
      graph->adj_lists[i] = NULL;
      graph->visited[i] = 0;
      i++;
      }
      return (graph);
      }

      void add_edge(t_graph *graph, int src, int dst)
      {
      t_node *new_node;

      new_node = create_node(dst);
      new_node->next = graph->adj_lists[src];
      graph->adj_lists[src] = new_node;

      new_node = create_node(src);
      new_node->next = graph->adj_lists[dst];
      graph->adj_lists[dst] = new_node;
      }

      void bfs(t_graph *graph, int start_vertex)
      {
      t_queue *queue;

      queue = init_queue();
      graph->visited[start_vertex] = 1;
      enqueue(queue, &start_vertex);
      while (!is_empty(queue))
      {
      my_print_queue(queue);
      int current_vertex;
      t_node *tmp;

      current_vertex = (int)dequeue(queue);
      printf("Visited %dn", current_vertex);
      tmp = graph->adj_lists[current_vertex];
      while (tmp)
      {
      int adj_vertex;

      adj_vertex = tmp->vertex;
      if (graph->visited[adj_vertex] == 0)
      {
      graph->visited[adj_vertex] = 1;
      enqueue(queue, &adj_vertex);
      }
      tmp = tmp->next;
      }
      }
      }


      I did some research like type-casting a void pointer to make it into a char or int, or any other data type. What happens is that the create graph does it's creation after calling it; but, when it comes to the bfs, it doesn't show the correct output after. I'm getting a result of



      Visited: 0


      The expected output should be something like this:



      Queue contains
      0 Resetting queueVisited 0

      Queue contains
      2 1 Visited 2

      Queue contains
      1 4 Visited 1

      Queue contains
      4 Resetting queueVisited 4


      While posting this, I will keep debugging on my side and see what it does with couple printf statements.







      beginner c gcc






      share|improve this question







      New contributor




      Zeid Tisnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Zeid Tisnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Zeid Tisnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 hour ago









      Zeid TisnesZeid Tisnes

      11




      11




      New contributor




      Zeid Tisnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Zeid Tisnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Zeid Tisnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      put on hold as off-topic by Jamal 59 mins ago


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Jamal

      If this question can be reworded to fit the rules in the help center, please edit the question.







      put on hold as off-topic by Jamal 59 mins ago


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Jamal

      If this question can be reworded to fit the rules in the help center, please edit the question.






















          0






          active

          oldest

          votes

















          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes

          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'