Skip to main content

C Program Code for Queue Data Structure

Radhika has the degree of master in computer applications. She has worked as a faculty with Aptech Computer Education Center for sometime.

Elements in a queue are served in the form of First-In-First-Out manner always. A queue data structure adds data to the rear end called enqueue and removes data from the front end called dequeue. In Computer Science, a queue performs the functions of a buffer. Queues are common in computer programs that are coupled with access routines.

C program for queue operations takes data as the data item in the structure named que. The front and rear pointers to the structure point to the first and last elements in the queue respectively. They are initialized to point to the first element of the queue and after creating the queue, the push and pop operations are carried out with the help of the functions push() and pop().

1. Queue creation

Creating a queue function takes the pointer named ptr as the parameter. As elements are added to the rear end of a queue always, pointer rear is changed each time. ptr->data stores the values of the data input and each time data is being read, the next node of pointer rear is linked to the new data. Then, the pointer rear itself is made to point to the new data. The checking value -999 stops the creation of the queue.

C code for creating the queue should be written with care considering all the loopholes. A small mistake can cause runtime errors while executing the program.

Flowchart for the function createque(que *ptr)

Queue creatilon

Queue creatilon

Output

Output of queue creation

Output of queue creation

2. Push Operation

Pushing elements into a queue is done by the function named push(). The pointer to member ptr->data reads the value of the new element that gets pushed into the queue. It is added to the rear end by linking the next node of rear pointer to ptr. Then rear pointer itself is made to point to ptr just like we did while creating the queue.

One thing to notice is front pointer remains unchanged during the creation and during the push operation of a queue.

Flowchart for the function push()

Queue after push operation

Queue after push operation

Scroll to Continue

Output

Queue after push operation

Queue after push operation

3. Pop Operation

Pop operation removes one element from the front end of the queue. The element that gets served first should be removed first. Here front pointer comes into action. Pointer ptr is initialized to front pointer first and then front pointer is linked to its next node. Then the built-in function free(ptr) removes the first element of the queue. Pop operation of a queue is similar to that of a stack.

Flowchart for the function pop()

Queue after pop operation

Queue after pop operation

Output of pop operation

Queue after pop operation

Queue after pop operation

4. Displaying a queue

This function displays the elements of a queue in the same manner as in a linked list or stack. Pointer named cur is initialized to front pointer and the value of cur->data is displayed first. Then cur is incremented and the elements of the queue are displayed one by one by iterating in a loop. When cur reaches NULL value, iteration stops and display function is exited.

Flowchart for display() function

Displaying a queue

Displaying a queue

A queue is an example of a linear data structure or more abstractly a sequential collection. Check-out lines, escalators and ATMs use queues. In each case, the customer at the front of the line is the first one to enter, while at the end of the line is the last to have entered (enqueue). Every time a customer finishes his job, he leaves the queue from the front. This represents the dequeue function.



C program code

#include<stdio.h>
#include<conio.h>
#include<alloc.h>


struct queue
{
int data;
struct queue *next;
};

typedef struct queue que;
que *front, *rear;

void main()

{
int ch;

void createque(que *);
void push();
void pop();
void display();
que *ptr;

clrscr();

printf("\n\n\tCreate a queue(-999 to end):\n\n");
ptr=(que *)malloc(sizeof(que));

front=ptr;
rear=ptr;


createque(ptr);
printf("\n\n\tGiven queue is");
printf("\n\t--------------\n");

display();
clrscr();

display();

do
{
printf("\n\n\t\t\tQueue Operations");
printf("\n\t\t\t----------------");
printf("\n\n\t1. Push");
printf("\n\t2. Pop");
printf("\n\t3. Exit");


printf("\n\n\tEnter your choice:");
scanf("%d", &ch);

switch(ch)

{
case 1:push();
printf("\n\tQueue after push operation is:\n\n");
display();
break;
case 2: pop();
printf("\n\tQueue after pop operation is:\n\n");
display();
break;
case 3: exit(1);

}
clrscr();
display();

}while(ch!=3);

getch();

}


void createque(que *ptr)
{

printf("\n\n\tEnter the data:");
scanf("%d", &ptr->data);

if((ptr->data)!=-999)
{
rear->next=ptr;
rear=ptr;

ptr->next=(que *)malloc(sizeof(que));
createque(ptr->next);
}
else
rear->next=NULL;

}

void push()

{
que *ptr;
ptr=(que *)malloc(sizeof(que));

printf("\n\tEnter the data to push into the queue:");
scanf("%d",&ptr->data);
rear->next=ptr;
rear=ptr;
rear->next=NULL;
}


void pop()
{
que *ptr;
ptr=front;
front=front->next;
free(ptr);
}

void display()
{
que *cur;
printf("\n\t");
cur=front;

while(cur)
{
printf("%d---->",cur->data);
cur=cur->next;
}

getch();
}




  • C program code for stack data structure
    Stacks are memory areas that store function attributes in a Last In First Out manner (LIFO). Whenever a function is called, its parameters and local variables get pushed to the top of the stack for faster execution.
  • C program code for linked list manipulations
    Linked lists are data structures that are user friendly. They not only save memory space, but also reduce the complexity in the manipulation of list elements such as insertion or deletion.
  • About C Programming; An Overview
    Why C has gained this much popularity? It is its portability that differentiates it from other programming languages. A program written in C in one computer system can be transferred to another with minimal changes. This is why the name ‘C’ is most h

Related Articles