Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define ILOSC 5
  4. float wynik;
  5.  
  6. struct fifo_node
  7. {
  8. int data;
  9. struct fifo_node *next,*previous;
  10. };
  11.  
  12. struct fifo_pointers
  13. {
  14. struct fifo_node *head, *tail,*prev;
  15. }fifo;
  16.  
  17.  
  18. void enqueueFront(struct fifo_pointers *fifo, int data)
  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. new_node->previous=NULL;
  26. if(fifo->head==NULL)
  27. fifo->head = fifo->tail = fifo->prev = new_node;
  28. else
  29. {
  30. fifo->tail->next=new_node;
  31. fifo->tail=new_node;
  32.  
  33. }
  34. } else
  35. fprintf(stderr,"Nowy element nie zosta³ utworzony!\n");
  36. }
  37.  
  38. void enqueueBack(struct fifo_pointers *fifo, int data)
  39. {
  40. struct fifo_node *new_node = (struct fifo_node *)malloc(sizeof(struct fifo_node));
  41. if(new_node)
  42. {
  43. new_node->data = data;
  44. new_node->next = NULL;
  45. new_node->previous=NULL;
  46. if(fifo->head==NULL)
  47. fifo->head = fifo->tail = fifo->prev = new_node;
  48. else
  49. {
  50. fifo->head->previous =new_node;
  51. new_node->next=fifo->head;
  52. fifo->head=new_node;
  53. }
  54. } else
  55. fprintf(stderr,"Nowy element nie zosta³ utworzony!\n");
  56. }
  57. int dequeueBegin(struct fifo_pointers *fifo)
  58. {
  59. if(fifo->head) {
  60. struct fifo_node *tmp = fifo->head->next;
  61. int data = fifo->head->data;
  62. free(fifo->head);
  63. fifo->head=tmp;
  64. if(tmp==NULL)
  65. fifo->tail = NULL;
  66.  
  67. return data;
  68. }
  69. return -1;
  70. }
  71.  
  72.  
  73.  
  74. void print_queue(struct fifo_pointers fifo)
  75. {
  76. while(fifo.head)
  77. {
  78. printf("%d ",fifo.head->data);
  79. fifo.head = fifo.head->next;
  80. }
  81. puts("");
  82. }
  83. void print_queue_with_for(struct fifo_pointers fifo)
  84. {
  85. for(;fifo.head;fifo.head=fifo.head->next)
  86. printf("%d ",fifo.head->data);
  87. puts("");
  88. }
  89.  
  90. int main(void)
  91. {
  92.  
  93. int liczba;
  94. int i;
  95. enqueueBack(&fifo,4);
  96. enqueueBack(&fifo,5);
  97. enqueueFront(&fifo,3);
  98. enqueueFront(&fifo,8);
  99. enqueueBack(&fifo,3);
  100.  
  101. while(fifo.head)
  102. {
  103. printf("%d",dequeueBegin(&fifo));
  104. }
  105.  
  106.  
  107.  
  108.  
  109. puts("");
  110.  
  111. return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement