Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. #include "fifo2.h"
  2. #include <memory>
  3.  
  4. Queue* QCreate()
  5. {
  6.   Queue* p = (Queue*)calloc(1,sizeof(Queue));
  7.   if( !p )
  8.   {
  9.     printf("can't alloc memory");
  10.     return NULL;
  11.  }
  12.   QItem* w = (QItem*)calloc(1, sizeof(QItem));
  13.   if(!w)
  14.   {
  15.     printf("can't alloc memory");
  16.     return NULL;
  17.   }
  18.   p->pHead = p->pTail = w;
  19.   return p;
  20. }
  21. int QisEmpty(Queue* q)
  22. {
  23.     return !(q->pHead->pNext);
  24. }
  25. void QEnQueue(Queue* q, int x)
  26. {
  27.   QItem* p = (QItem*)calloc(1, sizeof(QItem));     //warunek czy przydzielono pamiec
  28.   if( !p )
  29.   {
  30.     printf("nie znaleziono wolnej panieci WYNIK ERROR");
  31.     return;
  32.   }
  33.  q->pTail->pNext = p;      //dodaje nowe pudelko
  34.   q->pTail = p;    //ptail wskazuje na ost element
  35.   p->nKey = x;      //daje wartosc ost elem
  36.  
  37. }
  38. int QDecQueue(Queue* q)
  39. {
  40.   if( !q )
  41.   {
  42.     printf("ERROR:Queue is not even created");
  43.     return 5;
  44.   }
  45.   if( QisEmpty(q) )
  46.   {
  47.     printf("ERROR:Queue is empty");
  48.     return 5;
  49.   }
  50.   QItem* p = q->pHead->pNext;
  51.  
  52.   int x = p->nKey;
  53.   q->pHead->pNext = p->pNext;
  54.   free(p);
  55.   if( QisEmpty(q) )
  56.     q->pTail = q->pHead;  //!!!               wyzej sprawdzam warunek
  57.  
  58.   return x;                       //return .. q->pHead->pNext->nKey
  59. }
  60. void QClear(Queue* q)
  61. {
  62.   if( !q )
  63.   {
  64.     printf("ERROR:Queue is not even created");
  65.     return;
  66.   }
  67.   if(QisEmpty(q) )
  68.   {
  69.     printf("can't clear queue -->queue has been already cleared \n");
  70.     return;
  71.   }
  72.   while( !QisEmpty(q) )
  73.     QDecQueue(q);
  74. }
  75. void QDel(Queue** q)                  //!!!!!
  76. {
  77.  if( !*q )
  78.  {
  79.    printf("ERROR:Queue is not even created");
  80.    return;
  81.  }
  82.     QClear(*q);
  83.     free(*q);
  84.   *q = NULL;
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. /*
  95. //////////////////QEnQUEUE!
  96. QItem* p = (QItem*)calloc(1, sizeof(QItem));     //warunek czy przydzielono pamiec
  97. if( !p )
  98. {
  99.   printf("nie znaleziono wolnej panieci WYNIK ERROR");
  100.   return;
  101. }
  102. //if( !q->pHead->pNext )
  103. q->pTail->pNext = p;      //dodaje nowe pudelko
  104.                           //  else
  105.                           //q->pHead->pNext = p;      //make first item
  106.  
  107. q->pTail = p;    //ptail wskazuje na ost element
  108. p->nKey = x;      //daje wartosc ost elem
  109.  
  110. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement