-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0516043_5.h
107 lines (105 loc) · 1.58 KB
/
0516043_5.h
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
100
101
102
103
104
105
106
107
#ifndef CircularDeque
#define CircularDeque
using namespace std;
class CirDeque{
private:
int data[10];
int front, rear;
public:
CirDeque(){rear = front = 9;};
void push_front(int); // push an element to the front of the deque
void push_back(int); // push an element to the end of the deque
int pop_front(); // pop an element from the front of the deque
int pop_back(); // pop an element from the end of the deque
friend ostream& operator<<(ostream&, const CirDeque&);
// print the elements in queue from front to rear and separate each element by a space.
};
// you may write your code below this line
void CirDeque::push_front(int n)
{
int k;
k=this->front-1;
if(k<0)
{
k=9;
}
if(k==this->rear)
{
cout<<"queue is full"<<endl;
}
else
{
this->front=k;
this->data[k]=n;
}
return;
}
void CirDeque::push_back(int n)
{
int k;
k=this->rear+1;
if(k>9)
{
k=0;
}
if(k==this->front)
{
cout<<"queue is full"<<endl;
}
else
{
this->data[this->rear]=n;
this->rear=k;
}
return;
}
int CirDeque::pop_front()
{
if(front!=rear)
{
int n=data[front];
front++;
if(front>9)
{
front=0;
}
return(n);
}
else
{
cout<<"deque is empty"<<endl;
return(0);
}
}
int CirDeque::pop_back()
{
if(front!=rear)
{
rear--;
if(rear<0)
{
rear=9;
}
int n=data[rear];
return(n);
}
else
{
cout<<"deque is empty"<<endl;
return(0);
}
}
ostream& operator<<(ostream& c, const CirDeque& d);
{
for(int i=d.front;i!=d.rear;i++)
{
if(i>9)
{
i=0;
}
c<<d.data[i];
}
return(c);
}
// you may write your code above this line
#endif