C code Linked list - code stops after first list insertion

Question: What is causing this code to close itself after the first insertion?

Repl link/Link to where the bug appears: https://replit.com/@theusf40/Atividade-Lista-Encadeada#questoes/q1.c

Screenshots, links, or other helpful context: I’m doing some Linked List code challenges in C, and it seemed to work as expected when coded into a single file, but as I tried to break down my code to be more readable, it stops working after the first insertion in the list, simply stopping the program with no more output. I opened the broken down code inside another tool, like Online GDB, and it worked with no issues.

The console doesn’t even allow me to insert the first element of the list, for some reason. Here’s the code that I’m trying to make work, it’s in Portuguese, but I hope it won’t be an issue to debug it in. To help, I’ve translated the functions called inside them.


/** The content of this file is called inside main.c
* Didn't include main, as it only calls this function through a "questions.h"
#include <stdio.h>
#include <stdlib.h>
#include "../listaEncadeada.h"

void q1() {
    int resp, resp2 = 1;
    printf("Lista Simplesmente Encadeada \n");

    celula *topo, *buscado, *topoPar, *topoImpar;

    for (int j = 0; j < 4; j++) {
      printf("Digite o próximo elemento da lista: ");
      scanf("%d", &resp);
      topo = insertAtEnd(resp, topo);

      //This part is not even called, since the program stops at the first insertion in the above code
    printf("Fim da Inserção \n");

    while (resp2) {

      celula *aux = topo;

      while (aux != NULL) {
        // Adiciona aux no topoPar se aux->conteudo for par
        if (aux->conteudo % 2 == 0) topoPar = insertAtEnd(aux->conteudo, topoPar);
        else topoImpar = insertAtEnd(aux->conteudo, topoImpar);

        // Passa pro próximo elemento
        aux = aux->prox;

      printf("Lista original:\n");
      printf("\nLista de pares:\n");
      printf("\nLista de impares:\n");

      printf("Deseja repetir a busca? 1 - Sim, 0 - Não\n");
      scanf("%d", &resp2);


#include <stdio.h>
#include <stdlib.h>
#include "listaEncadeada.h"

celula *insertAtEnd(int x, celula *p) {
  celula *novo, *topo;
  topo = p;
  novo = malloc(sizeof(celula));
  novo->conteudo = x;
  novo->prox = NULL;
  if (p != NULL) {
    for (p; p->prox != NULL; p = p->prox)
    p->prox = novo;
  } else
    topo = novo;
  return topo;

celula *search(int x, celula *lista) {
  celula *aux = lista;
  while (aux != NULL && aux->conteudo != x)
    aux = aux->prox;
  return aux;

celula *recursiveSearch(int x, celula *lista) {
  if (lista == NULL)
    return NULL;
  if (lista->conteudo != x)
    return buscarRecursivo(x, lista->prox);
    return lista;

void remove(celula *p) {
  celula *lixo;
  lixo = p->prox;
  p->prox = lixo->prox;

celula *searchAndRemove(int x, celula *listaEncadeada) {
  celula *p, *q;
  p = listaEncadeada;
  if (p->conteudo == x) {
    q = listaEncadeada;
    p = listaEncadeada->prox;
    return p;
  } else {
    q = listaEncadeada->prox;
    while (q != NULL && q->conteudo != x) {
      p = q;
      q = q->prox;
    if (q != NULL) {
      p->prox = q->prox;
    return listaEncadeada;

celula *insertnBeginning(int x, celula *p) {
  celula *nova;
  nova = malloc(sizeof(celula));
  nova->conteudo = x;
  nova->prox = NULL;
  if (p != NULL)
    nova->prox = p;
  p = nova;
  return p;

void printList(celula *listaEncadeada) {
  celula *p;
  for (p = listaEncadeada; p != NULL; p = p->prox)
    printf("%d\n", p->conteudo);



//Boilerplate da lista encadeada
typedef struct NODO {
  int conteudo;
  struct NODO *prox;
} celula;

celula *insertAtEnd(int x, celula *p);
celula *search(int x, celula *lista);
celula *RecursiveSearch(int x, celula *lista);
void remove(celula *p);
celula *searchAndRemover(int x, celula *listaEncadeada);
celula *insertAtBeginning(int x, celula *p);
void printList(celula *listaEncadeada);


If you could potentially translate your code from Portuguese to English, I’m sure a lot more people would be able to help.

Shouldn’t this list:

celula *topo, *buscado, *topoPar, *topoImpar;

Be someting like this?

celula *topo = NULL, *buscado = NULL, *topoPar = NULL, *topoImpar = NULL;

To ensure that the list starts empty and that the insertAtEnd function can properly handle the insertion of the first element?

1 Like