/* insertion and deletion in a circular queue array implementation */ # include<stdio.h> # include<string.h> # include # include<stdlib.h> # define size 6 int l, u; /* l means lower bound u means upper bound */ int rear, front; int ch; int q[size]; int rear = -1; int front = -1; void insert_queue(); void delete_queue(); void display_queue(); /* function to create queue */ void insert_queue() { if ((front == 0) && (rear == size-1)) { printf("\n overflow"); rear = 1; return; } else if (front < 0) /* insert first element */ { front = 0; rear = 0; printf("\ninput the element :"); scanf("%d", &ch); q[rear] = ch ; } else if (rear == size-1) { rear = 0; printf("\n input the element :"); scanf("%d", &ch); q[rear] = ch ; } else { rear ++; printf("\n input the element :"); scanf("%d", &ch);
q[rear] = ch ; } } /* function to perform delete operation */ void delete_queue() { if (front < 0) { printf("\nunderflow"); return ; } ch = q[front]; q[front] = null; printf("\n element deleted :", ch); if(front == rear) { front = -1; rear = -1; } else if ( front == size-1) { front = 0; } else { front++ ; } } /* output function */ void display_queue() { int i; if (front < 0) return; if ( rear >= front) { for(i = front; i <= rear; i++) { printf("\n i = %d", i); printf(" %d ", q[i]); } } else { for(i = front; i < size; i++) { printf("\n i = %d", i); printf(" %d", q[i]); } for(i = 0; i <= rear; i++)
{ printf("\n i = %d", i); printf(" %d ", q[i]); } } } /* function main */ void main() { int k = 0; char choice; do { printf("\ninsert->i delete->d quit->q:"); printf("\ninput the choice : "); do { choice = getchar(); choice = tolower(choice); }while(strchr("idq",choice)==null); printf("your choice is: %c",choice); switch(choice) { case 'i' : insert_queue(); printf("\nqueue after inserting "); display_queue(); break; case 'd' : delete_queue(); printf("\nqueue content after deleteion is as follows:\n"); display_queue(); break; case 'q': k = 1; } } while(!k); } /* insertion and deletion in a queue array implementation */ # include<stdio.h> # include<string.h> # include # include<stdlib.h> # define size 10
int rear, front; int ch; int q[size]; int rear = 0; int front = 0; void insert_queue(); void delete_queue(); void display_queue(); /* function to create queue */ void insert_queue() { printf("\n input the element :"); scanf("%d", &ch); if(rear < size) { rear ++; q[rear] = ch ; if(front == 0) front = 1; } else printf("\n queue is overflow"); } /* function to perform delete operation */ void delete_queue() { if (front == 0) { printf("\n underflow"); return ; } else { ch = q[front]; printf("\n element deleted : %d", ch); } if(front == rear) { front = 0; rear = 0; } else front = front + 1; } /* output function */ void display_queue() //char q[]) { int i;
if (front == 0) return; for(i = front ; i <= rear; i++) printf(" %d ", q[i]); } /* function main */ void main() { int k = 0; char choice; do { printf("\ninsert->i delete->d quit->q:"); printf("\ninput the choice : "); do { choice = getchar(); choice = tolower(choice); }while(strchr("idq",choice)==null); printf("your choice is: %c ", choice); switch(choice) { case 'i' : insert_queue(); printf("\nqueue after inserting "); display_queue(); break; case 'd' : delete_queue(); printf("\nqueue content after deleteion is as follows:\n"); display_queue(); break; case 'q': k = 1; } } while(!k); }