-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueue_Reverse.cpp
More file actions
109 lines (96 loc) · 1.62 KB
/
Queue_Reverse.cpp
File metadata and controls
109 lines (96 loc) · 1.62 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
100
101
102
103
104
105
106
107
108
109
/*
Given an integer K and a queue of integers, we need to reverse the order of the first K elements of the queue, leaving the other elements in the same relative order.
Input Format
5
1 2 3 4 5
3
Constraints
Total number of integers should be 5
Complete the provided function modify Queue that takes queue and k as parameters and returns a modified queue.
Output Format
3 2 1 4 5
Sample Input 0
5
5 4 3 2 1
4
Sample Output 0
2 3 4 5 1
*/
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int *queue=NULL;
int *stack=NULL;
int front=-1,rear=-1,top=-1,n;
void enqueue(int x)
{
{
if(front==-1)
{
front=0;
rear=0;
}
else if(rear==n-1)
{
rear=0;
}
else
{
rear++;
}
queue[rear]=x;
}
}
int deqeue()
{
int deleted=queue[front];
{
if(front == rear)
{
front = -1; rear = -1;
}
else if(front == 4) front = 0;
else front = front +1;
}
return deleted;
}
void push(int x)
{
top =top+1;
stack[top] = x;
}
int pop()
{
int deleted = stack[top];
top=top-1;
return deleted;
}
int main()
{
cin>>n;
queue = new int[n];
for(int i=0;i<n;i++)
{
int data;
cin>>data;
enqueue(data);
}
int k;
cin>>k;
stack = new int[k];
while(front<k)
{
push(deqeue());
}
while(top!=-1)
{
enqueue(pop());
}
for(int i=0;i<n;i++)
{
cout<<queue[i]<<" ";
}
}