Program in C and C++ to implement queue Using Array . Second Method
C PROGRAM
#include<stdio.h> #include<conio.h> #define size 45 void push(int q[],int *f,int *r,int value) { if(*r==size-1){ printf("\nQueue overflow"); return; } if(*f==-1) *f=0; *r+=1; q[*r]=value; } void pop(int q[],int *f,int *r) { if(*f==-1||*f>*r){ printf("\nQueue underflow"); return; } printf("\nDeleted data is %d",q[*f]); *f++; } void showRev(int q[],int *f,int *r) { int i; if(*f==-1||*f>*r){ printf("\nQueue underflow"); return; } printf("\nEntered queue elements is "); for(i=*r;i>=*f;i--) printf("%d ",q[i]); } void show(int q[],int *f,int *r) { int i; if(*f==-1||*f>*r){ printf("\nQueue underflow"); return; } printf("\nEntered queue elements is "); for(i=*f;i<=*r;i++) printf("%d ",q[i]); } int length(int q[],int *f,int *r) { return (*r-*f+1); } int main() { int queue[size],front,rear,queue1[size],front1,rear1; front=rear=-1; push(queue,&front,&rear,23); push(queue,&front,&rear,45); push(queue,&front,&rear,80); push(queue,&front,&rear,4); push(queue,&front,&rear,8); push(queue,&front,&rear,24); push(queue,&front,&rear,8); show(queue,&front,&rear); printf("\nSize of queue = %d\n",length(queue,&front,&rear)); front1=rear1=-1; push(queue1,&front1,&rear1,23); push(queue1,&front1,&rear1,45); push(queue1,&front1,&rear1,80); push(queue1,&front1,&rear1,4); push(queue1,&front1,&rear1,44); show(queue1,&front1,&rear1); printf("\nSize of queue = %d\n",length(queue1,&front1,&rear1)); return 0; }
C++ PROGRAM
#include<iostream> #include<conio.h> #define size 45 using namespace std; void push(int q[],int *f,int *r,int value) { if(*r==size-1){ cout<<"\nQueue overflow"; return; } if(*f==-1) *f=0; *r+=1; q[*r]=value; } void pop(int q[],int *f,int *r) { if(*f==-1||*f>*r){ cout<<"\nQueue underflow"; return; } cout<<"\nDeleted data is "<<q[*f]; *f++; } void showRev(int q[],int *f,int *r) { int i; if(*f==-1||*f>*r){ cout<<"\nQueue underflow"; return; } cout<<"\nEntered queue elements is "; for(i=*r;i>=*f;i--) cout<<" "<<q[i]; } void show(int q[],int *f,int *r) { int i; if(*f==-1||*f>*r){ cout<<"\nQueue underflow"; return; } cout<<"\nEntered queue elements is "; for(i=*f;i<=*r;i++) cout<<" "<<q[i]; } int length(int q[],int *f,int *r) { return (*r-*f+1); } int main() { int queue[size],front,rear,queue1[size],front1,rear1; front=rear=-1; push(queue,&front,&rear,13); push(queue,&front,&rear,15); push(queue,&front,&rear,86); push(queue,&front,&rear,47); push(queue,&front,&rear,81); push(queue,&front,&rear,34); push(queue,&front,&rear,56); show(queue,&front,&rear); cout<<"\nSize of queue = "<<length(queue,&front,&rear)<<endl; front1=rear1=-1; push(queue1,&front1,&rear1,13); push(queue1,&front1,&rear1,25); push(queue1,&front1,&rear1,30); push(queue1,&front1,&rear1,14); show(queue1,&front1,&rear1); cout<<"\nSize of queue = "<<length(queue1,&front1,&rear1)<<endl; return 0; }
Output :-
Related Programs
- Program in C and C++ to implement queue Using Array .
- Program in C and C++ to implement queue Using Structure and Class.
- Program in C and C++ to implement queue Using Array and Function.
- Program in C++ to implement queue Using Template feature.
0 Comments