Skip to content

Instantly share code, notes, and snippets.

@giljr
Created November 26, 2021 20:40
Show Gist options
  • Save giljr/bbc4a2c505f451687b6e98cab39ac3c9 to your computer and use it in GitHub Desktop.
Save giljr/bbc4a2c505f451687b6e98cab39ac3c9 to your computer and use it in GitHub Desktop.
MUSIC ALBUM_V2.C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 20
char filename[MAX_LEN] = "musicalbum_v0.csv";
FILE *file;
// Prototypes:
int menu();
void insertFront(int, char[], char[], int);
void insertLast(int, char[], char[], int);
void insertMid(int, char[], char[], int, int);
int removeFromDeque(int);
void displayDeque();
void loadFile(char[]);
struct SimpleDeque_Item
{
int track;
char artist[50];
char music[50];
int duration;
struct SimpleDeque_Item *next;
} * head;
int main()
{
int op, num, pos, c, res, track, duration;
char artist[50], music[50];
head = NULL;
FILE *fpt;
errno_t err;
loadFile(filename);
while (1)
{
op = menu();
switch (op)
{
case 1: // 1.Insert at beginning of deque
printf("Enter the desired Track number: ");
scanf_s("%d", &track);
while ((c = getchar()) != '\n' && c != EOF)
{
}
printf("Enter the Artist name: ");
scanf_s("%[a-zA-z ]]", &artist);
while ((c = getchar()) != '\n' && c != EOF)
{
}
printf("Enter the Music name: ");
scanf_s("%[a-zA-z ]]", &music);
while ((c = getchar()) != '\n' && c != EOF)
{
}
printf("Enter the Duration of the track: ");
scanf_s("%d", &duration);
while ((c = getchar()) != '\n' && c != EOF)
{
}
insertFront(track, artist, music, duration);
break;
case 2: // 2.Insert at end of deque
printf("Enter the desired Track number: ");
scanf_s("%d", &track);
while ((c = getchar()) != '\n' && c != EOF)
{
}
printf("Enter the Artist name: ");
scanf_s("%[a-zA-z ]]", &artist);
while ((c = getchar()) != '\n' && c != EOF)
{
}
printf("Enter the Music name: ");
scanf_s("%[a-zA-z ]]", &music);
while ((c = getchar()) != '\n' && c != EOF)
{
}
printf("Enter the Duration of the track: ");
scanf_s("%d", &duration);
while ((c = getchar()) != '\n' && c != EOF)
{
}
insertLast(track, artist, music, duration);
break;
case 3: // 3.Insert at middle of deque
printf("Enter the desired Track number: ");
scanf_s("%d", &track);
while ((c = getchar()) != '\n' && c != EOF)
{
}
printf("Enter the Artist name: ");
scanf_s("%[a-zA-z ]]", &artist);
while ((c = getchar()) != '\n' && c != EOF)
{
}
printf("Enter the Music name: ");
scanf_s("%[a-zA-z ]]", &music);
while ((c = getchar()) != '\n' && c != EOF)
{
}
printf("Enter the Duration of the track: ");
scanf_s("%d", &duration);
while ((c = getchar()) != '\n' && c != EOF)
{
}
printf("Enter the position you want to insert the new track: ");
scanf_s("%d", &pos);
while ((c = getchar()) != '\n' && c != EOF)
{
}
insertMid(track, artist, music, duration, pos);
break;
case 4: // 4.Remove from deque
printf("Enter the desired Track number: ");
scanf_s("%d", &track);
while ((c = getchar()) != '\n' && c != EOF)
{
}
res = removeFromDeque(track);
if (res == 1)
printf("Track Number removed from Deque!");
else
printf("Track Number not found!");
break;
case 5: // 5.Display deque items
displayDeque();
break;
case 6: // 6.Exit
err = fopen_s(&fpt, filename, "w+");
/* Saving all Tracks to a File */
struct SimpleDeque_Item *ScanItem;
ScanItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
ScanItem = head;
if (ScanItem == NULL)
{
return 1;
}
while (ScanItem != NULL)
{
fprintf(fpt, "%i,%s,%s,%i\n", ScanItem->track, ScanItem->artist, ScanItem->music, ScanItem->duration);
ScanItem = ScanItem->next;
}
/* Closing the file to avoid memory leakage */
fclose(fpt);
printf("\n %s file saved sucessfuly!\n\n", filename);
printf("\nThank You kindly!\n\n");
return 0;
default:
printf("Invalid Number!\n");
}
}
return 0;
}
int menu()
{
int op, c;
system("Cls");
printf("WELCOME to J3 MUSIC ALBUM!!! ENJOY!!!\n");
printf("This Deque supports these operations:\n");
printf("_____________________________________\n\n");
printf("1.Insert at beginning of deque\n");
printf("2.Insert at end of deque\n");
printf("3.Insert at middle of deque\n");
printf("4.Remove from deque\n");
printf("5.Display deque items\n");
printf("6.Exit\n");
printf("_____________________________________\n");
printf("Which operation you chose? ");
scanf_s("%d", &op);
while ((c = getchar()) != '\n' && c != EOF)
{
}
system("Cls");
return op;
}
void insertFront(int track, char artist[50], char music[50], int duration)
{
struct SimpleDeque_Item *NewItem;
NewItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
NewItem->track = track;
strcpy(NewItem->artist, artist);
strcpy(NewItem->music, music);
NewItem->duration = duration;
if (head == NULL)
{
head = NewItem;
head->next = NULL;
}
else
{
NewItem->next = head;
head = NewItem;
}
}
void insertLast(int track, char artist[50], char music[50], int duration)
{
struct SimpleDeque_Item *NewItem;
NewItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
struct SimpleDeque_Item *ScanItem;
ScanItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
NewItem->track = track;
strcpy(NewItem->artist, artist);
strcpy(NewItem->music, music);
NewItem->duration = duration;
if (head == NULL)
{
head = NewItem;
head->next = NULL;
}
else
{
ScanItem = head;
while (ScanItem->next != NULL)
ScanItem = ScanItem->next;
ScanItem->next = NewItem;
NewItem->next = NULL;
}
}
void insertMid(int track, char artist[50], char music[50], int duration, int position)
{
struct SimpleDeque_Item *NewItem;
NewItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
struct SimpleDeque_Item *ScanItem;
ScanItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
struct SimpleDeque_Item *AuxiliaryItem;
AuxiliaryItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
NewItem->track = track;
strcpy(NewItem->artist, artist);
strcpy(NewItem->music, music);
NewItem->duration = duration;
if (position == 0)
{
head = NewItem;
head->next = NULL;
}
else
{
ScanItem = head;
for (int i = 0; i < position - 1; i++)
ScanItem = ScanItem->next;
AuxiliaryItem = ScanItem->next;
ScanItem->next = NewItem;
NewItem->next = AuxiliaryItem;
}
}
int removeFromDeque(int track)
{
struct SimpleDeque_Item *ScanItem;
ScanItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
struct SimpleDeque_Item *Anterior;
Anterior = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
ScanItem = head;
while (ScanItem != NULL)
{
if (ScanItem->track == track)
{
if (ScanItem == head)
{
head = ScanItem->next;
free(ScanItem);
return 1;
}
else
{
Anterior->next = ScanItem->next;
free(ScanItem);
return 1;
}
}
else
{
Anterior = ScanItem;
ScanItem = ScanItem->next;
}
}
return 0;
}
void displayDeque()
{
struct SimpleDeque_Item *ScanItem;
ScanItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
ScanItem = head;
if (ScanItem == NULL)
{
return;
}
while (ScanItem != NULL)
{
printf("_____________________________________\n");
printf("Track: %i\n", ScanItem->track);
printf("Artist: %s\n", ScanItem->artist);
printf("Music: %s\n", ScanItem->music);
printf("Duration: %i\n", ScanItem->duration);
printf("_____________________________________\n");
ScanItem = ScanItem->next;
}
printf("\n");
system("pause");
return;
}
void loadFile(char filename[])
{
file = fopen(filename, "r+");
char line[500];
while (fgets(line, sizeof(line), file))
{
int track;
char artist[50] = {0};
char music[50] = {0};
int duration;
sscanf(line, "%i,%[^,], %[^,], %i", &track, artist, music, &duration);
insertLast(track, artist, music, duration);
}
fclose(file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment