Skip to content

Commit b387c5c

Browse files
authored
Doubly linked list is rotated
Doubly linked list is rotated by p number of nodes which is taken input from the user
1 parent 0bd8b46 commit b387c5c

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
struct node
5+
{
6+
int data;
7+
struct node*next,*prev;
8+
};
9+
10+
struct node* update(struct node*start,int p);
11+
12+
int main()
13+
{
14+
15+
int n,p; // n is the number of nodes in the linked list , p is the number of nodes to be rotated starting from the head of linked list
16+
cin>>n>>p;
17+
struct node*start=NULL,*cur=NULL,*ptr=NULL;
18+
for(int i=0;i<n;i++)
19+
{
20+
int a;
21+
cin>>a;
22+
ptr=new(struct node);
23+
ptr->data=a;
24+
ptr->next=NULL;
25+
ptr->prev=NULL;
26+
if(start==NULL)
27+
{
28+
start=ptr;
29+
cur=ptr;
30+
}
31+
else
32+
{
33+
cur->next=ptr;
34+
ptr->prev=cur;
35+
cur=ptr;
36+
}
37+
}
38+
struct node*str=update(start,p);
39+
while(str!=NULL)
40+
{
41+
cout<<str->data<<" ";
42+
str=str->next;
43+
}
44+
cout<<endl ;
45+
}
46+
47+
struct node*update(struct node*start,int p)
48+
{
49+
node* temp = start ;
50+
51+
while(temp->next!=NULL)
52+
{
53+
temp = temp->next ;
54+
}
55+
node* s= start ;
56+
for(int i=0;i<p;i++)
57+
{
58+
node* t = s ;
59+
s = s->next ;
60+
temp->next = t ;
61+
t->prev = temp ;
62+
temp=temp->next ;
63+
t->next = NULL ;
64+
}
65+
return s ;
66+
}
67+
68+

0 commit comments

Comments
 (0)