Code Issues in C Chained List

Asked

Viewed 177 times

1

I’m having a problem with a C code. I’m trying to open a txt file that has movie name, year , actor,... And I’m putting the movies on a chained list. The problem is at the time of sorting the movies in the list, I tried to do by the bubble method (Bubble Sort) only that as the list is very large, when running in Dev the program is running infinitely the function without executing the rest that is in the main (by the inefficiency of sorting) someone would have a hint or help for me to apply in the code and be able to sort the list?

Follows the code:

 #include<stdio.h>
 #include<stdlib.h>
 #include<string.h>
 #include<conio.h>
  #define N 10000

 //wt escrita
  //rt leitura

 typedef struct Lista
 {
 char data[N];
 struct Lista *next;
 }Filmes;

 typedef struct ListaDupla 
{
char pessoa[N];
struct ListaDupla *prox;
struct ListaDupla *ant;
}DuplaLista;

  struct Lista* Insert(struct Lista *head,char data[N])
  {
    char aux3[N];
  struct Lista* tmp=((struct Lista*)malloc(sizeof(struct Lista)));
 int aux5;
 strcpy(tmp->data,data);
    tmp->next=NULL;
 if(head==NULL)
 {

    head=tmp;
    return head;
 }else{

struct Lista* aux=head;
struct Lista* aux2=head;
    while(aux->next!=NULL){
        aux=aux->next;
    }

    aux->next=tmp;



    while(aux!=NULL)
    {

        aux2=aux2->next;
        while(aux2!=NULL)
        {
     aux5=strcmp(aux->data,aux2->data);
    if(aux5>0)
    {

        strcpy(aux3,aux->data);
        strcpy(aux->data,aux2->data);
        strcpy(aux2->data,aux3);    
    }
        }
        aux=aux->next;
    }





    return head;
  } 

  // Complete this method
  }

 int main()
 {
 struct Lista* filmes=((struct Lista*)malloc(sizeof(struct Lista)));
 int opcao;
 char aux2[N];
 FILE *arq;
arq=fopen("nomes.txt","rt");
int i,a=0,b,aux;
char linha[600],nome[100];
if(arq==NULL)
{
    printf("Ocorreu um erro!");
    return 1;
}


  while(fgets(linha,700,arq))
  {



char *p=strtok(linha,",");
filmes=Insert(filmes,p);
while(filmes->next!=NULL)

{

printf(" \n Nome:%s",filmes->data);
filmes=filmes->next;
 }











    }



    fclose(arq);

  }
  • 1

    Did you test the code with a few movies in the list? Like a 3 or 4, to see if it works or if it "hangs" too?

  • @Gomiero he’s just with | blinking and won’t go forward if I change the whiles there he prints the movies but does not order anything is normal

  • Why don’t you order it on the list? https://answall.com/questions/301373/inserir-nomes-ordenadamente-em-uma-lista-nao-estou-sabendo-fz-isso

  • I don’t see the Bubble Sort, put it there.

  • @Marcelouchimura yes in the function of inserting the two whiles in the list

  • Take the first while and fixes the code in the second. No use doing strcpy; you have to exchange the order elements. That is, exchange the reference pointed by ant and prox of the items immediately preceding the item to be included, those of the item to be included itself and those of the item immediately following the item to be included.

  • Can you post an example? @Marcelouchimura I’m using strcpy because it’s string I tried to do changing pointers and error

  • https://answall.com/questions/301373/inserir-names-orderlye-em-uma-lista-nao-estou-sabendo-fz-isso?answertab=votes#tab-top

Show 3 more comments
No answers

Browser other questions tagged

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