5
I’m having a huge difficulty to be able to create a dynamically chained list within another using data structure and manipulation, because I haven’t found anything that exemplifies how to create a list within another one. I can create a single dynamic list, but I can’t create another one inside it.
What my program should do:
I have a list of Bands; each band is a list of albuns; and each album is a list of music. And in the program the user can manipulate music, albums and bands as he wants.
My code is below, but it doesn’t work. Someone could give me a light?
File . C
#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;
}
File . h
#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);
The includes of the headers normal are not necessary in the
Arquivo .h
. They look better in theArquivo .C
. You can add pinclude Guards](https://en.wikipedia.org/wiki/Include_guard) toArquivo .h
– pmg
If you want a program to use, and not make a program to learn C, you should definately be using a language that abstracts basic things like data structures. You can use Python, Javascript, Ruby, PHP, and even Java and worry more about your problem than how to do the basic things that C doesn’t do.
– jsbueno
What do you mean, "doesn’t work" ? You compiled the program ? The compilation ended without errors ? You ran the program ? It gave a different result than you expected ? What result did you expect and what result did it give ? The program gave crash ? I’m a doctor Jim, not a Psychic!
– zentrunix