-
Notifications
You must be signed in to change notification settings - Fork 1
/
revN.cpp
84 lines (63 loc) · 1.32 KB
/
revN.cpp
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
// g++ revN.cpp -o revN && ./revN
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node* next;
public:
node(int d){
data = d;
next = NULL;
}
};
node *start = NULL, *last = NULL;
void insert(int data){
node* temp = new node(data);
if(start == NULL)
start = last = temp;
else{
last->next = temp;
last = temp;
}
}
void dis(){
node* temp = start;
while(temp != NULL){
cout << temp->data << " ";
temp = temp->next;
}
}
void rev_helper(node* pre, node* its){
if(its->next == NULL){
start = its;
its->next = pre;
}
else{
rev_helper(its, its->next);
its->next = pre;
}
}
void revN(int n){
node *p = start, *q = start, *q_1;
for(int i = 0; i < n-1; i++) q = q->next;
q_1 = q->next;
q->next = NULL;
rev_helper(NULL,start);
p->next = q_1;
}
int main(){
int flag = 1, dtemp;
cout << "to insert press 1 to revN press 2 to display press 3" << endl;
do{
cin >> flag;
if(flag == 1){
cin >> dtemp;
insert(dtemp);
}
else if(flag == 2){
cin >> dtemp;
revN(dtemp);
}
else if(flag == 3) dis();
}while(flag != 0);
}