Advertisement
Guest User

lab3

a guest
Apr 24th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Osoba
  5. {
  6. char Nazwisko[20];
  7. char Imie[20];
  8. char Pesel[12];
  9. struct Osoba *Nastepny;
  10. };
  11.  
  12. struct Osoba *pobierz_dane(void) {
  13. char nazwisko[20];
  14. char imie[20];
  15. char pesel[20];
  16. struct Osoba * nowy = (struct Osoba *)malloc(sizeof(struct Osoba));
  17. printf("Podaj nazwisko: ");
  18. scanf("%s", nowy->Nazwisko);
  19. printf("Podaj imie: ");
  20. scanf("%s", nowy->Imie);
  21. printf("Podaj pesel: ");
  22. scanf("%s", nowy->Pesel);
  23. nowy->Nastepny = 0;
  24. return nowy;
  25. };
  26.  
  27. struct Osoba *wstaw_do_listy(struct Osoba *lista, struct Osoba *element) {
  28. if(lista == 0){
  29. lista = element;
  30. return lista;
  31. }
  32. if(strcmp(element->Nazwisko, lista->Nazwisko) < 0){
  33. element->Nastepny = lista;
  34. lista = element;
  35. return lista;
  36. }
  37.  
  38. if(lista->Nastepny == 0){
  39. lista->Nastepny = element;
  40. return lista;
  41. }
  42.  
  43. struct Osoba * glowa = lista;
  44. while(glowa->Nastepny){
  45. if((strcmp(element->Nazwisko,glowa->Nazwisko) >= 0) && (strcmp(element->Nazwisko,glowa->Nastepny->Nazwisko) <= 0)){
  46. struct Osoba * pom = glowa->Nastepny;
  47. element->Nastepny = pom;
  48. glowa->Nastepny = element;
  49. return lista;
  50. }
  51. glowa = glowa->Nastepny;
  52. }
  53. //jesli doszlismy do tego etapu, to znaczy, element powinien zostac wstawiony na sam koniec listy
  54. glowa->Nastepny = element;
  55. return lista;
  56. };
  57.  
  58. void wyswietl_liste(struct Osoba *lista){
  59. while(lista != 0){
  60. printf("Nazwisko: %s\n",lista->Nazwisko);
  61. printf("Imie: %s\n",lista->Imie);
  62. printf("Pesel: %s\n",lista->Pesel);
  63. lista = lista->Nastepny;
  64. }
  65. }
  66.  
  67. void zwolnij_pamiec(struct Osoba *lista) {
  68. while(lista){
  69. struct Osoba * pom = lista->Nastepny;
  70. free(lista);
  71. lista = pom;
  72. }
  73. }
  74.  
  75. int main()
  76. {
  77. struct Osoba * glowa = 0;
  78. for(int i = 0; i < 5; i ++){ //wpisywanie 5 osob do listy
  79. glowa = wstaw_do_listy(glowa, pobierz_dane());
  80. }
  81. //glowa = wstaw_do_listy(glowa, doDodania);
  82. wyswietl_liste(glowa);
  83. zwolnij_pamiec(glowa);
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement