Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Queue Implemented in array
- // C-Program
- #include<stdio.h>
- #include<malloc.h>
- #include<stdlib.h>
- void insert(int *ar,int *front,int *rear,int N){
- int n;
- printf("Enter number to insert :");
- scanf("%d",&n);
- if((*front==0 && *rear== N) || *front==*rear+1){
- printf("overflow condition -> queue is full");
- }
- else if(*front==-1){
- *front=0;
- *rear=0;
- *(ar+(*rear))=n;
- }
- else if(*rear==N && *front !=0){
- *rear=0;
- *(ar+(*rear))=n;
- }
- else{
- *rear=*rear+1;
- *(ar+(*rear))=n;
- }
- }
- int del(int *ar,int *front,int *rear,int N){
- int x;
- if(*front == -1 ){ //queue is empty
- printf("\nUnderflow cond! -> No element in queue to delete");
- }
- else if(*front==*rear){ //only one element in queue
- x=*(ar+(*front));
- *front=-1;
- *rear=-1;
- }
- else if(*front ==N){
- x=*(ar+(*front));
- *front=0;
- }
- else{
- x=*(ar+(*front));
- *front=*front+1;
- }
- return x;
- }
- void display(int *ar,int f,int r,int N){
- printf("\n");
- if(f<=r){
- while(f<=r){
- printf(" %d ",*(ar+f));
- f=f+1;
- }
- }
- else{
- while(f<=N){
- printf(" %d ",*(ar+f));
- f=f+1;
- }
- f=0;
- while(f<=r){
- printf(" %d ",*(ar+f));
- f=f+1;
- }
- }
- printf("\n");
- }
- main(){
- int maxsi,front=-1,rear=-1;
- int ch,x;
- int *ar;
- printf("Enter Max size of Queue : ");
- scanf("%d",&maxsi); // max number of element you wanna put in queue
- ar=(int*)malloc(sizeof(int)*maxsi); //create array of size maxsi
- while(1){
- printf(" 1 : Insert \n 2 : Delete \n 3 : Exit\n Enter your choice :");
- scanf("%d",&ch);
- switch(ch){
- case 1:
- insert(ar,&front,&rear,maxsi-1);
- display(ar,front,rear,maxsi-1);
- break;
- case 2:
- x=del(ar,&front,&rear,maxsi-1);
- printf("\n%d is deleted from queue",x);
- display(ar,front,rear,maxsi-1);
- break;
- case 3:
- exit(0);
- break;
- default:
- printf("Wrong choice try again ??");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement