-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreverseQ.cpp
More file actions
68 lines (64 loc) · 1.2 KB
/
Copy pathreverseQ.cpp
File metadata and controls
68 lines (64 loc) · 1.2 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
#include<bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node *next;
}*front = NULL, *rear = NULL;
int n = 0;
void append(int data){
Node *temp = front;
Node *newNode = new Node;
newNode->next = NULL;
newNode->data = data;
if(front == NULL){
front = rear = newNode;
n++;
}
else{
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
rear = newNode;
n++;
}
}
void print(){
Node *node = front;
while(node != NULL){
cout << node->data << " ";
node = node->next;
}
cout << endl;
}
void reverse(){
Node *prev = NULL;
Node *curr = front;
Node *next;
while(curr != NULL){
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
rear = front;
front = prev;
}
int main(){
int data;
do{
cin >> data;
if(data>=0)
append(data);
}while(data>=0);
if(n==0){
cout << "Queue is empty";
return 0;
}
cout << "Before reversing:" << endl;
print();
reverse();
cout << "After reversing:" << endl;
print();
return 0;
}