My C code contains some errors (Data Structure List)

Asked

Viewed 75 times

2

I created a Music Player for a Data Structure discipline using Double Chained and Circular List. However, you are showing some errors and I am not seeing where. Could someone help me ? Maybe it’s logic error and/or syntax error.

The play option shows the name of the current song and moves to the next one. showPlaylist shows all 7 songs that the playlist contains.

#include <stdio.h>
#include <stdbool.h>
#include <locale.h>
#include <string.h>

typedef struct Music {
    char name[25];
    struct Music *previous;
    struct Music next; 
} music;

music *playlist;


void start(){
    music *m1=malloc (sizeof(music));
    strcpy(m1->name, "MUSIC1");
    playlist = m1;
    music *m2=(music *)malloc (sizeof(music));
    strcpy(m2->name, "MUSIC2");
    m1->next = m2;
    m2->previous = m1; //ERRO2
    music *m3=(music *)malloc (sizeof(music));
    strcpy(m3->name, "MUSIC3");
    m2->next = m3;
    m3->previous = m2;
    music *m4=(music *)malloc (sizeof(music));
    strcpy(m4->name, "MUSIC4");
    m3->next = m4;
    m4->previous = m3;
    music *m5=(music *)malloc (sizeof(music));
    strcpy(m5->name, "MUSIC5");
    m4->next = m5;
    m5->previous = m4;
    music *m6=(music *)malloc (sizeof(music));
    strcpy(m6->name, "MUSIC6");
    m5->next = m5;
    m6->previous = m5;
    music *m7=(music *)malloc (sizeof(music));
    strcpy(m7->name, "MUSIC7");
    m6->next = m7;
    m7->previous = m6;
    m7->next = m1;
    m1->previous = m7;
}

void play(){
    printf("%s\n",playlist->name);
    playlist=playlist->next;
}

void showPlaylist(){
    music *aux = playlist;
    int i = 1;
    while (i<=7){
        printf("%s\n",aux->name);
        aux = aux->next;
        i++;
    }
}

void main(){
    setlocale(LC_ALL, "Portuguese");
    int op;
    start();
    do
    {
        printf("1 - Play music\n");
        printf("2 - Show Playlist\n");
        printf("0 - Exit\n");
        printf("Choose Option:");
        scanf("%d",op);
        int n;
        switch(op) {
            case 1: play();
            case 2: showPlaylist(); 
                    break;
            case 0: printf("Byee!\n");
                    break;
            default: printf("What?!\n");
        }       
    } while (op!=0);
}

1 answer

1


Your code has several mistakes, let’s go in pieces:

1 - You forgot to include the <stdlib.h> in your code (for malloc function);

2 - Your struct should be like this:

typedef struct Music {
    char name[25];
    struct Music *previous;
    struct Music *next; // ponteiro para next, não a struct next
} music;

3 - You forgot to convert the (void*) which is returned by malloc for (*music) on M1, so it should be like this: music *m1= (music*) malloc(sizeof(music));

4 - The function scanf receives pointers for variables, so should be: scanf("%d", &op);

The tidy code at the end can be:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <locale.h>
#include <string.h>

typedef struct Music {
    char name[25];
    struct Music *previous;
    struct Music *next;
} music;

music *playlist;


void start(){
    music *m1= (music*) malloc(sizeof(music));
    strcpy(m1->name, "MUSIC1");
    playlist = m1;
    music *m2=(music *)malloc (sizeof(music));
    strcpy(m2->name, "MUSIC2");
    m1->next = m2;
    m2->previous = m1; //ERRO2
    music *m3=(music *)malloc (sizeof(music));
    strcpy(m3->name, "MUSIC3");
    m2->next = m3;
    m3->previous = m2;
    music *m4=(music *)malloc (sizeof(music));
    strcpy(m4->name, "MUSIC4");
    m3->next = m4;
    m4->previous = m3;
    music *m5=(music *)malloc (sizeof(music));
    strcpy(m5->name, "MUSIC5");
    m4->next = m5;
    m5->previous = m4;
    music *m6=(music *)malloc (sizeof(music));
    strcpy(m6->name, "MUSIC6");
    m5->next = m5;
    m6->previous = m5;
    music *m7=(music *)malloc (sizeof(music));
    strcpy(m7->name, "MUSIC7");
    m6->next = m7;
    m7->previous = m6;
    m7->next = m1;
    m1->previous = m7;
}

void play(){
    printf("%s\n",playlist->name);
    playlist=playlist->next;
}

void showPlaylist(){
    music *aux = playlist;
    int i = 1;
    while (i<=7){
        printf("%s\n",aux->name);
        aux = aux->next;
        i++;
    }
}

void main(){
    setlocale(LC_ALL, "Portuguese");
    int op;
    start();
    do
    {
        printf("1 - Play music\n");
        printf("2 - Show Playlist\n");
        printf("0 - Exit\n");
        printf("Choose Option:");
        scanf("%d", &op);
        int n;
        switch(op) {
            case 1: play();
            case 2: showPlaylist();
                    break;
            case 0: printf("Byee!\n");
                    break;
            default: printf("What?!\n");
        }
    } while (op!=0);
}

Note: I did not tidy/improve other things that can be tidied/improved because I only tidied up what may be preventing the development of your program.

  • 1

    Many mistakes of mine due to lack of attention. Thanks Adriano for the revision in the code and for the help. I’m slowly improving.

Browser other questions tagged

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