Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct slowo{
- char slowo[60];
- struct slowo *poprz;
- struct slowo *nast;
- };
- struct slowo *wsk;
- struct slowo *glowa=NULL;
- struct slowo *nowy;
- FILE *stream;
- void dodajnowy(void){
- nowy=malloc(sizeof(struct slowo));
- fscanf(stream, "%s", nowy->slowo);
- printf("%s\n", nowy->slowo);
- if(strcmp(nowy->slowo,glowa->slowo)<0){
- nowy->nast=glowa;
- nowy->poprz=NULL;
- glowa->poprz=nowy;
- glowa=nowy;
- return;
- }
- else{
- wsk=glowa;
- while(wsk->nast!=NULL){
- wsk=wsk->nast;
- if(strcmp(nowy->slowo,wsk->slowo)<0){
- nowy->nast=wsk;
- wsk->poprz->nast=nowy;
- nowy->poprz=wsk->poprz;
- wsk->poprz=nowy;
- return;
- }
- }
- }
- if(wsk->nast==NULL){
- nowy->nast=NULL;
- nowy->poprz=wsk;
- wsk->nast=nowy;
- }
- return;
- }
- void wczytajdane(void)
- {
- if((stream=fopen("slowa.txt","r"))==NULL){
- printf("Nie mozna otworzyc pliku.\n");
- exit(0);
- }
- while(!feof(stream)){
- if(glowa==NULL){
- glowa=wsk=malloc(sizeof(struct slowo));
- glowa->poprz=NULL;
- fscanf(stream, "%s", wsk->slowo);
- printf("%s\n", wsk->slowo);
- glowa->nast=NULL;
- }
- dodajnowy();
- }
- fclose(stream);
- }
- void wypiszodpoczatku(void)
- {
- printf("**************************\nulozone:\n");
- wsk=glowa;
- while(wsk->nast!=NULL)
- {
- printf("%s\n", wsk->slowo);
- wsk=wsk->nast;
- }
- printf("%s\n", wsk->slowo);
- }
- main()
- {
- wczytajdane();
- wypiszodpoczatku();
- wsk=glowa;
- struct slowo *pomoc;
- while(glowa->nast!=NULL){
- while(wsk->nast!=NULL){
- pomoc=wsk;
- wsk=wsk->nast;
- }
- pomoc->nast=NULL;
- free(wsk);
- wsk=glowa;
- }
- free(glowa);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment