Completely Solved C, C++ Programs Assignment.




C program to implement a queue using an array

Filed Under: ,

Array implementation of Queue Since a queue is also a list, it can be implemented using an array or it can be implemented using a linked representation.When an array is used to implement a queue, then the insert and delete operations are realized using the operations available on an array. The limitation of an array implementation is that the queue cannot grow and shrink dynamically as per the requirement.
Program C program to implement a queue by using an array
#include <stdio.h>
#define MAX 10 /* The maximum size of the queue */
#include <stdlib.h>

void insert(int queue[], int *rear, int value)
{
if(*rear < MAX-1)
{
*rear= *rear +1;
queue[*rear] = value;
}
else
{
printf("The queue is full can not insert a valuen");
exit(0);
}
}

void delete(int queue[], int *front, int rear, int * value)
{
if(*front == rear)
{
printf("The queue is empty can not delete a valuen");
exit(0);
}
*front = *front + 1;
*value = queue[*front];
}
void main()
{
int queue[MAX];
int front,rear;
int n,value;
front=rear=(-1);
do
{
do
{
printf("Enter the element to be insertedn");
scanf("%d",&value);
insert(queue,&rear,value);
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);

printf("Enter 1 to delete an elementn");
scanf("%d",&n);
while( n == 1)
{
delete(queue,&front,rear,&value);
printf("The value deleted is %dn",value);
printf("Enter 1 to delete an elementn");
scanf("%d",&n);
}
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);
}
Output
Enter the element to be inserted
10
Enter 1 to continue
1
Enter the element to be inserted
20
Enter 1 to continue
1
Enter the element to be inserted
30
Enter 1 to continue
0
Enter 1 to delete an element
1
The value deleted is 10
Enter 1 to delete an element
1
The value deleted is 20
Enter 1 to delete an element
0
Enter 1 to continue
1
Enter the element to be inserted
40
Enter 1 to continue
1
Enter the element to be inserted
50
Enter 1 to continue
0
Enter 1 to delete an element
1
The value deleted is 30
Enter 1 to delete an element
1
The value deleted is 40
Enter 1 to delete an element
0
Enter 1 to continue
0

Back to main directory:  Data Structure 


Get Free Programming Tutorials and Solved assignments