Program in C and C++ to implement queue Using Array .
C PROGRAM
#include<stdio.h> #include<conio.h> #define size 45 int front=-1; int rear=-1; int q[size]; void push(int n) { if(rear==size-1){ printf("\nQueue overflow"); return; } if(front==-1) front=0; rear+=1; q[rear]=n; } void pop() { if(front==-1||front>rear){ printf("\nQueue underflow"); return; } printf("\nDeleted data is %d",q[front]); front++; } void showRev() { int i; if(front==-1||front>rear){ printf("\nQueue underflow"); return; } printf("\nEntered queue elements is "); for(i=rear;i>=front;i--) printf("%d ",q[i]); } void show() { int i; if(front==-1||front>rear){ printf("\nQueue underflow"); return; } printf("\nEntered queue elements is "); for(i=front;i<=rear;i++) printf("%d ",q[i]); } int length() { return (rear-front+1); } int main() { push(23); push(45); push(80); push(4); push(8); push(24); push(78); show(); printf("\nSize of queue = %d\n",length()); return 0; }
C++ PROGRAM
<>#include<iostream> #include<conio.h> #define size 45 using namespace std; int front=-1; int rear=-1; int q[size]; void push(int n) { if(rear==size-1){ cout<<"\nQueue overflow"; return; } if(front==-1) front=0; rear+=1; q[rear]=n; } void pop() { if(front==-1||front>rear){ cout<<"\nQueue underflow"; return; } cout<<"\nDeleted data is "<<q[front]; front++; } void showRev() { int i; if(front==-1||front>rear){ cout<<"\nQueue underflow"; return; } cout<<"\nEntered queue elements is "; for(i=rear;i>=front;i--) cout<<" "<<q[i]; } void show() { int i; if(front==-1||front>rear){ cout<<"\nQueue underflow"; return; } cout<<"\nEntered queue elements is "; for(i=front;i<=rear;i++) cout<<" "<<q[i]; } int length() { return (rear-front+1); } int main() { push(12); push(56); push(2); push(6); push(90); push(67); push(15); show(); cout<<"\nSize of queue = "<<length()<<endl; return 0; }
Output :-
0 Comments