Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct liczby
- {
- int x;
- struct liczby *nast;
- };
- typedef struct liczby EL;
- typedef EL *WSK;
- void Wstaw(WSK *pierwszy,WSK nowy)
- {
- WSK temp = NULL;
- nowy->nast = NULL;
- if(!(*pierwszy))
- {
- (*pierwszy) = nowy;
- //(*pierwszy)->id = 0;
- return 0;
- }
- temp = *pierwszy;
- while(temp->nast)
- temp = temp->nast;
- //nowy->id = temp->id+1;
- temp->nast = nowy;
- }
- double srednia(WSK lista)
- {
- double srednia=0;
- int suma=0,i=0;
- WSK temp=lista;
- while(lista)
- {
- i++;
- suma+=lista->x;
- lista=lista->nast;
- }
- srednia=(double)suma/i;
- printf("\n SREDNIA: %d / %d = %lf",suma,i,srednia);
- return srednia;
- }
- double sort(WSK lista)
- {
- int i=0;
- WSK temp=lista;
- temp=temp->nast;
- while(temp)
- {
- if((lista->x)<temp->x)
- {
- temp=temp->nast;
- lista=lista->nast;
- }
- else
- {
- printf("Lista nie jest posortowana\n");
- break;
- return 0;
- }
- i++;
- if(i==4)
- printf("\nLista jest posortowana\n");
- }
- }
- void plik(char *nazwa, WSK lista)
- {
- FILE *f=NULL;
- f=fopen(nazwa,"wb");
- if(!f)
- {
- printf("Blad przy otwieraniu pliku");
- return -1;
- }
- while(lista)
- {
- fwrite(lista,sizeof(EL),1,f);
- lista=lista->nast;
- }
- fclose(f);
- }
- void wyswietl(WSK lista)
- {
- while(lista)
- {
- printf("\nLista po odczycie to: %d",lista->x);
- lista=lista->nast;
- }
- }
- void odczyt(char *nazwa, WSK lista)
- {
- FILE *f=NULL;
- f=fopen(nazwa,"rb");
- if(!f)
- {
- printf("Blad przy odczycie pliku");
- return -1;
- }
- WSK temp=(WSK)malloc(sizeof(EL));
- while(fread(temp->x,sizeof(EL),1,f))
- {
- temp=(WSK)malloc(sizeof(EL));
- Wstaw(&lista,temp);
- }
- wyswietl(lista);
- fclose(f);
- }
- void krance(WSK lista, int a, int b)
- {
- WSK temp=NULL;
- WSK temp2=NULL;
- while(lista)
- {
- if(lista->x > a && lista->x < b)
- {
- temp2 = (WSK)malloc(sizeof(EL));
- *temp2 = *lista;
- Wstaw(&temp,temp2);
- }
- lista=lista->nast;
- }
- while(temp)
- {
- printf("\nLista %d - %d: %d",a,b,temp->x);
- temp=temp->nast;
- }
- }
- int main()
- {
- int i=0;
- WSK lista=NULL;
- WSK nowy=(WSK)malloc(sizeof(EL));
- WSK lista2=NULL;
- while(i<5)
- {
- scanf("%d",&(nowy->x));
- i++;
- Wstaw(&lista,nowy);
- nowy=(WSK)malloc(sizeof(EL));
- }
- WSK temp=lista;
- while(temp)
- {
- printf(" !!! %d !!! ",temp->x);
- temp=temp->nast;
- }
- // sort(lista);
- // srednia(lista);
- // krance(lista,3,8);
- plik("plika.bin",lista);
- odczyt("plika.bin",lista2);
- }
Add Comment
Please, Sign In to add comment