Error at the time of running the program hangs.List list (Resolved) But I am still in doubt

-2

I have an error that only appears during the execution of the program. I would like someone who has the knowledge of why this happens, can you explain.

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

struct TNoDeCaractere
{
char caractere;
struct TNoDeCaractere *proximo;
};

 struct TNoDeCaractere* inicio;

void insere(char valor){
struct TNoDeCaractere *p, *np;

//Se a lista está vazia, cria um nó e faz "inicio"apontar para ele.
if(inicio == NULL){

np=malloc(sizeof(struct TNoDeCaractere));
(*np).caractere= valor;
(*np).proximo =inicio;
inicio=np;

}else{//Caso contrário, ou seja, se a lista não está vazia...
p=malloc(sizeof(struct TNoDeCaractere));
p=inicio;
//varre toda a lista,
while((*p).proximo !=inicio){
p=(*p).proximo;
}printf("Teste");
//Cria novo nó
np=malloc(sizeof(struct TNoDeCaractere));
(*np).caractere=valor;
(*np).proximo=inicio;
// e liga a lista existente ao novo nó.
(*p).proximo=np;
}
}
void imprimir(){
struct TNoDeCaractere *p;
p=inicio;
while((*p).proximo !=NULL){
   printf("Valor: %d \n",(*p).proximo);
    p=(*p).proximo;
}
}
 void remover(char valor)
{
struct TNoDeCaractere *pAnt, *p;
//Verifica se a partir do segundo nó, há nó a ser retirado.
pAnt=inicio;
p=(*inicio).proximo;
while(p!=inicio)
    {
        if((*p).caractere == valor)
        {

            (*pAnt).proximo=(*p).proximo;free(p);
            p=(*pAnt).proximo;
        }else{
            pAnt=(*pAnt).proximo;
            p=(*p).proximo;
            }
    }//Testa se a lista estácom o valor aser retirado no primeiro nó
    if((*inicio).caractere == valor)
    {

        p=(*inicio).proximo;
        free(inicio);
        inicio=p;
    }

}




int main(){

inicio=NULL;


insere("a");

insere("c");
insere("b");


imprimir();

remover('b');

imprimir();
}

I have a "printf (" Test ")" after the loop (in the function insert) which is where the program stops.

I've worked out how to make the code after a few attempts and manipulations at the pointer positions. However the reason that made me miss I do not know. I just manipulated the whole code to try to make it work. I'll now post the correct code.

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

   struct TNoDeCaractere
   {
   char caractere;
   struct TNoDeCaractere *proximo;
    };

    struct TNoDeCaractere *inicio;

    void insere(char valor){
    struct TNoDeCaractere *p, *np;
   //Se a lista está vazia, cria um nó e faz "inicio"apontar para ele.
   if(inicio == NULL){
   np=malloc(sizeof(struct TNoDeCaractere));
  (*np).caractere= valor;
  (*np).proximo =np;
  inicio=np;

  }else{//Caso contrário, ou seja, se a lista não está vazia...
  p=inicio;
  //varre toda a lista,
  while((*p).proximo !=inicio){
  p=(*p).proximo;}
 //Cria novo nó e insere no final.
 np=malloc(sizeof(struct TNoDeCaractere));
 (*np).caractere=valor;
 (*np).proximo=inicio;
 // e liga a lista existente ao novo nó.
(*p).proximo=np;
}
}
void imprimir(){
 struct TNoDeCaractere *p;
 p=inicio;
  while((*p).proximo !=inicio){
    printf("Valor: %c \n",(*p).caractere);
     p=(*p).proximo;
 }
 printf("Valor: %c \n",(*p).caractere);// Linha para imprimir ultima letra.
 }
void remover(char valor)
 {
  struct TNoDeCaractere *pAnt, *p;
   //Verifica se a partir do segundo nó, há nó a ser retirado.
  pAnt=inicio;
  p=(*inicio).proximo;
  while(p!=inicio)
    {
        if((*p).caractere == valor)
        {

            (*pAnt).proximo=(*p).proximo;free(p);
            p=(*pAnt).proximo;
        }else{
            pAnt=(*pAnt).proximo;
            p=(*p).proximo;
            }
    }//Testa se a lista estácom o valor aser retirado no primeiro nó
    if((*inicio).caractere == valor)
    {

        p=(*inicio).proximo;
        free(inicio);
        inicio=p;
       }

}




int main(){

inicio=NULL;


insere('G');insere('a');insere('b');
insere('r');insere('i');insere('e');insere('l');


imprimir();

remover('b');

imprimir();
}
    
asked by anonymous 29.10.2018 / 01:29

1 answer

0

struct TNoDeCaractere {    char character;    struct TNoDeCaractere * proximo; };

struct TNoDeCaractere * start = NULL;

void inserts (char value) {    //Good habits    // always initialize the variables that will be used    struct TNoDeCaractere * p = NULL, * np = NULL;    // If the list is empty, it creates a node and causes "start" to point to it.    if (start == NULL)    {       //Good habits       // when allocating memory always test to see if no error occurred       if ((np = (struct TNoDeCaractere *) malloc (sizeof (struct TNoDeCaractere))) == NULL)       {          printf ("Memory allocation failure \ n");       }       else       {          np-> character = value;          np-> next = np;          start = np;       }    }    else    {// Otherwise, that is, if the list is not empty ...       p = onset;       // scans the entire list,       while (p-> next! = start)       {          p = p-> gt;       }       // Creates a new node and inserts at the end.       //Good habits       // when allocating memory always test to see if no error occurred       if ((np = (struct TNoDeCaractere *) malloc (sizeof (struct TNoDeCaractere))) == NULL)       {          printf ("Memory allocation failure \ n");       }       else       {          np-> character = value;          np-> next = start;          // and binds the existing list to the new node.          p-> next = np;       }    } } void print () {    struct TNoDeCaractere * p = start;    // p = start;    while (p-> next! = start)    {       printf ("Value:% c \ n", p-> character);       p = p-> gt;    }    printf ("Value:% c \ n", p-> character); // Line to print last letter. } void remove (char value) {    struct TNoDeCaractere * pAnt = start;    struct TNoDeCaractere * p = start-> next;    // Checks if from the second node, there is a node to be removed.    //    while (p! = start)    {       if (p-> character == value)       {          pAnt-> next = p-> next;          free (p);          p = pAnt-> next;       }       else       {          pAnt = pAnt- >          p = p-> gt;       }    } // Tests whether the list is with the aser value removed on the first node    if (start-> character == value)    {       p = start-> next;       free;       start = p;    } } // there is no need for pointers when you will use them directly // this can confuse the compiler by indexing a memory address // this may be the problem that your program is going into screw. int main () {    struct TNoDeCaractere * top = NULL;    struct TNoDeCaractere * next = NULL;    inserts ('G'); inserts ('a'); inserts ('b');    inserts ('r'); inserts ('i'); inserts ('e'); inserts ('l');    print out();    remove ('b');    print out();    //    // always release all memory used    // because if you do not do this and run the program several times there may have been a lack of memory in the system    // or other problems, because the system thinks that this memory is still in use    top = start;    next = start-> next;    while (next!)    {       top- > next = next- > next;       free (next);       next = top-> next;    }    free; }

    
02.11.2018 / 04:30