Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "fifo_fun.h"
  3.  
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <stdlib.h>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. Queue* QCreate()
  12. {
  13. Queue* q = (Queue*)calloc(1, sizeof(Queue));
  14. return q;
  15. }
  16. int QEmpty( Queue* q ) // Sprawdza, czy kolejka jest pusta
  17. {
  18. return !(q->pHead);
  19. }
  20. void QEnqueue( Queue* q, int x ) // Włóż do kolejki
  21. {
  22. tagQItem* tmp = (tagQItem*)malloc(sizeof(tagQItem));
  23. tmp->nKey = x;
  24. if(q->pTail != NULL)
  25. q->pTail->pNext = tmp;
  26. q->pTail = tmp;
  27. if(q->pHead == NULL)
  28. q->pHead = q->pTail;
  29. printf("We have added %d to queue.\n", x);
  30. }
  31. int QDequeue( Queue* q ) // Wyrzuć z kolejki
  32. {
  33. if( !q )
  34. {
  35. printf("ERROR\n");
  36. return 3;
  37. }
  38. if(QEmpty(q))
  39. {
  40. printf("Queue is empty!\n");
  41. return INT_MIN;
  42. }
  43. tagQItem* tmp = (tagQItem*)calloc(1, sizeof(tagQItem));
  44. if( !tmp )
  45. {
  46. printf("ERROR");
  47. return 3;
  48. }
  49. int ret = q->pHead->nKey;
  50. tmp = q->pHead;
  51. q->pHead = tmp->pNext;
  52. printf("We have dequeued %d from queue.\n", ret);
  53. free(tmp);
  54. return ret;
  55.  
  56. /*tagQItem* tmp = q->pHead;
  57. q->pHead = q->pHead->pNext;
  58. int ret = tmp->nKey;
  59. printf("We have dequeued %d from queue.\n", ret);
  60. free(tmp);
  61. return ret;*/
  62. }
  63. void QClear( Queue* q ) // Czyści wszystko, zostawia kolejkę jako strukturę
  64. {
  65. while(!QEmpty(q))
  66. QDequeue(q);
  67. printf("Everything from queue was deleted but not structure!\n");
  68. }
  69. void QDel( Queue* q ) // Usunięcie całej kolejki
  70. {
  71. QClear(q);
  72. free(q);
  73. q = NULL;
  74. printf("Queue was deleted with structure!\n");
  75. }
  76.  
  77.  
  78. void QWrite( Queue* q )
  79. {
  80. QItem* tmp = q->pHead;
  81. printf("Queue has the form: ");
  82. while(tmp != q->pTail->pNext)
  83. {
  84. printf("%d ", tmp->nKey);
  85. tmp = tmp->pNext;
  86. }
  87. printf("\n");
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement