Advertisement
Guest User

45324234

a guest
Jan 24th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Element { //Tworzę strukturę, która przechowuje
  5. int x; //liczbę całkowitą oraz wskaźnik
  6. struct Element *next; //na kolejną strukturę
  7. };
  8.  
  9. struct Element *beginList = NULL; //Tworzę wskaźnik na strukturę i ustawiam go na NULL
  10.  
  11. void addToList(int numb); //Nagłówki funkcji
  12. void showList();
  13. void removeFirstElement();
  14. void removeLastElement();
  15.  
  16. int main()
  17. {
  18. addToList(10); //Dodaje dla testu kilka elementów do listy
  19. addToList(0);
  20. addToList(8);
  21. addToList(7);
  22. addToList(2);
  23.  
  24. showList(); //Wyświetlam zawartośc listy
  25.  
  26. removeFirstElement();
  27. printf("\n\n");
  28. showList();
  29.  
  30. removeLastElement();
  31. printf("\n\n");
  32. showList();
  33.  
  34. return 0;
  35. }
  36.  
  37. void addToList(int numb){
  38. struct Element *newElement = malloc(sizeof(struct Element)); //Alokuję pamięć na nowy element listy
  39. newElement->x = numb; //Ustawiam liczbę w nowym elemencie listy
  40. if(beginList != NULL){ //Jeżeli wskaźnik na początek listy nie wskazuje na NULL to:
  41. struct Element *help = beginList; //Tworzę pomocniczy wskaźnik na strukturę
  42. while(help != NULL){ //Przechodzę w pętli while przez całą listę
  43. if(help->next == NULL){ //Jeśli wskaźnik help jest ustawiony na ostatni element listy to:
  44. help->next = newElement; //Ustawaim wskaźnik help->next na nowy element listy
  45. newElement->next = NULL; //Oraz wskażnik next nowego elementu ustawiam na NULL
  46. }
  47. help = help->next; //Przestawiam wskaźnik help na kolejny element listy
  48. }
  49. }
  50. else{ //Jeżeli wskaźnik na początek listy wskazuje na NULL (lista jest pusta) to:
  51. beginList = newElement; //Ustawiam globalny wskaźnik na początek listy na nowy element
  52. beginList->next = NULL; //Oraz wskaźnik next nowego elemenyu ustawiam na NULL
  53. }
  54. }
  55.  
  56. void showList(){
  57. struct Element *help = beginList; //Tworzę pomocniczy wskaźnik na strukturę
  58. while(help != NULL){ //Przechodzę w pętli while przez całą listę
  59. printf("%d ",help->x); //W każdym obejściu wypisuje zawartość x w strukturze
  60. help=help->next; //Przestawiam wskaźnik help na kolejny element listy
  61. }
  62. }
  63.  
  64. void removeFirstElement(){
  65. if(beginList != NULL){
  66. struct Element *help = beginList;
  67. beginList = beginList->next;
  68. free(help);
  69. }
  70. else{
  71. printf("Lista jest pusta!");
  72. }
  73. }
  74.  
  75. void removeLastElement(){
  76. if(beginList != NULL){
  77. struct Element *help = beginList;
  78. struct Element *help2 = beginList->next;
  79. while(help != NULL){
  80. if(help2->next == NULL){
  81. free(help2);
  82. help->next = NULL;
  83. }
  84. help = help->next;
  85. help2 = help2->next;
  86. }
  87. }
  88. else{
  89. printf("Lista jest pusta!");
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement