Queue Implementation

Code Id 34
Date Updated 3/7/2010
Title Queue implementation  
Description
Program to implement simple queue using array. 
                                  
Codes Snippet
#include
#define MAX 50

struct queue
{
int front;
int rear;
int queue_arr[MAX];
};
delete_queue(struct queue *);
inesrt_queue(struct queue *, int);
full_queue(struct queue *);
empty_queue(struct queue *);
void display(struct queue *);

main()
{
        int choice,n;
        struct queue q;
        q=(struct queue *)malloc(sizeof(struct queue));
        q->front = q->rear = - 1;
        while(1)
        {
                printf("1.Pushn");
                printf("2.Popn");
                printf("3.Quitn");
                printf("Enter your choice : ");
                scanf("%d",&choice);
                switch(choice)
                {
                 case 1 :
                        if(!full_queue(q))
                        {
                                printf(�n Enter item to be inserted�);
                                scanf(�%d�,&n);
                        insert_queue(q,n);
                }
                else
                {
                        printf(�n Can�t push � queue is full�);
                        exit(1);
                }
                        break;
                 case 2:
                        if(!empty_queue(q))
                        {
                                n=delete_queue(q);
                                printf(�%d is the deleted item�,n);
                        break;
                                }
                else
                {
                        printf(�n Can�t delete � queue is empty�);
                        exit(1);
                }
                                break;
                 case 3:
                        exit(1);
                 default:
                        printf("Wrong choicen");
                }/*End of switch*/
        }/*End of while*/
}/*End of main()*/
full_queue(struct queue *q)
{
        if(q->rear == (MAX-1))
                return 1;
        else
                return 0;
}       
empty_queue(struct queue *q)
{
        if(q->front == -1)
                return 1;
        else
                return 0;
}       
inesrt_queue (struct queue *q, int n)
{
        if(q->front== -1)
                q->front++;
q->rear++;
        q->queue_arr[q->rear] = n;
return 0;
}/*End of inesrt_queue */
delete_queue ()
{
        int n;
n= q->queue_arr[q->front];
if(q->front==q->rear)
{
        front = rear = -1;
        return n;
}
        q->front++;
        return n;
}/*End of delete_queue ()*/

Comments are closed.