Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define ROZ 20
- struct dane
- {
- char imie[ROZ];
- char nazwisko[ROZ];
- int rokuro;
- };
- struct elem
- {
- struct dane dane;
- struct elem * next;
- };
- void wypisanieelementu(struct elem * wsk)
- {
- if(wsk != NULL)
- printf("%s%s%i \n",(*wsk).dane.imie,(*wsk).dane.nazwisko,(*wsk).dane.rokuro);
- }
- struct elem * dodaj_poczatek(struct elem * head, struct dane * wpis)
- {
- struct elem * new = malloc(sizeof(struct elem));
- (*new).dane = *wpis;
- (*new).next = head;
- return new;
- }
- struct elem * dodaj_koniec(struct elem * head, struct dane * wpis)
- {
- if(head == NULL)
- return dodaj_poczatek(head, wpis);
- struct elem * new = malloc(sizeof(struct elem));
- (*new).dane = *wpis;
- struct elem * tmp = head;
- while((*tmp).next != NULL)
- tmp = (*tmp).next;
- (*tmp).next = new;
- return head;
- }
- struct elem * dodaj_n(struct elem * head, struct dane * wpis, int n)
- {
- if(n == 0 || head == NULL)
- return dodaj_poczatek(head, wpis);
- struct elem * tmp = head;
- while(tmp != NULL && n > 1)
- {
- tmp = (*tmp).next;
- n--;
- }
- struct elem * new = malloc(sizeof(struct elem));
- (*new).dane = *wpis;
- (*new).next = (*tmp).next;
- (*tmp).next = new;
- return head;
- }
- int search_rokuro(struct elem * head, int rokuro)
- {
- int idx = 0;
- while(head != NULL)
- {
- if((*head).dane.rokuro==rokuro)
- return idx;
- idx++;
- head = (*head).next;
- }
- return -1;
- }
- int search_nazw(struct elem * head, char * nazwisko)
- {
- int idx = 0;
- while(head != NULL)
- {
- if(!strcmp((*head).dane.nazwisko, nazwisko))
- return idx;
- idx++;
- head = (*head).next;
- }
- return -1;
- }
- // Drugi sposob zwracania glowy
- void usun_poczatek(struct elem ** head)
- {
- struct elem * newhead = (*(*head)).next;
- free(*head);
- *head = newhead;
- }
- void usun_koniec(struct elem * head)
- {
- struct elem * tmp = head;
- while((*(*tmp).next).next != NULL) // biorę przedostani i w jego nexicie wpisuję null, po czym zwalniam poprzednie ostatni.//
- tmp = (*tmp).next;
- free((*tmp).next);
- (*tmp).next = NULL;
- }
- int usun_n(struct elem * head, int n)
- {
- int i;
- struct elem * tmp = head;
- for(i = 1; i < n ; i++)
- {
- if((*tmp).next == NULL)
- return 0;
- tmp = (*tmp).next;
- }
- struct elem * next = (*(*tmp).next).next;
- free((*tmp).next);
- (*tmp).next = next;
- return 1;
- }
- void wypisanie(struct elem * head)
- {
- while(head != NULL)
- {
- wypisanieelementu(head);
- head = (*head).next;
- }
- }
- int main()
- {
- struct elem * first=NULL;
- int w=-1;
- for(;w!=0;)
- {
- printf("wybierz co chcesz zrobić \n");
- printf("1 - wypisz listę \n");
- printf("2 - dodaj element na poczatek \n");
- printf("3 - dodawanie nowego elementu na końcu listy \n");
- printf("4 - wyszukiwanie elementów wg. roku urodzenia \n");
- printf("5 - dodawanie nowego elementu w dowolnym miejscu listy \n");
- printf("6 - usuwanie elementu z poczatku listy \n");
- printf("7 - usuwanie elementu z końca listy \n");
- printf("8 - usuwanie elementu z dowolnego miejsca listy \n");
- printf("9 - usuwanie całej listy \n");
- printf("0 - koniec \n");
- scanf("%i",&w);
- getchar();
- if(w!=1&&w!=2&&w!=3&&w!=4&&w!=5&&w!=6&&w!=7&&w!=8&&w!=9&&w!=0)
- printf("wybrano zly numer \n");
- else
- {
- if(w==1)
- {
- wypisanie(first);
- }
- if(w==2)
- {
- struct dane wpis;
- printf("wpisz imie ");
- fgets(wpis.imie,ROZ,stdin);
- printf("wpisz nazwisko ");
- fgets(wpis.nazwisko,ROZ,stdin);
- printf("wpisz rok urodzenia ");
- scanf("%i",&wpis.rokuro);
- getchar();
- first = dodaj_poczatek(first,&wpis);
- }
- if(w==3)
- {
- struct dane wpis;
- printf("wpisz imie ");
- fgets(wpis.imie,ROZ,stdin);
- printf("wpisz nazwisko ");
- fgets(wpis.nazwisko,ROZ,stdin);
- printf("wpisz rok urodzenia ");
- scanf("%i",&wpis.rokuro);
- getchar();
- first = dodaj_koniec(first,&wpis);
- }
- if(w==4)
- {
- int c;
- printf("wpisz rok urodzenia ");
- scanf("%i",&c);
- getchar();
- int res = search_rokuro(first,c);
- if(res == -1)
- printf("Nie znaleziono elementu\n");
- else
- printf("Pierwszy element z danym rokiem urodzenia ma index: %d\n", res);
- }
- if(w==5)
- {
- struct dane wpis;
- printf("wpisz imie ");
- fgets(wpis.imie,ROZ,stdin);
- printf("wpisz nazwisko ");
- fgets(wpis.nazwisko,ROZ,stdin);
- printf("wpisz rok urodzenia ");
- scanf("%i",&wpis.rokuro);
- getchar();
- printf("podaj ktorym miejscu chcesz dodac nowy \n");
- int n;
- scanf("%i",&n);
- getchar();
- first = dodaj_n(first, &wpis, n);
- }
- if(w==6)
- {
- usun_poczatek(&first);
- }
- if(w==7)
- {
- usun_koniec(first);
- }
- if(w==8)
- {
- printf("podaj ktory element chcesz usunąć \n");
- int o;
- scanf("%i",&o);
- getchar();
- usun_n(first,o);
- }
- if(w==9)
- {
- while(first != NULL){
- usun_poczatek(&first);
- }
- }
- if(w==0)
- {
- break;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement