Linear Queue in Data Structure | Linear Queue | Operations of Queue

In this article linear queue in data structure we give the information about what is linear queue, disadvantage of linear queue and operations of linear queue.

Linear Queue in Data Structure:-

A queue in which elements are added one after the other i.e. the element is inserted in sequential manner.

It is a linear data structure.

The data elements in the linear queue are organized one after the other in sequential order. It is called linear because it looks like a straight line in which the elements are arranged one after the other.

The Linear queue works on the principle of first in first out (FIFO). It is a collection of similar elements in which a new element is added from one end and deleted from the other end.

The performance of this queue is inefficient as compared to circular queue and there is wastage of memory in it

The condition for linear queue is the position of rear is always greater than or equal to front.

Condition: Front <= Rear

15 30 67
Front

Rear

In above example there are two spaces for adding the elements in queue but we cannot add any elements in the queue because rear is at the last position of array.

To overcome this problem the concept of circular queue is used.

It has three components:-

1:- Container of items: – It contains the elements of the queue.

2:- Pointer front: – It points to the first item of the queue.

3:- Pointer rear: – It points to the last item of the queue.

Operations of queue:-

Its operation is as follows:-

1. Initializing Queue.

2. Checking whether the queue is empty or not.

3. Checking whether the queue is full or not.

4. Inserting new elements from rear end.

5. Deleting elements from the front end.

Disadvantage of linear queue:-

A major disadvantage of the linear queue is that we cannot add new items to it, even if there is space in it.

Suppose there are 10 elements in a queue and we have inserted 10 items and after that we deleted some items. Now the queue has space. But in this condition we cannot insert new items in the queue. Due to which memory is damaged in it.

Linear Queue Example:

/* Implementation of linear Queue
This program implements the insert and delete operation in linear queue */
#include<stdio.h>
#include<conio.h>
#define MAX 5
struct queue
{
int front,rear;
int data[MAX];
}p;

void main()
{
int n,ch; // variable declaration
void insert(int); // function declaration
void delete1();
void display();
p.front=p.rear=-1;
clrscr();
while(1)
{
printf(“\n Menu:”);
printf(“\n 1:Insert \n2:Delete \n3:Display \n4:Exit”);
printf(“\n Enter your Choice: “);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
if(IsFull())
printf(“\n Queue Overflow…”);
else
{
printf(“\n Enter number to insert: “);
scanf(“%d”,&n);
insert(n);
}
break;
case 2:
if(IsEmpty())
printf(“\n Queue Underflow…”);
else
delete1();
break;
case 3:
if(IsEmpty())
printf(“\n Queue Underflow…”);
else
display();
break;
case 4:
exit();
default:
printf(“\n Enter correct choice…”);
break;
}
getch();
}
}

int IsFull()
{
if(p.rear==MAX-1)
return(1);
else
return(0);
}

int IsEmpty()
{
if(p.front==-1 || p.front>p.rear)
return(1);
else
return(0);
}

void insert(int no)
{
if(p.front==-1)
p.front=0;
p.rear=p.rear+1;
p.data[p.rear]=no;
printf(“\n %d is inserted into Queue…”,no);
}

void delete1()
{
int temp;
temp=p.data[p.front];
p.front=p.front+1;
printf(“\n %d element is deleted…”,temp);
}

void display()
{
int i;
for(i=p.front;i<=p.rear;i++)
printf(“\t%d”,p.data[i]);
}

OUTPUT:

Menu:

1: Insert

2: Delete

3: Display

4: Exit

Enter your choice: 1

Enter number to insert: 10

10 is inserted into Queue…

Menu:

1: Insert

2: Delete

3: Display

4: Exit

Enter your choice: 1

Enter number to insert: 20

20 is inserted into Queue…

Menu:

1: Insert

2: Delete

3: Display

4: Exit

Enter your choice: 3

10          20

Related Link:

Some More: DBMS/ WT/ DMDW

Santosh Nalawade

Work as Assistant Professor and Web Developer.

One thought on “Linear Queue in Data Structure | Linear Queue | Operations of Queue

Leave a Reply

Your email address will not be published. Required fields are marked *