C program Insert and Delete Elements in Queue with Algorithm

Algorithm :-

(A) Algorithm to insert an element in a queue.

Algorithm: PUSH(s,rear,x)

Step 1: [Check for overflow]
	if(rear>=N-1)
	Write (“Queue is overflow.”)
	Exit.
Step 2: [Increment rear pointer]
	rear=rear+1
Step 3: [Write the value]
	s[rear] <- x
Step 4: [Increment front pointer]
	If(front==-1)
        then front <- 0
Step 5: [Finished]
	Exit.

 

(B) Algorithm to delete an element in a queue.

Algorithm: POP(s,front,x)

Step 1: [Check for underflow]
	if(front<0)
	then write (“Queue is underflow.”)
	Exit.
Step 2: [Store value at front in x]
	x <- s[front]
Step 3: [Increment front pointer]
	if(front==rear)
        then
        front <- -1
        rear <- -1
        else
	front <- front+1
Step 4: [Display deleted element]
	Write x.
Step 5: [Finished]
	Exit.

 

Program :-

#include
#include
void push();
void pop();
void display();
int front=-1,rear=-1,x,s[10];
void main()
{
	int choice;
	clrscr();
	printf("Select choice from the menu.");
	printf("\n\t1. for Push.");
	printf("\n\t2. for Pop.");
	printf("\n\t3. for Display.");
	printf("\nEnter your choice:");
	scanf("%d",&choice);
	do
	{
		switch(choice)
		{
			case 1:push();
			break;
			case 2:pop();
			break;
			case 3:display();
			break;
			default:exit(0);
		}
		printf("\n\nSelect choice from the menu.");
		printf("\n\t1. for Push.");
		printf("\n\t2. for Pop.");
		printf("\n\t3. for Display.");
		printf("\n\t0. for Exit.");
		printf("\nEnter your choice:");
		scanf("%d",&choice);
	}while((choice>0)||(choice<=3)); getch(); } void push() { if(rear>=9)
	{
		printf("Queue is overflow.");
	}
	else
	{
		printf("Enter value to insert:");
		scanf("%d",&x);
		rear=rear+1;
		s[rear]=x;
		if(front==-1)
		front=0;
	}
}
void pop()
{
	if(front<0) 
        { 
            printf("Queue is underflow."); 
        } 
        else 
        { 
            x=s[front]; 
            if(front==rear)
            {
                front=-1; rear=-1;
            } 
            else
            { 
                front=front+1;
            } 
            printf("Deleted Element:%d",x); 
       } 
} 
void display() 
{ 
       int i;
       if(front==-1) 
       { 
            printf("Queue is underflow."); 
       }  
       else 
       { 
            printf("Queue:\n"); 
            for(i=rear;i>=front;i--)
	    {
		printf("%d,",s[i]);
	    }
	}
}

Output :-

Select choice from the menu.
        1. for Push.
        2. for Pop.
        3. for Display.
Enter your choice:1
Enter value to insert:5

Select choice from the menu.
        1. for Push.
        2. for Pop.
        3. for Display.
        0. for Exit.
Enter your choice:1
Enter value to insert:10

Select choice from the menu.
        1. for Push.
        2. for Pop.
        3. for Display.
        0. for Exit.
Enter your choice:1
Enter value to insert:15

Select choice from the menu.
        1. for Push.
        2. for Pop.
        3. for Display.
        0. for Exit.
Enter your choice:2
Deleted Element:5

Select choice from the menu.
        1. for Push.
        2. for Pop.
        3. for Display.
        0. for Exit.
Enter your choice:3
Queue:
15,10,

Select choice from the menu.
        1. for Push.
        2. for Pop.
        3. for Display.
        0. for Exit.
Enter your choice:0

Leave a Reply

Please enter your comment!
Please enter your name here

*