-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprint_even_in QUEUE.cpp
More file actions
99 lines (88 loc) · 1.9 KB
/
print_even_in QUEUE.cpp
File metadata and controls
99 lines (88 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Write a program to create the queue of N elements . Size of the queue should be greater than 2 and less than equal to 15. If the size of the queue will not be in the mentioned range then it should display the message “Invalid Queue range" with taking any elements for input.And if there is no even element present then display "No even element is there".
Input Format
First Input will represent the number of elements in the queue
Second Input insert the elements(Enqueue operation).
Constraints
Size (N) of the queue should be 2 < N <= 15.
Output Format
Your program should display the even elements of the queue . But if the size will be invalid or if the even elements are not there then display the mentioned message
Sample Input 0
3
2
3
4
Sample Output 0
2 4
*/
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n;
int *queue;
int front=-1,rear=-1;
void enqueue(int x)
{
if(front==-1)
{
front=0;
rear=0;
}
else if(rear==n-1)
{
rear=0;
}
else
{
rear++;
}
queue[rear]=x;
}
int dequeue()
{
int deleted=queue[front];
if(front ==n-1)
{
front=0;
}
else{
front=front+1;
}
return deleted;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int check=0;
cin>>n;
if(n>2 && n<=15)
{
queue=new int[n];
for(int i=0;i<n;i++)
{
int x;
cin>>x;
enqueue(x);
}
for(int i=0;i<n;i++)
{
int y=dequeue();
if(y%2==0)
{
cout<<y<<" ";
check=1;
}
}
if(check==0)
{
cout<<"No even element is there";
}
}
else
{
cout<<"Invalid Queue range";
}
return 0;
}