Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_GRAD 2
- #define MAX_VRSTA 20
- typedef struct Meso
- {
- char grad[MAX_GRAD+1];
- char vrsta[MAX_VRSTA+1];
- double cena;
- struct Meso *psledeci;
- } Meso;
- FILE* safeopen(char naziv[], char mode[]);
- int ucitaj(FILE*f, Meso** glava);
- void inicijalizuj(Meso** glava);
- void ispisi (Meso* glava);
- int main()
- {
- FILE*fu;
- Meso* glava;
- int n;
- fu=safeopen("meso.txt", "r");
- inicijalizuj(&glava);
- ispisi(glava);
- n=ucitaj(fu, &glava);
- printf("Imamo %d elemenata u listi \n", n);
- ispisi(glava);
- fclose(fu);
- return 0;
- }
- FILE* safeopen(char naziv[], char mode[])
- {
- FILE*f;
- f=fopen(naziv, mode);
- if(f==NULL)
- {
- exit(EXIT_FAILURE);
- }
- return f;
- }
- void inicijalizuj(Meso** glava)
- {
- * glava= NULL;
- return;
- }
- int ucitaj(FILE*f, Meso** glava)
- {
- Meso a;
- Meso* novi;
- Meso* tekuci;
- int i=0;
- while(fscanf(f, "%s %s %lf", a.grad, a.vrsta, &a.cena) != EOF)
- {
- i++;
- novi= (Meso*)malloc(sizeof(Meso));
- if(novi==NULL)
- {
- printf("Nema dovoljno memorije. ");
- exit(EXIT_FAILURE);
- }
- strcpy((*novi).grad, a.grad);
- strcpy((*novi).vrsta, a.vrsta);
- (*novi).cena=a.cena;
- }
- return i;
- }
- void ispisi(Meso* glava)
- {
- Meso*tekuci;
- tekuci=glava;
- if(tekuci==NULL)
- printf("Lista je prazna. \n");
- else
- {
- while(tekuci != NULL)
- {
- printf("%s %s %6.2lf \n\n", (*tekuci).grad, (*tekuci).vrsta, (*tekuci).cena);
- tekuci=(*tekuci).psledeci;
- }
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement