Advertisement
Guest User

Untitled

a guest
May 28th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct tnode {
  5. char value;
  6. struct tnode *next;
  7. } node;
  8.  
  9. node *dodaj_na_poczatek(node *head, char val) {
  10. node *temp = malloc(sizeof(struct tnode));
  11. if (!temp) {
  12. printf("Brak pamieci !\n");
  13. return head;
  14. }
  15. temp->value = val;
  16. temp->next = head;
  17. return temp;
  18. }
  19.  
  20. node *dodaj_rosnaco(node *head, char val) {
  21. node *elem = malloc(sizeof(node));
  22. if (!elem)
  23. return head;
  24. elem->value = val;
  25. elem->next = NULL;
  26. if (head) {
  27. if (head->value > val) {
  28. elem->next = head;
  29. head = elem;
  30. } else {
  31. node *p = head;
  32. for (; p->next && p->next->value < val; p = p->next);
  33. elem->next = p->next;
  34. p->next = elem;
  35. }
  36. } else {
  37. head = elem;
  38. }
  39. return head;
  40. }
  41.  
  42. void wypisz_liste(const node *wsk) {
  43. if (!wsk) {
  44. printf("Lista jest pusta.\n");
  45. return;
  46. }
  47. printf("head -> ");
  48. while (wsk != NULL) {
  49. printf("[ %c ] -> ", wsk->value);\
  50. wsk = wsk->next;
  51. }
  52. printf("NULL\n");
  53. }
  54.  
  55. node *usun_wszystkie(node *wsk) {
  56. while (wsk) {
  57. node *next = wsk->next;
  58. free(wsk);
  59. wsk = next;
  60. }
  61. return NULL;
  62. }
  63.  
  64. int main(void) {
  65. node *head = NULL;
  66.  
  67. head = dodaj_rosnaco(head, 'z');
  68. head = dodaj_rosnaco(head, 'j');
  69. head = dodaj_rosnaco(head, 'a');
  70. head = dodaj_rosnaco(head, 'k');
  71. puts("Przed dodaniem \'w\':");
  72. wypisz_liste(head);
  73.  
  74. head = dodaj_rosnaco(head, 'w');
  75. puts("Po dodaniu \'w\':");
  76. wypisz_liste(head);
  77.  
  78. usun_wszystkie(head);
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement