You are on page 1of 8

INSERTION OF ELEMENTS IN

LINEAR QUEUE USING


ARRAY
NAME : AADARSH SHARMA
DEPT : CSE –A
UNIV ROLL : 1 3 0 0 0 1 2 1 0 0 5
PAPER NAME : DATA STRUCTURES AND
ALGORITHMS
PAPER CODE : PCC-CS-301
❑ TABLE OF
CONTENTS

• INTRODUCTION

• ARRAY REPRESENTATIONOD QUEUE

• ALGORITHM OF INSERTION IN A QUEUE

• SOURCE CODE
❑ INTRODUCTION
• A Queue is a linear structure which follows a particular order in which the
operations are performed. The order is First In First Out (FIFO).

• A good example of a queue is any queue of consumers for a resource


where the consumer that came first is served first.

• The difference between stacks and queues is in removing. In a stack we


remove the item the most recently added; in a queue, we remove the item
the least recently added.

• An array in C is a collective name given to a group of similar variables .


Arrays is a kind of data structure that can store a fixed-size sequential
collection of elements of same type.
❑ Array representation of
Queue

➢ A Queue can easily be represented using linear arrays. There are two variables i.e.
front and rear, that are implemented in the case of every queue. 0 1 2 3 4 5

➢ Front and rear variables point to the position from where insertions and deletions Front= -1

Rear= -1
are performed in a queue. Initially, the value of front and rear is -1 which An empty queue
represents an empty queue.

➢ The figure aside shows an empty stack where both front and rear is -1. After
addition of first element, both front and end should get incremented by 1 i.e set to H
0. 0 1 2 3 4 5

➢ The next figure aside shows an element ‘H’ being inserted at the 0th index. Now,
Front Rear
both the front and the rear has been incremented by 1.
After inserting an element in the queue
➢ The above figure shows the queue of characters forming the English
word "HELLO". Since, No deletion is performed in the queue till now, therefore
the value of front remains 0. However, the value of rear increases by one every
time an insertion is performed in the queue.

➢ After inserting elements into the queue shown in the figure aside, the queue will
look something like following. The value of rear will become 5 while the value of
front remains same.

➢ After deleting an element, the value of front will increase from 0 to 1. However,
the queue will look something like following.
❑ ALGORITHM OF INSERTION IN
A QUEUE

Step 1 : If Rear = MAX -1 Write Stack Overflow Go to End of IF

Step 2 : If Front = -1 and Else


Set Front = Rear = 0
Rear = -1 Set Rear = Rear +1

Step 3 :
Set Queue [ Rear ] = Num

Step 4 : Exit
void insert (int queue[], int max, int front, int rear, int item)
{ ❑ SOURCE CODE
if (rear + 1 == max)
{
printf("overflow");
}
else
{ else
{
if(front == -1 && rear == -1)
rear = rear + 1;
{ }
front = 0; queue[rear]=item;
rear = 0; }
}
}

You might also like