Ciao, dovrei fare un programma per la gestione di code FIFO che funzioni sia per interi che per stringhe.
L'interfaccia del file della coda fifo e il seguente:
coda.h
codice:
#ifndef _CODA_H
#define _CODA_H
typedef struct queue *Q;
Q QUEUEinit(int maxN);
int QUEUEempty(Q q);
void QUEUEput(Q q, Item item);
Item QUEUEget(Q q);
void QUEUEprint(Q q);
#endif
coda.c
Sto facendo la prova di una coda fifo fi stringhe, quindi utilizzo il file stringhe.h e il relativo stringhe.c per la gestione dei dati.
codice:
#include<stdlib.h>
#include "coda.h"
#include "stringhe.h"
typedef struct QUEUEnode* link;
struct QUEUEnode
{
Item item;
link next;
};
struct queue
{
link head;
link tail; /*ultimo nodo della lista*/
};
link NEW(Item item, link next)
{
link x = malloc(sizeof *x);
x->item = item;
x->next = next;
return x;
}
Q QUEUEinit(int maxN)
{
Q q = malloc(sizeof *q);
q->head = NULL;
return q;
}
int QUEUEempty(Q q)
{
return q->head == NULL;
}
void QUEUEput(Q q, Item item)
{
link x;
if(q->head == NULL) /*lista vuota*/
{
x = NEW(item, q->head);
q->tail = x;
q->head = q->tail;
return;
}
x = NEW(item, q->tail->next); /*lista non vuota*/
q->tail->next = x;
q->tail = q->tail->next;
}
Item QUEUEget(Q q)
{
Item item = q->head->item;
link t = q->head->next;
free(q->head);
q->head = t;
return item;
}
void QUEUEprint(Q q)
{
link x;
for(x = q->head; x!=NULL; x = x->next)
print_dato(x->item);
}
stringhe.c
codice:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "stringhe.h"
Item* data_read(FILE *fp, int* ndati)
{
Item* vett;
char str[100];
int i;
(*ndati)=0;
while (fscanf(fp,"%s",str)!=EOF)
(*ndati)++;
rewind(fp);
vett = (Item*)malloc((*ndati) * sizeof(Item));
if (vett==NULL)
{
printf("Errore allocazione memoria\n");
exit(-1);
}
for (i=0; i<(*ndati); i++)
{
fscanf(fp,"%s",str);
vett[i] = strdup(str);
// vett[i] = (Item)malloc((strlen(str)+1) * sizeof(char));
if (vett[i]==NULL)
{
printf("Errore allocazione memoria\n");
exit(-1);
}
strcpy(vett[i], str);
}
fclose(fp);
return vett;
}
void print(Item* vett, int ndati)
{
int i;
for (i=0; i<ndati; i++)
printf("%s\n", vett[i]);
return;
}
Item acq_dato()
{
char item[100],*str;
scanf("%s", item);
str = strdup(item);
return str;
}
void print_dato(Item item)
{
printf("Dato: %s\n", item);
return;
}
Il compilatore mi genera i seguenti errori:
Syntax error before "Item" (relativo alla riga QUEUEput del coda.h)
Syntax errore before QUEUEget, (relativo alla riga QUEUEget del coda.h)
Mi sapreste dire dove caspita è l'errore??? Cosa sbaglio?
Grazie