Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct fifo_node
  5. {
  6. int data;
  7. struct fifo_node *next;
  8. };
  9.  
  10. struct fifo_pointers
  11. {
  12. struct fifo_node *head, *tail;
  13. } fifo;
  14.  
  15.  
  16. void enqueue(struct fifo_pointers *fifo, int data)
  17. {
  18. struct fifo_node *new_node =
  19. (struct fifo_node *)malloc(sizeof(struct fifo_node));
  20. if(new_node) {
  21. new_node->data = data;
  22. new_node->next = NULL;
  23. if(fifo->head==NULL) fifo->head = fifo->tail = new_node;
  24. else {
  25. fifo->tail->next=new_node;
  26. fifo->tail=new_node;
  27. }
  28. } else
  29. fprintf(stderr,"Nowy element nie został utworzony!\n");
  30. }
  31. int dequeue(struct fifo_pointers *fifo)
  32. {
  33. if(fifo->head) {
  34. struct fifo_node *tmp = fifo->head->next;
  35. int data = fifo->head->data;
  36. free(fifo->head);
  37. fifo->head=tmp;
  38. if(tmp==NULL)
  39. fifo->tail = NULL;
  40. return data;
  41. }
  42. return -1;
  43. }
  44.  
  45.  
  46.  
  47. void print_queue_req(struct fifo_pointers tmp)
  48. {
  49. if(tmp.head != NULL) return 0;
  50. printf("%d ",tmp.head->data);
  51. fifo.head = tmp.head->next;
  52.  
  53. print_queue_req(tmp);
  54.  
  55. }
  56.  
  57.  
  58. int main(void)
  59. {
  60. int i;
  61. for(i=0;i<20;i++)
  62. enqueue(&fifo,i);
  63.  
  64. print_queue_req(fifo);
  65.  
  66. while(fifo.head)
  67. printf("%d ",dequeue(&fifo));
  68. puts("");
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement