-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathright_shift.cpp
More file actions
63 lines (57 loc) · 965 Bytes
/
right_shift.cpp
File metadata and controls
63 lines (57 loc) · 965 Bytes
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
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int value;
Node *next;
};
Node *head=nullptr;
void createNode(int arr[], int n) //array data to link list data
{
int i;
Node *t,*last;
head=new Node;
head->value=arr[0];
head->next=nullptr;
last=head;
for(i=1;i<n;i++)
{
t=new Node;
t->value=arr[i];
t->next=nullptr;
last->next=t;
last=t;
}
}
void Display(struct Node *p) // front to end
{ cout<<"Displaying LinkedList:"<<endl;
while(p!=0)
{
cout<<p->value<<" ";
p=p->next;
}
}
void right_shift(int k)
{
while(k>0)
{
static Node *q=head,*r=nullptr;
while(q->next!=0)
{
r=q;
q=q->next;
}
q->next=head;
r->next=nullptr;
head=q;
k--;
}
}
int main()
{
int a[]={1,2,1,3,4,5};
createNode(a,6);
cout<<endl;
right_shift(3);
Display(head);
}