Program of queue using array

To represent(create) a queue we require a one dimensional array of some maximum size(int queue_arra[MAX];)  to hold the data items and two other variable (int rear = -1;int front = -1;) to point to the beginning and the end of the queue.

 

# include<stdio.h>

# define MAX 5

int queue_arra[MAX];

int rear = -1;

int front = -1;

main()

{

                int choice;

                while(1)

                {

                                printf("1.Insert\n");

                                printf("2.Delete\n");

                                printf("3.Display\n");

                                printf("4.Quit\n");

                                printf("Enter your choice : ");

                                scanf("%d",&choice);

                                switch(choice)

                                {

                                case 1 :

                                                insert();

                                                break;

                                case 2 :

                                                del();

                                                break;

                                case 3:

                                                display();

                                                break;

                                case 4:

                                                exit(1);

                                default:

                                                printf("Wrong choice\n");

                                }/* switch end*/

                }/* while end*/

}/* main() end*/

insert()

{

                int added_itemm;

                if (rear==MAX-1)

                                printf("Queue Overflow\n");

                else

                {

                                if (front==-1) 

                                                front=0;

                                printf("Pls Insert the element  : ");

                                scanf("%d", &added_itemm);

                                rear=rear+1;

                                queue_arra[rear] = added_itemm ;

                }

}/* insert() end*/

del()

{

                if (front == -1 || front > rear)

                {

                                printf("Queue Underflow\n\n");

                                return ;

                }

                else

                {

                                printf("Element deleted from queue is : %d\n", queue_arra[front]);

                                front=front+1;

                }

}/* del() end */

display()

{

                int i;

                if (front == -1)

                                printf("Queue is empty\n");

                else

                {

                                printf("Queue is :\n");

                                for(i=front;i<= rear;i++)

                                                printf("%d  ",queue_arra[i]);

                                printf("\n");

                }

}/*End of display() */

Output......

Queue with array

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @