forked from shriyajalana/programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorial.cpp
More file actions
87 lines (79 loc) · 1.8 KB
/
Copy pathfactorial.cpp
File metadata and controls
87 lines (79 loc) · 1.8 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
// this is the implementation of Circular Queue data structure
// we have to increment/decrement in a circular manner:: (i+1)%n
// current_position=i;
// next_position= (i+1)%n;
// previous_position=(i+n-1)%n (rear+5-front)%5+1
#include<iostream>
int arr[5];
int front=-1;
int rear=-1;
bool Isempty(){
return (front==-1 && rear==-1) ? true : false;
}
bool full(){
return ((rear+1)%5==front) ? true : false;
}
void Enqueue(int x){
if(full()){
std:: cout<<"queue is full\n";
return;
}
if(Isempty()){
front=0;
rear=0;
arr[rear]=x;
}
else{
rear=(rear+1)%5;
arr[rear]=x;
}
}
void Dequeue(){
if(Isempty()){
std::cout<<"c queue is empty\n";
return;
}
if(front==rear){
std:: cout<<"Dequeue element is: "<<arr[front];
front=-1;
rear=-1;
}
else{
std:: cout<<"Dequeue element is: "<<arr[front]<<std::endl;
front=(front+1)%5;
}
}
void head(){
(Isempty())? std:: cout<<"cqueue is empty\n" : std::cout<<arr[front]<<std::endl;
}
void qprint(){
int count = (rear+5-front)%5 + 1;
std::cout<<"Queue : ";
for(int i = 0; i <count; i++)
{
int index = (front+i) % 5; // Index of element while travesing circularly from front
std::cout<<arr[index]<<" ";
}
std::cout<<"\n";
}
int main(){
Enqueue(3);
Enqueue(5);
Enqueue(7);
qprint();
Enqueue(9);
qprint();
Dequeue();
Enqueue(12);
Dequeue();
qprint();
Enqueue(51);
Enqueue(12);
qprint();
Isempty();
full();
head();
Enqueue(12);
Dequeue();Dequeue();Dequeue();Dequeue();Dequeue();Dequeue();
return 0;
}