queues15 - Data Structures: Queues and Trees Queues A queue...

This preview shows pages 1–4. Sign up to view the full content.

Queues A queue is a list from which items may be deleted at one end (the front or head) of the list and into which items may be inserted at the other end (the rear or tail). A queue is similar to a checkout line at the grocery store – first come first served. Unlike a stack which is LIFO a queue is FIFO. Queues have many applications in computer systems ranging from process/job scheduling to printer spooling to packet processing in networks. Primitive operations defined for a queue are: o enqueue(q, x) which inserts item x at the rear/tail of queue q . o dequeue(q, x) which removes item x from the front/head of queue q . o isEmpty(q) which returns true if queue q is empty and false otherwise. Example: enqueue(q, ‘A’); enqueue(q, ’B’); enqueue(q, ‘C’); x = dequeue(q); enqueue(q, ‘D’); enqueue(q, ‘E’); x = dequeue(q); Queues and Trees - 1 Data Structures: Queues and Trees A B C front rear front rear B C D E

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Linked List Implementation Just like the stack, we keep additional pointers to allow access to the structure. In the case of the queue, we need two additional pointers, one to the front and one to the read of the queue. Queues and Trees - 2 A B C D front rear struct queueNode{ char data; struct queueNode * next; }; struct queue{ struct queueNode *front; struct queueNode *rear; }; void enqueue( struct queue *q, char value) { struct queueNode *newptr; newptr = malloc(sizeof(struct queueNode)); if (newptr != NULL) { newptr->data = value; newptr->next = NULL; if (isEmpty(*q)) q->front = newptr; else q->rear->next = newptr; q->rear = newptr; } else printf(“%c” is not inserted. No memory available.\n”, value); } void dequeue( struct queue *q) { char value; struct queueNode *tempptr; value = q->front->data; tempptr = q->front; q->front = q->fron->next; if (q->front == NULL) q->rear = NULL; free(tempptr); return value; }
Binary Trees A binary tree is a data structure that is made up of nodes and pointers, much in the same way that a linked list is structures. The difference between them lies in how they are organized. A linked list represents a linear or predecessor/successor relationship

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 10

queues15 - Data Structures: Queues and Trees Queues A queue...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online