ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming

C Program Code for Queue Data Structure

Updated on August 19, 2017
radhikasree profile image

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

Queue data structure
Queue data structure | Source

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 ‘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 ‘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 | Source

Output

Output of queue creation
Output of queue creation | Source

2. Push Operation

Pushing elements into a queue is done by the function ‘void 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 | Source

Output

Queue after push operation
Queue after push operation | Source

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 | Source

Output of pop operation

Queue after pop operation
Queue after pop operation | Source

4. Displaying a queue

This function displays the elements of a queue in the same manner as in a linked list or stack. Pointer ‘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 | Source

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();
}




Do you find this code easy to follow?

See results

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.