[go: up one dir, main page]

0% found this document useful (0 votes)
91 views4 pages

Name: Pranav Ganesh Dasgaonkar Roll No: 70 Class: Secmpn-2 Batch: C-4

This document describes the implementation of a linear queue using an array in C programming language. It defines key queue terminology like front, rear, enqueue, dequeue. It provides algorithms for enqueue and dequeue operations on the queue. It includes a sample program that demonstrates creating a queue with size 5, performing enqueue and dequeue operations, and printing the final queue. The output shows the elements added and removed from the queue.

Uploaded by

shubham chutke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views4 pages

Name: Pranav Ganesh Dasgaonkar Roll No: 70 Class: Secmpn-2 Batch: C-4

This document describes the implementation of a linear queue using an array in C programming language. It defines key queue terminology like front, rear, enqueue, dequeue. It provides algorithms for enqueue and dequeue operations on the queue. It includes a sample program that demonstrates creating a queue with size 5, performing enqueue and dequeue operations, and printing the final queue. The output shows the elements added and removed from the queue.

Uploaded by

shubham chutke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

NAME : PRANAV GANESH DASGAONKAR

ROLL NO : 70

CLASS : SECMPN-2

BATCH : C- 4

TITLE : Implementation of linear queue using array.

THEORY :

You can think of queues as a queue of people at airport ticket counter. The first person
to enter the queue is served by the air hostess at ticket counter first, the last person to
enter is served last. Which is why Queues are called as First in First out
(FIFO) system or Last in Last out system(LILO)

The following are terminologies used in Queue Array implementation –

• Front – The item at the front of the queue is called front item
• Rear – The item at the end of the Queue is called rear item
• Enqueue – Process of adding or inserting a new item in the queue is called as
Enqueing
• Dequeueing – Process of removing or deleting an existing item from the queue
is called as dequeueing
• Size – The max size of the queue is called as size an is initialised when the
queue is created
• currSize – The size of queue at any given time is notated as currSize.
Algorithm for ENQUEUE operation

1. Check if the queue is full or not.


2. If the queue is full, then print overflow error and exit the program.
3. If the queue is not full, then increment the tail and add the element.

Algorithm for DEQUEUE operation

1. Check if the queue is empty or not.


2. If the queue is empty, then print underflow error and exit the program.
3. If the queue is not empty, then print the element at the head and increment the
head.

PROGRAM :
#include<stdio.h>
#define SIZE 5

//Basic value initialisation


int queue[SIZE], front = -1, rear = -1;

//Function created to handle enqueue


void enqueue(int item){
if(rear == SIZE-1){
printf("Can't enqueue as the queue is full\n");
}
else{
//The first element condition
if(front == -1){
front = 0;
}

rear = rear + 1;
queue[rear] = item;
printf("We have enqueued %d\n",item);
}
}

//Function created to handle dequeue


void dequeue(){
if(front == -1){
printf("Can't dequeue as the queue is empty\n");
}
else{
printf("We have dequeued : %d\n", queue[front]);
front = front + 1;
//Only happens when the last element was dequeued
if(front > rear){
front = -1;
rear = -1; }
}
}

//function to print the queue


void printQueue(){
if(rear == -1)
printf("\nUnable to display as queue is empty");
else{
int i;
printf("\nThe queue after enqueue & dequeue ops looks like :");

for(i = front; i <= rear; i++)


printf("%d ",queue[i]);
}
}

int main()
{
//enqueue begins here
enqueue(2);
enqueue(4);
enqueue(6);
enqueue(8);

//dequeue beings here


dequeue();
dequeue();

printQueue();
return 0;
}

OUTPUT:

We have enqueued 2
We have enqueued 4
We have enqueued 6
We have enqueued 8

OUTPUT :

We have dequeued : 2
We have dequeued : 4

The queue after enqueue & dequeue ops looks like :6 8

You might also like