Skip to content

Instantly share code, notes, and snippets.

@Duckle29
Last active July 23, 2017 14:38
Show Gist options
  • Save Duckle29/ce76ebd59b7d40dc242b44b5f26ad83c to your computer and use it in GitHub Desktop.
Save Duckle29/ce76ebd59b7d40dc242b44b5f26ad83c to your computer and use it in GitHub Desktop.
beer linked list project
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "beer.h"
static int g_nNumberOfBeers=0; /* Made static so it only can be accessed here */
/* g_ is for "global" */
struct Beer *addBeer(struct Beer * beer) {
struct Beer * new_beer;
/* Allocate space for new element/node: */
new_beer=(struct Beer *)malloc(sizeof(struct Beer));
/* Insert new element/beer before any other element: */
new_beer->next=beer;
printf("name: ");
scanf("%s",new_beer->name);
printf("Price: ");
scanf("%f",&new_beer->price);
printf("Percentage: ");
scanf("%f",&new_beer->alc);
printf("Amount [ml]: ");
scanf("%f",&new_beer->ml);
g_nNumberOfBeers++; /* We increment since we just added a beer */
return new_beer;
}
void seeBeers(struct Beer *beers)
{
if(beers == NULL)
{
printf("You're out of beers :(\n\n");
return;
}
int i;
for (i=0; i<g_nNumberOfBeers; i++)
{
printf("name: %s\n",beers->name);
printf("Price: %f\n",beers->price);
printf("Percentage: %f\n",beers->alc);
printf("Amount [ml]: %f\n--- --- ---\n",beers->ml);
beers=beers->next;
}
}
struct Beer * findBeer(struct Beer * beers, char search_term[81])
{
char st[81];
if(search_term[0] == 0)
{
printf("Enter name of beer you wish to find\n");
scanf("%s", st);
}
else
{
strcpy(st, search_term);
}
struct Beer * currentBeer = NULL;
struct Beer * previousBeer = NULL;
for(currentBeer = beers; currentBeer != NULL && strcmp(currentBeer->name, st); currentBeer = currentBeer->next)
{
printf("%s", currentBeer->name);
previousBeer = currentBeer;
}
return previousBeer;
}
struct Beer * removeBeer(struct Beer * beers)
{
struct Beer * toEdit = NULL;
struct Beer * toFree = NULL;
seeBeers(beers);
printf("Enter name of beer you wish to remove\n");
fflush(stdout);
char search_term[81];
scanf("%s",search_term);
toEdit = findBeer(beers, search_term);
toFree = toEdit->next;
if(toEdit->next == NULL)
{
printf("You don't have that beer :(\n");
fflush(stdout);
return;
}
toEdit->next = toEdit->next->next;
free(toFree);
g_nNumberOfBeers--;
return beers;
}
struct Beer {
char name[81]; /* name of beer - max of 80 letters */
float price; /* Price */
float alc; /* Percentage */
float ml; /* Amount milliliter [ml] */
struct Beer *next;
};
struct Beer * addBeer(struct Beer * beer);
struct Beer * findBeer(struct Beer * beers, char search_term[81]); /* This function returns the pointer to the element before the one with the correct name */
struct Beer * removeBeer(struct Beer * beer);
void seeBeers(struct Beer *beer);
#include <stdio.h>
#include <stdbool.h>
#include "beer.h"
int menu() {
int nSelection;
printf("\nOptions:\n");
printf("1) Add new beer\n");
printf("2) See all beers\n");
printf("3) Find a beer\n");
printf("4) Remove a beer\n");
printf("5) Remove all beers\n");
printf("6) Stop the program\n");
scanf("%d",&nSelection);
return nSelection;
}
int main()
{
struct Beer *beer;
beer=NULL;
bool run = true;
while(run)
{
switch(menu())
{
case 1:
beer=addBeer(beer);
break;
case 2:
seeBeers(beer);
break;
case 3: ;
struct Beer * foundBeer;
foundBeer = findBeer(beer,"\0");
printf("name: %s\n",foundBeer->name);
printf("Price: %f\n",foundBeer->price);
printf("Percentage: %f\n",foundBeer->alc);
printf("Amount [ml]: %f\n--- --- ---\n",foundBeer->ml);
break;
case 4:
beer=removeBeer(beer);
break;
case 5:
beer=NULL;
break;
case 6:
run = false;
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment