Error when executing the program hangs. Circular list(Solved) However I’m still in doubt

Asked

Viewed 45 times

-2

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

#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();
}

Obs: I put a "printf("Test")" after the loop(In the function inserts)which is where the program to.

I decided how to make the code after a few attempts and manipulations in pointer positions. However, the reason I made a mistake I don’t know. I just manipulated the whole code to try to make it work. I will 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();
}
  • 2

    What is the mistake ? what should happen, and what is happening ? you need to explain clearly...

  • So friend the error is exactly the name of the "post". Please read again to see that it does not have a "name" as classic compilation errors. It has an error while running which is the phase in which the program is running on the machine. But the program does not answer me at all. Just latch and stop working.

1 answer

0

struct Tnodecaractere { character struct Tnodecharacter *next; };

struct Tnodecharacter *inicio = NULL;

void inserts(char value) { //good practice //always initialize the variables that will be used struct Tnodecharacter *p = NULL, *np = NULL; //If the list is empty, create a node and "start" point to it. if (start == NULL) { //good practice //when allocating memory always test to see if no error occurred if ((np = (struct Tnodecharacter *) malloc(sizeof(struct Tnodecharacter))) == NULL) { printf("Memory allocation failure n"); } Else { np->character = value; np->proximo = np; start = np; } } Else {//Otherwise, that is, if the list is not empty... p = start; //scan the entire list, while (p->proximo != start) { p = p->proximo; } //Create new node and insert at the end. //good practice //when allocating memory always test to see if no error occurred if ((np = (struct Tnodecharacter *) malloc(sizeof(struct Tnodecharacter))) == NULL) { printf("Memory allocation failure n"); } Else { np->character = value; np->proximo = start; // and connects the existing list to the new node. p->proximo = np; } } } void print() { struct Tnodecharacter *p = start; //p = start; while (p->proximo != start) { printf("Value: %c n", p->character); p = p->proximo; } printf("Value: %c n", p->character);// Line to print last letter. } void remove(char value) { struct Tnodecharacter *pant = start; struct Tnodecharacter * p = start->near; //Check if from the second node there is node to be removed. // while (p != start) { if (p->character == value) { pant->proximo = p->proximo; free(p); p = pant->proximo; } Else { pant = pant->proximo; p = p->proximo; } }//Test if the list has the Aser value removed at the first node if (start->character == value) { p = start->near; free(start); start = p; } } //no need to use pointers when you use pointers directly //This may confuse the compiler by indexing a memory address //This may be the problem that your program is going into a screw. int main() { struct Tnodecharacter * top = NULL; struct Tnodecharacter * next = NULL; inserts('G'); inserts('a'); inserts('b'); inserts('r'); inserts('i'); inserts('e'); inserts('l'); print(); remove ('b'); print(); // //always release all memory used //because if you do not do this and run the program several times there may be 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 != start) { top->proximo = next->proximo; free(next); next = top->proximo; } free(start); }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.