Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct Osoba
- {
- char Nazwisko[20];
- char Imie[20];
- char Pesel[12];
- struct Osoba *Nastepny;
- };
- struct Osoba *pobierz_dane(void) {
- char nazwisko[20];
- char imie[20];
- char pesel[20];
- struct Osoba * nowy = (struct Osoba *)malloc(sizeof(struct Osoba));
- printf("Podaj nazwisko: ");
- scanf("%s", nowy->Nazwisko);
- printf("Podaj imie: ");
- scanf("%s", nowy->Imie);
- printf("Podaj pesel: ");
- scanf("%s", nowy->Pesel);
- nowy->Nastepny = 0;
- return nowy;
- };
- struct Osoba *wstaw_do_listy(struct Osoba *lista, struct Osoba *element) {
- if(lista == 0){
- lista = element;
- return lista;
- }
- if(strcmp(element->Nazwisko, lista->Nazwisko) < 0){
- element->Nastepny = lista;
- lista = element;
- return lista;
- }
- if(lista->Nastepny == 0){
- lista->Nastepny = element;
- return lista;
- }
- struct Osoba * glowa = lista;
- while(glowa->Nastepny){
- if((strcmp(element->Nazwisko,glowa->Nazwisko) >= 0) && (strcmp(element->Nazwisko,glowa->Nastepny->Nazwisko) <= 0)){
- struct Osoba * pom = glowa->Nastepny;
- element->Nastepny = pom;
- glowa->Nastepny = element;
- return lista;
- }
- glowa = glowa->Nastepny;
- }
- //jesli doszlismy do tego etapu, to znaczy, element powinien zostac wstawiony na sam koniec listy
- glowa->Nastepny = element;
- return lista;
- };
- void wyswietl_liste(struct Osoba *lista){
- while(lista != 0){
- printf("Nazwisko: %s\n",lista->Nazwisko);
- printf("Imie: %s\n",lista->Imie);
- printf("Pesel: %s\n",lista->Pesel);
- lista = lista->Nastepny;
- }
- }
- void zwolnij_pamiec(struct Osoba *lista) {
- while(lista){
- struct Osoba * pom = lista->Nastepny;
- free(lista);
- lista = pom;
- }
- }
- int main()
- {
- struct Osoba * glowa = 0;
- for(int i = 0; i < 5; i ++){ //wpisywanie 5 osob do listy
- glowa = wstaw_do_listy(glowa, pobierz_dane());
- }
- //glowa = wstaw_do_listy(glowa, doDodania);
- wyswietl_liste(glowa);
- zwolnij_pamiec(glowa);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement