Powered By Blogger

Sunday, April 15, 2012

Program : 20 (a) --> linear queue using Arrays.


 Linear Queue using Arrays

#include<stdio.h>
#include<conio.h>
#define MAX 5
int Q[20];
int f=0,r=0;
void enQ();
void deQ();
void display();
int menu();
main()
{
                int ch;
                clrscr();
                do
                {
                                ch=menu();
                                switch(ch)
                                {
                                                case 1:enQ();
                                                       break;
                                                case 2:deQ();
                                                                break;
                                                case 3:display();
                                                                break;
                                                case 4:exit(1);
                                }
                }while(1);
                getch();
}
int menu()
{
                int ch;
                printf("menu \n");
                printf("1.enqueue \n");
                printf("2.dequeue \n");
                printf("3.display \n");
                printf("4.exit \n");
                printf("enter your choice:\n");
                scanf("%d",&ch);
                return(ch);
}
void enQ()
{
                int data;
                if(r==MAX)
                {
                                printf("queue is full");
                }
                else
                {
                                printf("enter data \n");
                                scanf("%d",&data);
                                Q[r]=data;
                                r++;
                                printf("element is enqueued");
                 }
}
void deQ()
{
                if(f==MAX)
                {
                                printf("queue is empty");
                }
                else
                {
                                printf("deleted element is %d",Q[f]);
                                f++;
                }
}
void display()
{
                int i;
                if(f==MAX)
                {
                                printf("queue is empty");
                }
                else
                {
                                for(i=f;i<r;i++)
                                {
                                                printf("%d",Q[i]);
                                }
                } 
  }

No comments:

Post a Comment