1
0
Files
2024-03-22 17:37:24 +01:00

113 lines
1.9 KiB
C

// Laboratorio 8 - Esercizio 3 - AssetList.c
// Matteo Schiff - s295565
#include <stdlib.h>
#include <string.h>
#include "Asset.h"
#include "AssetList.h"
#include "ExrateBST.h"
typedef struct node *link;
struct node
{
Asset asset;
link next;
};
struct assetList
{
link head;
int N;
};
static link createNode(Asset asset, link next)
{
link new = malloc(sizeof(*new));
new->asset = asset;
new->next = next;
return new;
}
AssetList AssetListCreate()
{
AssetList al = malloc(sizeof(*al));
al->head = NULL;
al->N = 0;
return al;
}
void AssetListFree(AssetList al)
{
for (link x = al->head, p = x->next; p != NULL; x = p, p = x->next)
{
free(x);
}
free(al);
}
int AssetListLength(AssetList al) {
return al->N;
}
void AssetListPrint(AssetList al) {
link h = al->head;
while (h != NULL) {
AssetPrint(h->asset);
h = h->next;
}
}
Asset * AssetListGet(AssetList al, int index) {
link h;
int i = 0;
h = al->head;
while (h != NULL) {
if (i == index)
return &(h->asset);
h = h->next;
i++;
}
return NULL;
}
Asset * AssetListSearch(AssetList al, char * query) {
link h;
h = al->head;
while (h != NULL) {
if (strcmp(AssetTitle(h->asset), query) == 0)
return &(h->asset);
h = h->next;
}
return NULL;
}
void AssetListInsert(AssetList al, Asset asset)
{
link x, p;
if (al->head == NULL)
{
al->head = createNode(asset, al->head);
al->N++;
return;
}
for (x = al->head, p = x->next; p != NULL; x = p, p = x->next)
{
if (AssetCompare(asset, x->asset) == 0)
{
ExrateBSTMerge(AssetGetExrates(x->asset), AssetGetExrates(asset));
return;
}
else if (AssetCompare(asset, x->asset) < 0)
{
break;
}
}
x->next = createNode(asset, p);
al->N++;
}