Advertisement
meraxes

Linked lists

Oct 25th, 2017
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define EXIT_SUCCESS 0
  6. #define ALLOCATION_ERROR -1
  7. typedef struct osoba *position;
  8. typedef struct osoba
  9. {
  10. char ime[20];
  11. char prezime[20];
  12. int god_rod;
  13. position next;
  14. }_OSOBA;
  15.  
  16. position CreateNode(position);
  17. position Insert(position);
  18. position InsertLast(position);
  19. void Print(position);
  20. position FindLast(position);
  21. position FindbyName(position);
  22. void Delete(position);
  23. position FindBefore(position head, position p);
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27. position head;
  28. head = NULL;
  29. int n;
  30.  
  31. puts("Broj clanova liste:");
  32. scanf("%d", &n);
  33. getchar();
  34. while (n > 0)
  35. {
  36. head = Insert(head);
  37. n--;
  38. }
  39.  
  40.  
  41. Print(head);
  42.  
  43. Delete(head);
  44.  
  45. Print(head);
  46.  
  47. return EXIT_SUCCESS;
  48. }
  49. position CreateNode(position p)
  50. {
  51. position q;
  52.  
  53. q = NULL;
  54. q = (position)malloc(sizeof(_OSOBA));
  55.  
  56. if (q == NULL)
  57. exit(-1);
  58. return q;
  59. }
  60. position Insert(position p)
  61. {
  62. position q;
  63.  
  64. q = CreateNode(p);
  65. q->next = p;
  66.  
  67. puts("Ime:");
  68. fgets(q->ime, 50, stdin);
  69. puts("Prezime:");
  70. fgets(q->prezime, 50, stdin);
  71. puts("Godina rodenja:");
  72. scanf("%d", &(q->god_rod));
  73. getchar();
  74.  
  75.  
  76.  
  77. return q;
  78.  
  79.  
  80. }
  81. void Print(position p)
  82. {
  83. while (p != NULL)
  84. {
  85. puts(p->ime);
  86. puts(p->prezime);
  87. printf("%d", p->god_rod);
  88.  
  89. printf("\n");
  90.  
  91. p = p->next;
  92. }
  93. return;
  94. }
  95. position InsertLast(position p)
  96. {
  97. position last, q;
  98. last = FindLast(p);
  99. q = Insert(NULL);
  100. last->next = q;
  101. return p;
  102. }
  103. position FindLast(position p)
  104. {
  105. while (p->next != NULL)
  106. p = p->next;
  107.  
  108. return p;
  109. }
  110. position FindbyName(position p)
  111. {
  112. char prez[50];
  113. puts("Trazeno prezime:");
  114. fgets(prez, 50, stdin);
  115. while (p != NULL)
  116. {
  117. if (strcmp(p->prezime, prez) == 0)
  118. {
  119. printf("Nadeno je prezime: %s", p->prezime);
  120. }
  121. p = p->next;
  122.  
  123. }
  124.  
  125. return p;
  126. }
  127. //position FindBefore(position p)
  128. //{
  129. // position q = NULL;
  130. //
  131. // q = FindbyName(p);
  132. //
  133. // while (p->next != NULL && p->next != q)
  134. // {
  135. // p = p->next;
  136. //
  137. // }
  138. // return p;
  139. //}
  140. void Delete(position p)
  141. {
  142. position q;
  143.  
  144. q = FindbyName(p);
  145.  
  146. while (p->next != NULL && p->next != q)
  147. {
  148. p = p->next;
  149.  
  150. }
  151.  
  152. p->next = q->next;
  153. free(q);
  154.  
  155. return;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement