The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Data Structures - Linear Queues

A queues which are all represented using array is said to be Linear queue. Only finite amount of elements can be inserted into a linear queue. To insert an element 47 in a linear queue, then rear value of the linear queue will be incremented by one to place a value 47 in its last position. If we need to delete an element from the queue, we are left with no other option except a first element in an array.

C program - Linear Queues

Here is the program to demonstrate Linear Queues.

linear-queues.c
#include <stdio.h>
#include <malloc.h>
#define MAX 10
int queue[MAX];
int front = -1, rear = -1;
void insert(void);
int delete_element(void);
int peek(void);
void display(void);
int main()
{
int choice, val;
do {
printf("\n1. Insert an element into a queue ");
printf("\n2. Delete an element from a queue ");
printf("\n3. Peek an element form a queue ");
printf("\n4. Display the queue ");
printf("\n5. EXIT");
printf("\n\n\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
val = delete_element();
if(val != -1);
printf("\n The number deleted is : %d", val);
break;
case 3:
val = peek();
if(val != -1);
printf("\n The first value in queue is : %d", val);
break;
case 4:
display();
break;
}
} while(choice != 5);
return 0;
}
void insert() {
int num;
printf("\n Enter the element to be inserted into the queue : ");
scanf("%d", &num);
if(rear == MAX-1)
printf("\n OVERFLOW");
else if(front == -1 && rear == -1)
front = rear = 0;
else
rear++;
queue[rear] = num;
}
int delete_element()
{
int val;
if(front == -1 || front > rear)
{
printf("\n UNDERFLOW");
return -1;
}
else {
val = queue[front];
front++;
if(front > rear)
front = rear = -1;
return val;
}
}
int peek()
{
if(front == -1 || front > rear)
{
printf("\n QUEUE IS EMPTY");
return -1;
}
else {
return queue[front];
}
}
void display()
{
int i;
printf("\n");
if(front == -1 || front > rear )
printf("\n QUEUE IS EMPTY");
else {
for(i = front;i <= rear ; i++)
printf("\t %d", queue[i]);
}
}
1. Insert an element into a queue
2. Delete an element from a queue
3. Peek an element form a queue 
4. Display the queue
5. EXIT
Enter your choice : 1
Enter the element to be inserted into the queue : 1

1. Insert an element into a queue
2. Delete an element from a queue
3. Peek an element form a queue
4. Display the queue
5. EXIT
Enter your choice : 1
Enter the element to be inserted into the queue : 2

1. Insert an element into a queue
2. Delete an element from a queue
3. Peek an element form a queue
4. Display the queue
5. EXIT
Enter your choice : 1
Enter the element to be inserted into the queue : 3

1. Insert an element into a queue
2. Delete an element from a queue
3. Peek an element form a queue
4. Display the queue
5. EXIT
Enter your choice : 4
1 2 3 

1. Insert an element into a queue
2. Delete an element from a queue
3. Peek an element form a queue
4. Display the queue
5. EXIT
Enter your choice : 5

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report

We to update you