Term
|
Definition
int EnQueue(Item item, Queue * pq) { Node * pnew; if(QueueIsFull(pq)) return 0; pnew = (Node *) malloc(sizeof(Node)); if(pnew == NULL) { printf("Unlable to allocate memory!\n"); exit(1); } CopyToNode(item, pnew); pnew->next = NULL; if(QueueIsEmpty(pq)) pq->front = pnew; else pq->rear->next = pnew; pq->rear = pnew; pq->items++; return 1; } void CopyToNode(Item item, Node * pn) { pn->item = item; } |
|
|
Term
|
Definition
int DeQueue(Item * pitem, Queue * pq) { Node * pt; if(QueueIsEmpty(pq)) return 0; CopyToItem(pq->front,pitem); pt=pq->front; pq->front = pq->front->next; free(pt); pq->items--; if(pq->items == 0) pq->rear = NULL; return 1; } void CopyToItem(Node * pn, Item * pitem) { *pitem = pn->item; } |
|
|
Term
|
Definition
void EmptyTheQueue(Queue * pq) { Item dummy; while(!QueueIsEmpty(pq)) DeQueue(&dummy, pq); } |
|
|
Term
|
Definition
int main(void) { Queue line; Item temp; char ch; InitializeQueue(&line); puts("Testing the Queue interface. Type a to add a value,"); puts("type d to delete a value, and type q to quit."); while((ch = getchar ())!='q') { if(ch != 'a' && ch != 'd') continue; if(ch == 'a') { printf("Integer to add:"); scanf("%d", &temp); if(!QueueIsFull(&line)) { printf("Putting %d into queue\n", temp); EnQueue(temp, &line); } else puts("Queue is full!"); } else { if(QueueIsEmpty(&line)) puts("Nothing to delete!"); else { DeQueue(&temp, &line); printf("Removing %d from queue\n", temp); } } printf("%d items in queue\n", QueueItemCount(&line)); puts("Type a to add, d to delete, q to quit:"); } EmptyTheQueue(&line); puts("Bye!"); return 0; |
|
|