Shared Flashcard Set

Details

Queues and stacks
Queues and stacks
4
Computer Science
Undergraduate 2
12/07/2012

Additional Computer Science Flashcards

 


 

Cards

Term
Enqueue
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
Dequeue
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
EmptyTheQueue
Definition
void EmptyTheQueue(Queue * pq)
{
Item dummy;
while(!QueueIsEmpty(pq))
DeQueue(&dummy, pq);
}
Term
Queue Implementation
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;
Supporting users have an ad free experience!