Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #pragma warning(disable:4996)
- #define MAX_BUFOR 80
- typedef struct stud {
- char *imie;
- char *nazwisko;
- int rok;
- char *adres;
- double stypendium;
- struct stud *n;
- struct stud *prev;
- }STUDENT;
- STUDENT *H;
- STUDENT *T;
- FILE *fd = NULL;
- void list1(FILE *plik)
- {
- unsigned len;
- STUDENT *B = NULL, *P = NULL;
- char bufor[MAX_BUFOR];
- H = (STUDENT *)NULL;
- T = (STUDENT *)NULL;
- while (fgets(bufor, MAX_BUFOR, fd)) {
- B = (STUDENT *)malloc(sizeof(STUDENT));
- if (H == (STUDENT *)NULL)
- {
- H = B;
- T = B;
- }
- else
- {
- P->n = B;
- T = P->n;
- }
- B->n = (STUDENT *)NULL;
- B->prev = (STUDENT *)NULL;
- len = (unsigned)strlen(bufor);
- bufor[len - 1] = '\0';
- B->imie = (char*)malloc(len);
- strcpy(B->imie, bufor);
- fgets(bufor, MAX_BUFOR, fd);
- len = (unsigned)strlen(bufor);
- bufor[len - 1] = '\0';
- B->nazwisko = (char*)malloc(len);
- strcpy(B->nazwisko, bufor);
- fgets(bufor, MAX_BUFOR, fd);
- B->rok = atoi(bufor);
- fgets(bufor, MAX_BUFOR, fd);
- len = (unsigned)strlen(bufor);
- bufor[len - 1] = '\0';
- B->adres = (char*)malloc(len);
- strcpy(B->adres, bufor);
- fgets(bufor, MAX_BUFOR, fd);
- B->stypendium = atof(bufor);
- B->prev = P;
- P = B;
- }
- }
- STUDENT *max_s()
- {
- STUDENT *w, *x;
- double max;
- max = H->stypendium;
- x = H;
- for (w = H; w != (STUDENT *)NULL; w = w->n)
- {
- if (w->stypendium > max)
- {
- max = w->stypendium;
- x = w;
- }
- }
- return x;
- }
- void dispList1()
- {
- STUDENT *w;
- w = H;
- if (H == NULL)
- {
- printf("Lista pusta");
- return;
- }
- for (w = H; w != (STUDENT *)NULL; w = w->n)
- {
- printf("%d %d\n", (int)w, (int)w->n);
- printf("%12s| %12s| %4d| %25s| %7.2lf\n", w->imie, w->nazwisko, w->rok, w->adres, w->stypendium);
- }
- }
- void revdispList1() // wypisanie listy od konca
- {
- STUDENT *w;
- w = T;
- if (H == NULL)
- {
- printf("Lista pusta");
- return;
- }
- for (w = T; w != (STUDENT *)NULL; w = w->prev)
- {
- printf("%d %d\n", (int)w, (int)w->n);
- printf("%12s| %12s| %4d| %25s| %7.2lf\n", w->imie, w->nazwisko, w->rok, w->adres, w->stypendium);
- }
- }
- void addHeadList1()
- {
- char bufor[MAX_BUFOR];
- unsigned len;
- STUDENT *head;
- head = (STUDENT *)malloc(sizeof(STUDENT));
- printf("podaj imie, nazwisko, rok, adres, stypendium\n");
- fgets(bufor, MAX_BUFOR, stdin);
- len = (unsigned)strlen(bufor);
- bufor[len - 1] = '\0';
- head->imie = (char*)malloc(len);
- strcpy(head->imie, bufor);
- fgets(bufor, MAX_BUFOR, stdin);
- len = (unsigned)strlen(bufor);
- bufor[len - 1] = '\0';
- head->nazwisko = (char*)malloc(len);
- strcpy(head->nazwisko, bufor);
- fgets(bufor, MAX_BUFOR, stdin);
- head->rok = atoi(bufor);
- fgets(bufor, MAX_BUFOR, stdin);
- len = (unsigned)strlen(bufor);
- bufor[len - 1] = '\0';
- head->adres = (char*)malloc(len);
- strcpy(head->adres, bufor);
- fgets(bufor, MAX_BUFOR, stdin);
- head->stypendium = atof(bufor);
- head->n = H;
- H = head;
- }
- int lenList1()
- {
- int count = 0;
- STUDENT *w;
- w = H;
- if (H == NULL)
- {
- printf("Lista pusta");
- return count;
- }
- while (w)
- {
- count += 1;
- w = w->n;
- }
- return count;
- }
- void remHeadList1()
- {
- STUDENT *w = NULL;
- if (H == NULL)
- {
- printf("Lista pusta");
- return;
- }
- w = H->n;
- free(H);
- H = w;
- }
- void addTailList1()
- {
- char bufor[MAX_BUFOR];
- unsigned len;
- STUDENT *tail, *w;
- if (H == NULL)
- {
- printf("Lista pusta");
- return;
- }
- w = T;
- tail = (STUDENT *)malloc(sizeof(STUDENT));
- w->n = tail;
- printf("podaj imie, nazwisko, rok, adres, stypendium\n");
- fgets(bufor, MAX_BUFOR, stdin);
- len = (unsigned)strlen(bufor);
- bufor[len - 1] = '\0';
- tail->imie = (char*)malloc(len);
- strcpy(tail->imie, bufor);
- fgets(bufor, MAX_BUFOR, stdin);
- len = (unsigned)strlen(bufor);
- bufor[len - 1] = '\0';
- tail->nazwisko = (char*)malloc(len);
- strcpy(tail->nazwisko, bufor);
- fgets(bufor, MAX_BUFOR, stdin);
- tail->rok = atoi(bufor);
- fgets(bufor, MAX_BUFOR, stdin);
- len = (unsigned)strlen(bufor);
- bufor[len - 1] = '\0';
- tail->adres = (char*)malloc(len);
- strcpy(tail->adres, bufor);
- fgets(bufor, MAX_BUFOR, stdin);
- tail->stypendium = atof(bufor);
- tail->n = NULL;
- tail->prev = T;
- T = tail;
- }
- void remTailList1()
- {
- STUDENT *w;
- if (H == NULL)
- {
- printf("Lista pusta");
- return;
- }
- w = T->prev;
- free(w->n);
- w->n = NULL;
- }
- int main()
- {
- STUDENT *ms;
- fd = fopen("dane.txt", "r");
- if (fd == NULL) {
- printf("Nie mogę otworzyć pliku dane.txt do odczytu!\n");
- getchar();
- exit(1);
- }
- list1(fd);
- //revdispList1();
- //ms = max_s();
- //printf("%s %.2lf\n", ms->nazwisko, ms->stypendium);
- //addHeadList1();
- addTailList1();
- //dispList1();
- //printf("dlugosc listy = %d\n", lenList1());
- //remHeadList1();
- revdispList1();
- }
Advertisement
Add Comment
Please, Sign In to add comment