C program to implement a linear queue library (st_queue.h) of integers using a static implementation of the queue and implementing the above six operations.

 

Implement a linear queue library (st_queue.h) of integers using a static implementation of the queue and implementing the above six operations. Write a program that includes queue library and calls different queueoperations:


#include<stdio.h>
#include "st_queue.h"

int main()
{
QUEUE q;//create
int choice, num;
initqueue(&q);
do
{
printf("\n1: ADD\n2: REMOVE\n3: PEEK\n3: EXIT");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(isfull(&q))
printf("Overflow");
else
{
printf("Enter the element to add: ");
scanf("%d",&num);
addq(&q,num);
}
break;
case 2:
if(isempty(&q))
printf("Underflow");
else
{
num=removeq(&q);
printf("The removed element is %d", num);
}
break;
case 3:
if(isempty(&q))
printf("Underflow");
else
{
printf("Value: %d",peek(&q));
q.front--;
}
break;
}

}while(choice!=4);
return 0;
}

Output :

1: ADD
2: REMOVE
3: PEEK
3: EXIT
Enter your choice : 1
Enter the element to add: 90

1: ADD
2: REMOVE
3: PEEK
3: EXIT
Enter your choice : 2
The removed element is 66
1: ADD
2: REMOVE
3: PEEK
3: EXIT
Enter your choice : 2
The removed element is 90

Stack Library :

save below code as "st_queue.h" 

#define MAXSIZE 30
typedef struct queue
{
int data[MAXSIZE];
int front,rear;
}QUEUE;
void initqueue(QUEUE *pq)
{
pq->front=pq->rear=-1;
}
int isfull(QUEUE *pq)
{
if(pq->rear==MAXSIZE-1)
return 1;
return 0;
}
int isempty(QUEUE *pq)
{
return(pq->front==pq->rear);
}
void addq(QUEUE *pq, int num)
{
pq->data[++pq->rear]=num;
}
int removeq(QUEUE *pq)
{
int num;
++pq->front;
num=pq->data[pq->front];
return num;
//return pq->data[++pq->front];
}
int peek(QUEUE *pq)
{
    int num;
    ++pq->front;
    num=pq->data[pq->front];
    return num;
}

Post a Comment

2 Comments

Thanks,To visit this blog.