Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "fifo2.h"
- #include <memory>
- Queue* QCreate()
- {
- Queue* p = (Queue*)calloc(1,sizeof(Queue));
- if( !p )
- {
- printf("can't alloc memory");
- return NULL;
- }
- QItem* w = (QItem*)calloc(1, sizeof(QItem));
- if(!w)
- {
- printf("can't alloc memory");
- return NULL;
- }
- p->pHead = p->pTail = w;
- return p;
- }
- int QisEmpty(Queue* q)
- {
- return !(q->pHead->pNext);
- }
- void QEnQueue(Queue* q, int x)
- {
- QItem* p = (QItem*)calloc(1, sizeof(QItem)); //warunek czy przydzielono pamiec
- if( !p )
- {
- printf("nie znaleziono wolnej panieci WYNIK ERROR");
- return;
- }
- q->pTail->pNext = p; //dodaje nowe pudelko
- q->pTail = p; //ptail wskazuje na ost element
- p->nKey = x; //daje wartosc ost elem
- }
- int QDecQueue(Queue* q)
- {
- if( !q )
- {
- printf("ERROR:Queue is not even created");
- return 5;
- }
- if( QisEmpty(q) )
- {
- printf("ERROR:Queue is empty");
- return 5;
- }
- QItem* p = q->pHead->pNext;
- int x = p->nKey;
- q->pHead->pNext = p->pNext;
- free(p);
- if( QisEmpty(q) )
- q->pTail = q->pHead; //!!! wyzej sprawdzam warunek
- return x; //return .. q->pHead->pNext->nKey
- }
- void QClear(Queue* q)
- {
- if( !q )
- {
- printf("ERROR:Queue is not even created");
- return;
- }
- if(QisEmpty(q) )
- {
- printf("can't clear queue -->queue has been already cleared \n");
- return;
- }
- while( !QisEmpty(q) )
- QDecQueue(q);
- }
- void QDel(Queue** q) //!!!!!
- {
- if( !*q )
- {
- printf("ERROR:Queue is not even created");
- return;
- }
- QClear(*q);
- free(*q);
- *q = NULL;
- }
- /*
- //////////////////QEnQUEUE!
- QItem* p = (QItem*)calloc(1, sizeof(QItem)); //warunek czy przydzielono pamiec
- if( !p )
- {
- printf("nie znaleziono wolnej panieci WYNIK ERROR");
- return;
- }
- //if( !q->pHead->pNext )
- q->pTail->pNext = p; //dodaje nowe pudelko
- // else
- //q->pHead->pNext = p; //make first item
- q->pTail = p; //ptail wskazuje na ost element
- p->nKey = x; //daje wartosc ost elem
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement