Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 4) WAP to implement Queue
- #include <iostream>
- using namespace std;
- const int QueueSpace=10;
- int rear=-1;
- int front=0;
- int Queue[QueueSpace];
- void push(int data){
- if(rear==(QueueSpace-1)){
- cout<<"Queue over flowed!!!"<<endl;
- return ;
- }
- Queue[++rear] = data;
- }
- void pop(){
- if(front==rear+1){
- cout<<"Queue under flowed!!!"<<endl;
- return ;
- }
- front++;
- }
- void peek(){
- if(front==rear+1){
- cout<<"Queue is empty!!!"<<endl;
- return ;
- }
- cout<<"Current front element of Queue is: "<<Queue[front]<<endl;
- }
- void size(){
- cout<<"Current Queue size: "<<rear-front+1<<endl;
- }
- void printQueue(){
- cout<<"Current Queue elements: ";
- for(int i=front;i<=rear;i++){
- cout<<Queue[i]<<" ";
- }
- cout<<endl;
- }
- int main(){
- push(10);
- push(20);
- push(30);
- push(40);
- peek();
- size();
- printQueue();
- pop();
- peek();
- size();
- printQueue();
- return 0;
- }
- /*
- OUTPUT:
- Current front element of Queue is: 10
- Current Queue size: 4
- Current Queue elements: 10 20 30 40
- Current front element of Queue is: 20
- Current Queue size: 3
- Current Queue elements: 20 30 40
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement