Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include<ctype.h>
- typedef struct Osoba
- {
- int ID;
- char imie[100];
- char nazwisko[100];
- int wiek;
- char plec[50];
- struct Osoba* next;
- } Osoba ;
- // zlicza dlugosc listy
- int dlugosc_listy(Osoba* first)
- {
- int d = 0;
- Osoba* current = first;
- while(current != NULL)
- {
- d++;
- current = current->next;
- }
- return d;
- }
- // dodaje osobe do listy
- void dodajOsobe(Osoba** first)
- { char line[500];
- Osoba* nowa = (Osoba*)malloc(sizeof(Osoba));
- printf("Podaj imie: ");
- scanf("%s", nowa->imie);
- printf("Podaj nazwisko: ");
- scanf("%s", nowa->nazwisko);
- printf("Podaj wiek: " );
- scanf("%d", &(nowa->wiek));
- printf("Podaj plec: " );
- scanf("%s", nowa->plec);
- nowa->next=(*first);
- *first= nowa;
- }
- //Wypisuje cala liste
- void wypisz_liste(Osoba* first)
- {
- Osoba* current = first;
- if(first == NULL)
- printf("first JEST PUSTA");
- else
- printf("first zawiera %d elementow: \n", dlugosc_listy(first) );
- int i=1;
- while( current != NULL)
- {
- printf("%d %s %s %d %s\n", i, current->imie, current->nazwisko, current->wiek, current->plec);
- current=current->next;
- i++;
- }
- }
- //Usuwa element z listy
- void usun(Osoba** first, int ID)
- {
- Osoba* poprzedni = NULL;
- Osoba* current = *first;
- int i;
- for(i = 1; i < ID; i++)
- {
- poprzedni=current;
- current=current->next;
- }
- if(poprzedni==NULL)
- {
- (*first)=(*first)->next;
- free(current);
- }
- else
- {
- poprzedni->next=current->next;
- free(current);
- }
- }
- // Usuwa osobe po indexie
- void usun_osobe(Osoba** first)
- {
- int ID;
- printf("Podaj indeks osoby: " );
- scanf("%d", &ID);
- if((ID > dlugosc_listy(*first)) || (ID < 1))
- {
- printf("Nie ma takiego indeksu");
- }
- else
- {
- usun(first,ID);
- }
- }
- int usun_imnaz_first(char* imie, char* nazwisko, Osoba** first)
- {
- Osoba* poprzedni=NULL;
- Osoba* current= *first;
- while((current != NULL) && ( ( strcmp(current->imie, imie)!=0) || (strcmp(current->nazwisko, nazwisko )!=0) ))
- {
- poprzedni=current;
- current=current->next;
- }
- if(current == NULL )
- {
- return 0;
- }
- else
- {
- if(poprzedni==NULL)
- {
- (*first)=(*first)->next;
- free(current);
- }
- else
- {
- poprzedni->next=current->next;
- free(current);
- }
- return 1;
- }
- }
- //usuwa po imieniu i nazwisku
- /*void usun_imnaz(Osoba** first)
- {
- char imie[100];
- char nazwisko[100];
- printf("Podaj imie: ");
- scanf("%s", imie);
- printf("Podaj nazwisko: ");
- scanf("%s", nazwisko);
- if(usun_imnaz_first(imie, nazwisko, first))
- printf("Osoba zostala usunieta");
- else
- printf("Osoba nie zostala usunieta, nie ma takiej osoby");
- }*/
- //wypisz osobe o podanym indexie
- void wypisz_osobe(Osoba* first)
- {
- int ID;
- printf("Podaj numer osoby: " );
- scanf("%d", &ID);
- if((ID > dlugosc_listy(first)) || (ID < 1))
- {
- printf("Nie ma takiego indeksu");
- }
- else
- {
- int i;
- Osoba* current = first;
- for(i = 1; i < ID; i++)
- {
- current=current->next;
- }
- printf("%d %s %s %d %s\n", i, current->imie, current->nazwisko, current->wiek, current->plec);
- }
- }
- int main()
- {
- Osoba* first = NULL;
- char imie[100],nazwisko[100];
- printf("\n\n MENU");
- printf("\n\n\n 0 Wyjscie z programu");
- printf("\n 1 Wpisz dane osoby do bazy");
- printf("\n 2 Usun osobe o podanym indeksie");
- printf("\n 3 Usun osobe o podanych imieniu i nazwisku");
- printf("\n 4 Wyswietl osobe o podanym indeksie");
- printf("\n 5 Wyswietl cala liste\n");
- for(;;)
- {
- char option;
- scanf("%s", &option);
- while(getchar() != '\n');
- switch(option)
- {
- case '0' :
- return 0;
- case '1' :
- printf("DODAWANIE OSOBY\n");
- dodajOsobe(&first);
- break;
- case '2':
- printf("USUWANIE OSOBY O PODANYM INDEKSIE\n");
- usun_osobe(&first);
- break;
- case '3':
- printf("USUWANIE OSOBY NA BAZIE IMIENIA I NAZWISKA");
- usun_imnaz_first(imie, nazwisko, &first);
- break;
- case '4':
- printf("WYSWIETL OSObe O PODANYM INDEKSIE\n");
- wypisz_osobe(first);
- break;
- case '5':
- printf("WYSWIETL CALA BAZE\n");
- wypisz_liste(first);
- break;
- default:printf("niepoprawna operacja\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement