Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Circular Queue
- #include<conio.h>
- #include<stdio.h>
- #define MAX 3
- //Globar Variable
- int queue [MAX];
- int front = -1,rear = -1;
- //UDF Function
- void insert();
- int Delete_Element();
- void display();
- void main()
- {
- int op, val;
- clrscr();
- do
- {
- getch();
- clrscr();
- printf("\n -------------------------");
- printf("\n -----Circular Queue------");
- printf("\n -------------------------");
- printf("\n 1. Insert");
- printf("\n 2. Delete");
- printf("\n 3. Display");
- printf("\n 4. Exit");
- printf("\n -------------------------");
- printf("\n Maximum 3 Value Get");
- printf("\n -------------------------");
- printf("\n Enter Choice : ");
- scanf("%d", &op);
- printf(" -------------------------");
- switch(op)
- {
- case 1:
- insert();
- break;
- case 2:
- val=Delete_Element();
- if(val !=-1)
- {
- printf("\n The Number Delete is : %d", val);
- }
- break;
- case 3:
- display();
- break;
- }
- }while(op != 4);
- getch();
- }
- //UDF Function Declaration
- void insert()
- {
- int num;
- printf("\n Enter The Number to be Insert in The Queue : ");
- scanf("%d", &num);
- if(front == 0 && rear == MAX -1 || front == MAX -1 && rear == front -1)
- {
- printf("\n Circular Queue is Overflow");
- return;
- }
- else
- {
- if(front == -1 && rear == -1)
- {
- front = rear = 0;
- queue[rear]=num;
- }
- else if(rear==MAX-1 && front!=0)
- {
- rear=0;
- queue[rear]=num;
- }
- else
- {
- rear++;
- queue[rear]=num;
- }
- }
- }
- int Delete_Element()
- {
- int val;
- if(front == -1 && rear == -1)
- {
- printf("\n Circular Queue is Underflow");
- return -1;
- }
- val = queue [front];
- if(front == rear)
- {
- front = rear = -1;
- }
- else
- {
- if(front == MAX -1)
- {
- front=0;
- }
- else
- {
- front++;
- }
- }
- return val;
- }
- void display()
- {
- int i;
- printf("\n ");
- if(front == -1 && rear == -1)
- {
- printf("\n Circular Queue is Empty");
- }
- else
- {
- if(rear>=front)
- {
- for(i=front; i<=rear; i++)
- {
- printf("\n Element : %d", i);
- printf(" -> %d", queue[i]);
- }
- }
- else
- {
- for (i=front; i<MAX; i++)
- {
- printf("\n Element : %d", i);
- printf(" -> %d", queue[i]);
- }
- for (i=0; i<rear; i++)
- {
- printf("\n Element : %d", i);
- printf(" -> %d", queue[i]);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment