Skip to content

Instantly share code, notes, and snippets.

@giljr
Created November 29, 2021 21:56
Show Gist options
  • Save giljr/bc9687465dc1b888429334e61e54b9ff to your computer and use it in GitHub Desktop.
Save giljr/bc9687465dc1b888429334e61e54b9ff to your computer and use it in GitHub Desktop.
Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
/* This program uses pointer, malloc and structs :) */
int main(int argc, char *argv[]) {
/* Declaration of the structure to hold anniversaries data */
typedef struct Track {
int track;
char artist[50];
char music[50];
int duration;
struct Track *next; /* Pointer to the next structure */
struct Track *previous; /* Pointer to the previous structure */
}Track_t;
Track_t *first = NULL; /* Pointer to the first Register */
Track_t *current = NULL; /* Pointer to the current Register */
Track_t *last = NULL; /* Pointer to the previus Register */
/* Test value for ending input */
char test = '\0';
/* Variable to deal with buffer's cleaning */
int c;
printf("\n\n_____________________!!!!!WELCOME TO JR'PLAYLIST!!!!'_______________________\n\n");
for( ; ; ) /* Infinite loop; you do not have to worry about arrays and mess around with indexes */
{
printf("\nDo you What to enter a Track now (Y or N)?");
scanf(" %c", &test);
//while ((c = getchar()) != '\n' && c != EOF) { } /* clears input buffer */
if(tolower(test) == 'n') break;
/* Allocate memory to hold the structure */
/* This statement allocates the space for each structure dynamically as it's requered */
/* The malloc() function allocates the number of bytes specified by its argument and
returns the address of the block of memory allocated as a pointer to type votrack */
//ptr2Calendar[rcounter] = (Track_t*) malloc(sizeof(Track_t));
current = (Track_t*) malloc(sizeof(Track_t));
if(first == NULL)
{
first = current; /* Set pointer to first register */
current->previous = NULL; /* The first node points previous to NULL */
}
else
{
last->next = current; /* Set next pointer for previous register */
current->previous = last; /* Previous address for current node */
}
printf("\nPlease enter the Track Number: ");
//scanf("%i", &Calendar[rcount].month); /* Read the track of the person in the register */
//scanf("%i", &ptr2Calendar[rcounter]->month);
scanf("%i", &current->track);
while ((c = getchar()) != '\n' && c != EOF) { } /* clears input buffer */
printf("\nPlease enter the Artist Name: ");
gets(current->artist);
//scanf("%s", &current->artist);
//while ((c = getchar()) != '\n' && c != EOF) { }
printf("\nPlease enter the Music Name: ");
gets(current->music);
//scanf("%i", &current->music);
//while ((c = getchar()) != '\n' && c != EOF) { } /* clears input buffer */
printf("\nPlease enter the Track Duration (sec): ");
//scanf("%i", &Calendar[rcount].month); /* Read the track of the person in the register */
//scanf("%i", &ptr2Calendar[rcounter]->month);
scanf("%i", &current->duration);
while ((c = getchar()) != '\n' && c != EOF) { } /* clears input buffer */
current->next = NULL; /* In case it is the last... */
last = current; /* Save address of the last register */
}
//current = first; /* Starting at the beginning */
/* Printing the receipt if everything is ok */
printf ("\n You have Entered these Musics:\n\n");
printf("__________________________Tracks______List____________________________\n");
printf("| Track | Artist Name | Music Title | Time(sec) |\n") ;
printf("----------------------------------------------------------------------\n");
/* This for loop prints the output and frees the memory allocated, block by block */
//for(int i = 0; i < rcounter; i++)
while(current != NULL) /* As long as we have a valtrack pointer */
{
printf("| %i | %s | %s | %i |\n", current->track, current->artist, current->music, current->duration);
last = current; /* Save the pointer so we can free memory */
//current = current->next; /* Get the pointer to the next */
current = current->previous; /* PRINTTING IN REVERSE ORDER */
/* It is very important to free each block of memory previously allocated;
If you don't keep track and don't free it after malloc, you might have memory leaks :/
it's considered good style to free memory as soon as you don't need it any more. Read this post:
https://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc */
//free(ptr2Calendar[i]);
//free(previous);
free(last);
}
printf("\n______________________________________________________________________\n");
printf("\n______________________________________________________________________\n");
return 0;
}
© 2021 GitHub, Inc.
Terms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment