C Queue

Summary: in this tutorial, you will learn how to implement the C queue data structure using array.

Introduction to queue data structure

Queue is a collection of objects that are added and removed based on the first-in first-out (FIFO) principle. It means that the first element added to the queue will be first one to be removed from the queue.

Don’t be confused a queue with a stack, because a stack works based on the last-in first-out (LIFO) principle.

A good example of a queue is a line of customers in front of a shop. A new addition to the queue made to the back of the queue, whereas removal from the queue i.e., customer is served happens in the front of the queue. This is called first-come first-served.

There are two main operations in a queue: enqueue and dequeue.

Enqueue: inserts an element into the back of the queue.

Dequeue: removes an element from the front of the queue.

C queue implementation

We can implement the queue data structure in C using array. We add an element to the back of the queue, whereas we remove an element from the front of the queue. Therefore we need two pointers: head and tail to track the front and the back of the queue.

The queue first is empty so we need to initialize the head and tail pointers:

1

2

3

4

voidinit(int*head,int*tail)

{

*head=*tail=0;

}

The queue is empty when the head and the tail pointers point to the same position:

1

2

3

4

intempty(inthead,inttail)

{

returnhead==tail;

}

Whenever we enqueue an element, we increase the tail pointer 1 position:

1

2

3

4

voidenqueue(int*q,int*tail,intelement)

{

q[(*tail)++]=element;

}

When we dequeue an element, we increase the head pointer 1 position until the head pointer reaches the tail pointer i.e., the queue is empty.

1

2

3

4

intdequeue(int*q,int*head)

{

returnq[(*head)++];

}

The queue is full when the tail and the size of the queue are equal.

1

2

3

4

intfull(inttail,constintsize)

{

returntail==size;

}

The following program demonstrates the queue data structure:

queue.h

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

/*

* File : queue.h

* Author : zentut.com

* Purpose: stack header file

*/

#ifndef QUEUE_H_INCLUDED

#define QUEUE_H_INCLUDED

voidinit(int*head,int*tail);

voidenqueue(int*q,int*tail,intelement);

intdequeue(int*q,int*head);

intempty(inthead,inttail);

intfull(inttail,constintsize);

voiddisplay(int*q,inthead,inttail);

#endif // QUEUE_H_INCLUDED

queue.c

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

/*

* File : queue.c

* Author : zentut.com

* Purpose: stack header file

*/

/*

initialize queue pointers

*/

voidinit(int*head,int*tail)

{

*head=*tail=0;

}

/*

enqueue an element

precondition: the queue is not full

*/

voidenqueue(int*q,int*tail,intelement)

{

q[(*tail)++]=element;

}

/*

dequeue an element

precondition: queue is not empty

*/

intdequeue(int*q,int*head)

{

returnq[(*head)++];

}

/*

return 1 if queue is full, otherwise return 0

*/

intfull(inttail,constintsize)

{

returntail==size;

}

/*

return 1 if the queue is empty, otherwise return 0

*/

intempty(inthead,inttail)

{

returntail==head;

}

/*

display queue content

*/

voiddisplay(int*q,inthead,inttail)

{

inti=tail-1;

while(i>=head)

printf("%d ",q[i--]);

printf("\n");

}

main.c

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

/*

* File : main.c

* Author : zentut.com

* Purpose: queue program

*/

#include <stdio.h>

#include <stdlib.h>

#include "queue.h";

intmain()

{

constintSIZE=5;/* queue's size */

inthead,tail,element;

intqueue[SIZE];

init(&head,&tail);

printf("--Enqueue elements--\n");

/* push elements into stack */

while(!full(tail,SIZE))

{

printf("Enter a number to enqueue:");

scanf("%d",&element);

enqueue(queue,&tail,element);

display(queue,head,tail);

}

printf("Queue is full\n\n");

printf("--Dequeue elements --\n");

while(!empty(head,tail))

{

element=dequeue(queue,&head);

printf("dequeue element %d \n",element);

display(queue,head,tail);

}

printf("Queue is empty\n");

return0;

}

The following is the output of the program when we enqueue 1, 2, 3, 4, 5.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

--Enqueue elements--

Enteranumber toenqueue:1

1

Enteranumber toenqueue:2

21

Enteranumber toenqueue:3

321

Enteranumber toenqueue:4

4321

Enteranumber toenqueue:5

54321

Queue isfull

--Dequeue elements--

dequeue element1

5432

dequeue element2

543

dequeue element3

54

dequeue element4

5

dequeue element5

Queue isempty

You can download the C queue implementation project in the following link.