-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathReverseDLL.cpp
187 lines (125 loc) · 2.68 KB
/
ReverseDLL.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include<iostream>
using namespace std;
struct Node {
int data;
Node *next,*prev;
};
//function to reverse a doubly linked list using recursion and return the new head
Node *reverseRec(Node *head)
{
Node *temp;
if(!head) return NULL;
//first swap the next and prev pointers
temp=head->next;
head->next = head->prev;
head->prev = temp;
//if prev becomes NULL, this means we have reversed the list and reached end
if(head->prev==NULL)
{
return head;
}
//otherwise simply keep swapping next nd prev until we reach the end and prev becomes NULL
return reverseRec(head->prev);
}
int maxOccuringItem(Node *head)
{
if(head==NULL) return -1;
Node *temp=head;
Node *temp1;
while(temp!=NULL)
{
temp1 = temp;
while(temp1->next!=NULL)
{
if(temp->data==temp1->next->data)
{
return temp->data;
}
temp1 = temp1->next;
}
temp =temp->next;
}
return 0;
}
//simply traversing the list in reverse order-by first traversong till end of list, then simply swapping next and prev pointers and printing
void reversePrint(Node *head)
{
Node *temp;
if(!head) return;
//first reach till end of list
reversePrint(head->next);
//now first print and simply swap the next and rprev pointers
// cout<<head->data<<"<->";
//
// //swapping the next and prev pointers
// temp=head->next;
// head->next = head->prev;
// head->prev = temp;
cout<<head->data<<"<->";
}
void printAlternateRec(Node *head,bool odd=true)
{
if(head==NULL) return ;
if(odd==true)
{
cout<<head->data<<"<->";
}
//this bool variable will make sure to print alternate nodes
printAlternateRec(head->next,!odd);
}
void printOddEvenNodes(Node *head,bool even=false)
{
if(head==NULL) return;
if(even==true)
{
cout<<head->data<<"->";
}
printOddEvenNodes(head->next,!even);
}
void Traverse(Node *head)
{
if(head)
{
cout<<(head)->data<<"<->";
Traverse(head->next);
}
}
using namespace std;
int main()
{
struct Node *head = new Node();
struct Node *n1 = new Node();
struct Node *n2 = new Node();
struct Node *n3 = new Node();
struct Node *n4 = new Node();
head->data=1;
n1->data=2;
n2->data=3;
n3->data=4;
n4->data=5;
head->next = n1;
head->prev=NULL;
n1->next=n2;
n2->next = n3;
n3->next=n4;
n4->next=NULL;
n1->prev=head;
n2->prev=n1;
n3->prev=n2;
n4->prev=n3;
Traverse(head);
cout<<endl;
cout<<"Odd position nodes are:"<<endl;
printAlternateRec(head);
cout<<endl;
cout<<"Even position nodes are:"<<endl;
printOddEvenNodes(head);
// cout<<endl;
//
cout<<maxOccuringItem(head);
//storing the new head
// Node *new_head = reverseRec(head);
//
// Traverse(new_head);//list reversed
return 0;
}