Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define ROZMIAR 5
  6.  
  7. struct fifo_node
  8. {
  9. int data;
  10. struct fifo_node *next;
  11. };
  12.  
  13. struct fifo_pointers
  14. {
  15. struct fifo_node *head, *tail;
  16. }fifo;
  17.  
  18. void enqueue(struct fifo_pointers *fifo, int data) ///dodawanie elementu do kolejki ( na koniec - ogon)
  19. {
  20. struct fifo_node *new_node = (struct fifo_node *)malloc(sizeof(struct fifo_node));
  21. if(new_node)
  22. {
  23. new_node -> data = data;
  24. new_node -> next = NULL;
  25. if(fifo -> head == NULL)
  26. fifo->head=fifo->tail=new_node;
  27. else {
  28. fifo->tail->next = new_node ;
  29. fifo->tail = new_node;
  30. }
  31. }
  32. else
  33. printf("Nowy element nie zostal utworzony");
  34.  
  35. }
  36.  
  37. int dequeue(struct fifo_pointers *fifo) ///usuwanie elementu z kolejki ( z poczatku - glowa)
  38. {
  39. if(fifo->head)
  40. {
  41. struct fifo_node *tmp = fifo->head->next;
  42. int data = fifo->head->data;
  43. free(fifo->head);
  44. fifo->head = tmp;
  45. if(tmp == NULL)
  46. fifo->tail= NULL;
  47. return data;
  48.  
  49. }
  50. return -1;
  51. }
  52.  
  53. int show(struct fifo_pointers fifo)
  54. {
  55. if(fifo.head)
  56. {
  57. printf(" %d ", fifo.head->data);
  58. fifo.head = fifo.head->next;
  59. show(fifo);
  60. }
  61. else
  62. return 0;
  63. }
  64.  
  65. int main ()
  66. {
  67.  
  68. int i;
  69. for(i=1;i<=5;i++)
  70. enqueue(&fifo,i);
  71.  
  72. printf(" Elementy w kolejka(rekurencja): \t");
  73. show( fifo);
  74.  
  75. printf("\n Elementy usuniete: \t");
  76. while(fifo.head)
  77. printf(" %d ", dequeue(&fifo));
  78. puts(" ");
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement