I have written a function named rucksack
that takes a linear list and a limiter which limits the weight. The function returns the max costs of chosen items you can choose without getting over the weight of 4500.
#include <stdio.h>
struct item {
int weight; // Artikelgewicht in Gramm
int value; // Artikelwert in Euro
int stock; // Vorhandene Artikelmenge
struct item *next;
};
struct item laptop = {1500, 1000, 3, NULL};
struct item tablet = { 700, 300, 2, &laptop};
struct item mobile = { 180, 500, 2, &tablet};
struct item *items = &mobile;
int max(int a,int b)
{
return a > b ? a : b ;
}
int rucksack ( struct item* item , int limit)
{
if (item == NULL)
return 0 ;
int sum = 0 ;
for (int i = 0 ; i<= item->stock && i * item->weight<=limit ; i++)
sum = max(sum, i * item->value + rucksack (item->next , limit - i*item->weight )) ;
return sum;
}
int main() {
printf("%d", rucksack(items, 4500));
}
I want to know which combination of items was chosen but I do not know how to implement that in my recursion. Do anyone can help?