C Tutorial – Queues

In this C programming language tutorial we take a quick look at what a queue is and how to implement a queue using C.

What is a Queue?

Almost everybody knows printer queues. A print queue is a list where from where print jobs (an item) are used and then deleted from one end (front). At the other end of the queue new print jobs (or items are inserted (rear). It is also referred to as a first-in-first-out (FIFO) data structure.

So in short on one side you put things in and from the other side you take things out.

The Queue Source Code

Below you’ll find the queue source code example for this tutorial. You should note that there is no input checkin done in this example, so only use the numbers from one to four.


#include <stdio.h>
#include <stdlib.h>

#define QUEUESIZE 10
int queue[QUEUESIZE], f=0, r=-1;

// Check if queue is full
int queuefull() {
	if(r == QUEUESIZE - 1) {
		return 1;
	}
	return 0;
}

// Check if the queue is empty
int queueempty() {
	if(f > r) {
		return 1;
	}
	return 0;
}

// Show queue content
int queueshow() {
	int i;
	if(queueempty()) {
		printf(" \n The queue is empty\n");
	} else {
		printf("Start->");
		for(i=f; i<=r; i++) {
			printf("%d ", queue[i]);
		}
		printf("<-End");
	}
	return 0;
}

// Perform an insert operation.
int queueinsert(int oneelement) {
	if( queuefull()) { 
		printf("\n\n Overflow!!!!\n\n");
	} else {
		++r;
		queue[r] = oneelement;
	}
	return 0;
}

// Perform a delete operation
int queuedelete() {
	int elem;
	if(queueempty()) {
		printf(" \n The queue is empty\n");
		return(-1);
	} else {
		elem=queue[f];
		f=f+1;
		return(elem);
	}
}

int main() {
 	int option, element; 
 	char    ch;

 	do {
 	printf("\n Press 1-Insert, 2-Delete, 3-Show, 4-Exit\n");
 	printf("\n Your selection? ");
 	scanf("%d",&option);
 	switch(option) {
 		case 1:
 			printf("\n\nContent to be Inserted?");
 			scanf("%d",&element);
			queueinsert(element);
 			break;
 		case 2:
			element=queuedelete();
			if( element != -1 ) {
				printf("\n\nDeleted element (with content %d) \n",element); 
			}				
			break;
		case 3:
			printf("\n\nStatus of the queue\n\n");
			queueshow();
			break;
		case 4:
			printf("\n\n Ending the program \n\n");
			break; 
		default:
			printf("\n\nInvalid option, please retry! \n\n");
			break;
		}
    } while(option != 4);
    return 0; 
}

We start the queue example by defining a queue length of 10 (items). The first helper function is show if our queue if full or not. The second helper function is to check if the queue is empty or not. The third function is to show the content of the queue (the items). The fourth function is to add (insert) an item to our queue. The fifth function can be used to delete an item from our queue.

In the main we use a do-while loop (until four is pressed) to get user input with scanf and a switch statement to switch between our options. We will explain only the insert option, the rest of the functions use the same concept.

If the insert option is chosen then we get user input to insert integer value. Then the queuinsert() function is called. The queue insert function check if the queue is full or not. The variable r is raised with one and the user input is added to our queue (array).

That’s all for this quick tutorial about queue’s using arrays.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

Comments are closed.