My code in C contains some errors (Data Structure List)

2

I created a Music Player for a Data Structure discipline using Double-Chained and Circular List. However, it is showing some errors and I can not see where. Can anybody 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 song. The showPlaylist option shows all 7 songs 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);
}
    
asked by anonymous 21.03.2018 / 23:44

1 answer

1

Your code has several flaws, let's break it down:

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

2 - Your struct should look 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 (void*) which is returned by malloc to (* music) in m1, so it should look like this: music *m1= (music*) malloc(sizeof(music));

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

The code arranged 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 have not set up / improved any other things that can be fixed / improved since I only set up what may be preventing the development of your program.     

22.03.2018 / 00:02