I'm having a hard time getting a dynamically linked list inside another using data structure and manipulating it, as I have not found anything that exemplifies how to create a list within another. I can create a single dynamic list, but I can not create another one within it.
What my program should do:
I have a list of Bands ; each band is a list of albums ; and each album is a list of music . And in the program the user can manipulate musics, albums and bands as he wants.
My code is below, but it does not work. Could someone give me a light?
.C file
#include "biblioteca.h"
ElemM *lm;
ElemA *la;
ElemB *lb;
//---------------------------MAIN--------------------------------->
int main()
{
lb = cria_lista_banda();
la = cria_lista_album();
lm = cria_lista_musica();
system("PAUSE");
return 0;
}
//----------------------------------------------------------------->
ElemB *cria_lista_banda(){
ElemB *lb;
lb = malloc(sizeof(ElemB));
lb->prox_banda = NULL;
return lb;
}
ElemA *cria_lista_album(){
ElemA *la;
la = malloc(sizeof(ElemA));
la->prox_album= NULL;
return la;
}
ElemM *cria_lista_musica(){
ElemM *lm;
lm = malloc(sizeof(ElemM));
lm->prox_musica= NULL;
return lm;
}
int incluir_musica(ElemM *lm, ElemA *la, ElemB *lb, int id_m, int tempo, char *nome_m, int id_a, int id_b){
int valor1, valor2, valor3, i;
valor1 = busca_banda(id_b, lb);
valor2 = busca_album(id_b, id_a, lb, la);
valor3 = busca_musica(id_b, id_a, id_m, lb, la, lm);
if(valor3 == -1) return -1;
ElemB *q;
q = lb->prox_banda;
for(i = 0; i <= valor1; i++){
q = q->prox_banda;
}
ElemA *p;
p = q->albuns_da_banda->prox_album;
for(i = 0; i <= valor2; i++){
p = p->prox_album;
}
ElemM *novo, *novocpy;
p->musicas_do_album->novo = malloc( sizeof(ElemM));
novocpy = p->musica_do_album->novo;
if(novo == NULL) return 0;
novocpy->id_musica = id_m;
novocpy->tempo_da_musica = tempo;
strcpy(novocpy->nome_musica, nome_m);
novocpy->prox_musica = lc->prox_musica;
lm->prox_musica = novocpy;
return 1;
}
int busca_banda(int id, ElemB *lb){
int cont = 0;
ElemB *p;
p = lb->prox_banda;
while(p != NULL && p->id_banda != id){
p = p->prox_banda;
cont++;
}
if(p == NULL) return -1;
return cont;
}
int buscar_album(int id_b, int id_a, ElemB *lb, ElemR *la){
int valor_busca = busca_banda(id_b, lb);
int i;
int cont = 0;
if(valor_busca == -1) return -1;
ElemB *q;
q = lb->prox_banda;
for(i = 0; i <= valor_busca; i++){
q = q->prox_banda;
}
ElemA *p;
p = q->albuns_da_banda->prox_album;
while(p != NULL && p->id_album != id_a){
p = p->prox_album;
cont++;
}
if(p != NULL) return -1;
return cont;
}
int buscar_musica(int id_b, int id_a, int id_m, ElemB *lb, ElemA *la, ElemM *lm){
int valor_busca1 = busca_banda(id_b, lb);
int valor_busca2 = busca_album(id_b, id_a, lb, la);
int i;
int cont = 0;
if(valor_busca1 == -1) return -1;
if(valor_busca2 == -1) return -1;
ElemB *q;
q = lb->prox_banda;
for(i = 0; i <= valor_busca1; i++){
q = q->prox_banda;
}
ElemA *p;
p = q->albuns_da_banda->prox_album;
for(i = 0; i <= valor_busca2; i++){
p = p->prox_album;
}
ElemM *k;
k = p->musica_do_album->prox_musica;
while(k != NULL && k->id_musica != id_m){
k = k->prox_musica;
cont++;
}
if(k != NULL) return -1;
return cont;
}
.h file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct musica{
int id_musica;
int tempo_musica;
char nome_musica[30];
struct musica *prox_musica;
};
typedef struct musica ElemM;
struct album{
int id_album;
char nome_album[30];
struct musica *musica_do_album;
struct album *prox_album;
};
typedef struct album ElemA;
struct banda{
int id_banda;
char nome_banda[30];
struct album *albuns_da_banda;
struct banda *prox_banda;
};
typedef struct banda ElemB;
ElemB *cria_lista_banda();
ElemR *cria_lista_album();
ElemC *cria_lista_musica();
int incluir_musica(ElemM *lm, ElemA *la, ElemB *lb, int id_m, int tempo, char *nome_m, int id_a, int id_b)
int busca_banda(int id, ElemB *lb);
int buscar_album(int id_b, int id_a, ElemB *lb, ElemR *la);
int buscar_musica(int id_b, int id_a, int id_m, ElemB *lb, ElemA *la, ElemM *lm);