# Rotate of Doubly linked list by n nodes

Here given code implementation process.

``````//C Program
//Rotate Doubly linked list by n Nodes
#include <stdio.h>

#include <stdlib.h> //for malloc function

//create structure
struct Node {
int data;
struct Node *next;
struct Node *prev;
};

struct Node *head = NULL, *tail = NULL;

//insert Node element of end of linked list
void insert(int value) {

//Create a dynamic Node
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
if (node == NULL) {
printf("Memory overflow\n");
} else {
//set data value
node->data = value;
node->next = NULL;
node->prev = NULL;
tail = node;

} else {
node->prev = tail;
tail->next = node;

tail = node;

}
}
}
//display element of Node
void display() {
if (temp == NULL) {
} else {

printf("\n Head to Tail Nodes : \n");
//Traverse doubly linked list from front to rear
while (temp != NULL) {
//print Node value
printf("%3d", temp->data);
temp = temp->next;
}
printf("\n Tail to Head Nodes : \n");

temp = tail;

//Traverse doubly linked list from rear to front
while (temp != NULL) {
//print Node value
printf("%3d", temp->data);
temp = temp->prev;
}
}

}
void rotate(int n) {
if (head == NULL || n <= 0) {
return;
}

int counter = 1;
struct Node *find = NULL, *temp = head;
while (temp != NULL) {

if (counter == n) {
find = temp;
break;
}
counter++;
temp = temp->next;
}

if (find != NULL) {
if (find->next == NULL) {
//Then no modification in this case
return;
}

find->next->prev = NULL;
find->next = NULL;
tail = find;
} else {
printf("\nGiven Nodes %d are not exist", n);
}
}
int main() {

insert( 1);
insert( 2);
insert( 3);
insert( 4);
insert( 5);
insert( 6);
insert( 7);
//display all Node
display();

int n =3;

rotate(n );

printf("\n\n After rotate %d element \n",n);

display();
return 0;
}```
```

#### Output

`````` Head to Tail Nodes :
1  2  3  4  5  6  7
7  6  5  4  3  2  1

After rotate 3 element

4  5  6  7  1  2  3
3  2  1  7  6  5  4``````
``````/*
C++ Program
Rotate Doubly linked list by n nodes
*/

#include<iostream>

using namespace std;
class Node {
public:
int data;
Node *next;
Node *prev;
Node(int value) {
this->data = value;
this->next = NULL;
this->prev = NULL;
}
};
Node *tail;
public:
this->tail = NULL;
}
void insert(int value) {
Node *node = new Node(value);
if (node == NULL) {
cout << "Memory overflow\n";
return;
}
this->tail = node;
} else {
node->prev = this->tail;
this->tail->next = node;
this->tail = node;
}
}
void display() {
if (temp == NULL) {
return;
}
cout << "\n Head to Tail Nodes : \n";
while (temp != NULL) {
cout << temp->data << "  ";
temp = temp->next;
}
temp = this->tail;
cout << "\n Tail to Head Nodes : \n";
while (temp != NULL) {
cout << temp->data << "  ";
temp = temp->prev;
}
}
void rotate(int n) {
if (this->head == NULL || n <= 0) {
return;
}
int counter = 1;
Node *find = NULL, *temp = this->head;
while (temp != NULL) {
if (counter == n) {
find = temp;
break;
}
counter++;
temp = temp->next;
}
if (find != NULL) {
if (find->next == NULL) {
return;
}
find->next->prev = NULL;
find->next = NULL;
this->tail = find;
} else {
cout << "\nGiven Nodes " << n << " are not exist";
}
}

};
int main() {
obj.insert(1);
obj.insert(2);
obj.insert(3);
obj.insert(4);
obj.insert(5);
obj.insert(6);
obj.insert(7);
cout << "\nBefore";
obj.display();
int n = 3;
obj.rotate(n);
cout << "\n\n After rotate " << n << " element \n";
obj.display();
return 0;
}```
```

#### Output

``````Before
1  2  3  4  5  6  7
7  6  5  4  3  2  1

After rotate 3 element

4  5  6  7  1  2  3
3  2  1  7  6  5  4 ``````
``````/*
Java Program
Rotate Doubly linked list by n nodes

*/
class Node {

public int data;

public Node next;

public Node prev;

public Node(int value) {
//Setup initial values of linked list node
this.data = value;
this.next = null;
this.prev = null;
}
}

public Node tail;

tail = null;
}
public void insert(int value) {
Node node = new Node(value);

if (node == null) {

System.out.print("Memory overflow\n");
return;
}
tail = node;
} else {
node.prev = tail;
tail.next = node;
tail = node;
}
}

public void display() {
if (temp == null) {
return;
}

System.out.print("\n Head to Tail Nodes : \n");

while (temp != null) {
System.out.print(temp.data + "  ");
temp = temp.next;
}

temp = tail;

System.out.print("\n Tail to Head Nodes : \n");

while (temp != null) {
System.out.print(temp.data + "  ");
temp = temp.prev;
}

}
public void rotate(int n) {

if (head == null || n <= 0) {
return;
}
int counter = 1;
Node find = null, temp = head;

while (temp != null) {

if (counter == n) {
find = temp;
break;
}
counter++;
temp = temp.next;
}

if (find != null) {

if (find.next == null) {
return;
}
find.next.prev = null;
find.next = null;
tail = find;
} else {

System.out.print("\nGiven Nodes " + n + " are not exist");
}
}

public static void main(String[] args) {
obj.insert(1);
obj.insert(2);
obj.insert(3);
obj.insert(4);
obj.insert(5);
obj.insert(6);
obj.insert(7);
System.out.print("\nBefore");
obj.display();

int n = 3;

obj.rotate(n);

System.out.print("\n\n After rotate " + n + " element \n");
obj.display();
}

}```
```

#### Output

``````Before
1  2  3  4  5  6  7
7  6  5  4  3  2  1

After rotate 3 element

4  5  6  7  1  2  3
3  2  1  7  6  5  4 ``````
``````/*
C# Program
Rotate Doubly linked list by n nodes

*/
using System;
public class Node {

public int data;

public Node next;

public Node prev;

public Node(int value) {
//Setup initial values of linked list node
this.data = value;
this.next = null;
this.prev = null;
}
}

public Node tail;

tail = null;
}
public void insert(int value) {
Node node = new Node(value);

if (node == null) {

Console.Write("Memory overflow\n");
return;
}
tail = node;
} else {
node.prev = tail;
tail.next = node;
tail = node;
}
}

public void display() {
if (temp == null) {
return;
}

Console.Write("\n Head to Tail Nodes : \n");

while (temp != null) {
Console.Write(temp.data + "  ");
temp = temp.next;
}

temp = tail;

Console.Write("\n Tail to Head Nodes : \n");

while (temp != null) {
Console.Write(temp.data + "  ");
temp = temp.prev;
}

}
public void rotate(int n) {

if (head == null || n <= 0) {
return;
}
int counter = 1;
Node find = null, temp = head;

while (temp != null) {

if (counter == n) {
find = temp;
break;
}
counter++;
temp = temp.next;
}

if (find != null) {

if (find.next == null) {
return;
}
find.next.prev = null;
find.next = null;
tail = find;
} else {

Console.Write("\nGiven Nodes " + n + " are not exist");
}
}

public static void Main(String[] args) {
obj.insert(1);
obj.insert(2);
obj.insert(3);
obj.insert(4);
obj.insert(5);
obj.insert(6);
obj.insert(7);
Console.Write("\nBefore");
obj.display();

int n = 3;

obj.rotate(n);

Console.Write("\n\n After rotate " + n + " element \n");
obj.display();
}

}```
```

#### Output

``````Before
1  2  3  4  5  6  7
7  6  5  4  3  2  1

After rotate 3 element

4  5  6  7  1  2  3
3  2  1  7  6  5  4 ``````
``````# Python 3 Program
# Rotate Doubly linked list by n nodes

class Node :

def __init__(self, value) :
self.data = value
self.next = None
self.prev = None

def __init__(self) :
self.tail = None

def insert(self, value) :
node = Node(value)
if (node == None) :
print("Memory overflow\n")
return

self.tail = node
else :
node.prev = self.tail
self.tail.next = node
self.tail = node

def display(self) :
if (temp == None) :
return

print("\n Head to Tail Nodes : ")
while (temp != None) :
print(temp.data ,end="  ")
temp = temp.next

temp = self.tail
print("\n Tail to Head Nodes : ")
while (temp != None) :
print(temp.data ,end="  ")
temp = temp.prev

def rotate(self, n) :
if (self.head == None or n <= 0) :
return

counter = 1
find = None
while (temp != None) :
if (counter == n) :
find = temp
break

counter += 1
temp = temp.next

if (find != None) :
if (find.next == None) :
return

find.next.prev = None
find.next = None
self.tail = find
else :
print("\nGiven Nodes ", n ," are not exist")

def main() :
obj.insert(1)
obj.insert(2)
obj.insert(3)
obj.insert(4)
obj.insert(5)
obj.insert(6)
obj.insert(7)
print("\nBefore")
obj.display()
n = 3
obj.rotate(n)
print("\n\n After rotate ", n ," element ")
obj.display()

if __name__ == "__main__":
main()```
```

#### Output

``````Before
1  2  3  4  5  6  7
7  6  5  4  3  2  1

After rotate 3 element

4  5  6  7  1  2  3
3  2  1  7  6  5  4 ``````
``````# Ruby Program
# Rotate Doubly linked list by n nodes

class Node
attr_accessor :data, :next, :prev
def initialize(value)
self.data = value
self.next = nil
self.prev = nil
end
end

def initialize()
@tail = nil
end
def insert(value)
node = Node.new(value)
if (node == nil)
print("Memory overflow\n")
return
end
@tail = node
else
node.prev = @tail
@tail.next = node
@tail = node
end
end
def display()
if (temp == nil)
return
end
print("\n Head to Tail Nodes  :\n")
while (temp != nil)
print(temp.data ,"  ")
temp = temp.next
end
temp = @tail
print("\n Tail to Head Nodes  :\n")
while (temp != nil)
print(temp.data ,"  ")
temp = temp.prev
end
end
def rotate(n)
if (@head == nil or n <= 0)
return
end
counter = 1
find = nil
while (temp != nil)
if (counter == n)
find = temp
break
end
counter += 1
temp = temp.next
end
if (find != nil)
if (find.next == nil)
return
end
find.next.prev = nil
find.next = nil
@tail = find
else
print("\nGiven Nodes ", n ," are not exist")
end
end

end

def main()
obj.insert(1)
obj.insert(2)
obj.insert(3)
obj.insert(4)
obj.insert(5)
obj.insert(6)
obj.insert(7)
print("\nBefore")
obj.display()
n = 3
obj.rotate(n)
print("\n\n After rotate ", n ," element \n")
obj.display()
end
main()```
```

#### Output

``````Before
1  2  3  4  5  6  7
7  6  5  4  3  2  1

After rotate 3 element

4  5  6  7  1  2  3
3  2  1  7  6  5  4 ``````
``````<?php
/*
Php Program
Rotate Doubly linked list by n nodes
*/

class Node {
public \$data;
public \$next;
public \$prev;

function __construct(\$value) {
\$this->data = \$value;
\$this->next = null;
\$this->prev = null;
}
}
public \$tail;

function __construct() {
\$this->tail = null;
}
public  function insert(\$value) {
\$node = new Node(\$value);
if (\$node == null) {
echo("Memory overflow\n");
return;
}
\$this->tail = \$node;
} else {
\$node->prev = \$this->tail;
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
public  function display() {
if (\$temp == null) {
return;
}
echo("\n Head to Tail Nodes : \n");
while (\$temp != null) {
echo(\$temp->data ."  ");
\$temp = \$temp->next;
}
\$temp = \$this->tail;
echo("\n Tail to Head Nodes : \n");
while (\$temp != null) {
echo(\$temp->data ."  ");
\$temp = \$temp->prev;
}
}
public  function rotate(\$n) {
if (\$this->head == null || \$n <= 0) {
return;
}
\$counter = 1;
\$find = null;
while (\$temp != null) {
if (\$counter == \$n) {
\$find = \$temp;
break;
}
\$counter++;
\$temp = \$temp->next;
}
if (\$find != null) {
if (\$find->next == null) {
return;
}
\$find->next->prev = null;
\$find->next = null;
\$this->tail = \$find;
} else {
echo("\nGiven Nodes ". \$n ." are not exist");
}
}

}
function main() {
\$obj->insert(1);
\$obj->insert(2);
\$obj->insert(3);
\$obj->insert(4);
\$obj->insert(5);
\$obj->insert(6);
\$obj->insert(7);
echo("\nBefore");
\$obj->display();
\$n = 3;
\$obj->rotate(\$n);
echo("\n\n After rotate ". \$n ." element \n");
\$obj->display();
}
main();```
```

#### Output

``````Before
1  2  3  4  5  6  7
7  6  5  4  3  2  1

After rotate 3 element

4  5  6  7  1  2  3
3  2  1  7  6  5  4 ``````
``````/*
Node Js Program
Rotate Doubly linked list by n nodes
*/

class Node {

constructor(value) {
this.data = value;
this.next = null;
this.prev = null;
}
}

constructor() {
this.tail = null;
}
insert(value) {
var node = new Node(value);
if (node == null) {
process.stdout.write("Memory overflow\n");
return;
}
this.tail = node;
} else {
node.prev = this.tail;
this.tail.next = node;
this.tail = node;
}
}
display() {
if (temp == null) {
return;
}
process.stdout.write("\n Head to Tail Nodes : \n");
while (temp != null) {
process.stdout.write(temp.data + "  ");
temp = temp.next;
}
temp = this.tail;
process.stdout.write("\n Tail to Head Nodes : \n");
while (temp != null) {
process.stdout.write(temp.data + "  ");
temp = temp.prev;
}
}
rotate(n) {
if (this.head == null || n <= 0) {
return;
}
var counter = 1;
var find = null;
while (temp != null) {
if (counter == n) {
find = temp;
break;
}
counter++;
temp = temp.next;
}
if (find != null) {
if (find.next == null) {
return;
}
find.next.prev = null;
find.next = null;
this.tail = find;
} else {
process.stdout.write("\nGiven Nodes " + n + " are not exist");
}
}
}

function main() {
obj.insert(1);
obj.insert(2);
obj.insert(3);
obj.insert(4);
obj.insert(5);
obj.insert(6);
obj.insert(7);
process.stdout.write("\nBefore");
obj.display();
var n = 3;
obj.rotate(n);
process.stdout.write("\n\n After rotate " + n + " element \n");
obj.display();
}

main();```
```

#### Output

``````Before
1  2  3  4  5  6  7
7  6  5  4  3  2  1

After rotate 3 element

4  5  6  7  1  2  3
3  2  1  7  6  5  4 ``````
``````/*
Swift 4 Program
Rotate Doubly linked list by n nodes
*/

class Node {
var data: Int;
var next: Node? ;
var prev: Node? ;
init(_ value: Int) {
self.data = value;
self.next = nil;
self.prev = nil;
}
}
var tail: Node? ;
init() {
self.tail = nil;
}
func insert(_ value: Int) {
let node: Node? = Node(value);
if (node == nil) {
print("Memory overflow\n");
return;
}
self.tail = node;
} else {
node!.prev = self.tail;
self.tail!.next = node;
self.tail = node;
}
}
func display() {
if (temp == nil) {
return;
}
print("\n Head to Tail Nodes : ");
while (temp != nil) {
print(temp!.data ,terminator:"  ");
temp = temp!.next;
}
temp = self.tail;
print("\n Tail to Head Nodes : ");
while (temp != nil) {
print(temp!.data ,terminator:"  ");
temp = temp!.prev;
}
}
func rotate(_ n: Int) {
if (self.head == nil || n <= 0) {
return;
}
var counter: Int = 1;
var find: Node? = nil;
while (temp != nil) {
if (counter == n) {
find = temp;
break;
}
counter += 1;
temp = temp!.next;
}
if (find != nil) {
if (find!.next == nil) {
return;
}
find!.next!.prev = nil;
find!.next = nil;
self.tail = find;
} else {
print("\nGiven Nodes ", n ," are not exist");
}
}

}
func main() {
obj.insert(1);
obj.insert(2);
obj.insert(3);
obj.insert(4);
obj.insert(5);
obj.insert(6);
obj.insert(7);
print("\nBefore");
obj.display();
let n: Int = 3;
obj.rotate(n);
print("\n\n After rotate ", n ," element ");
obj.display();
}
main();```
```

#### Output

``````Before
1  2  3  4  5  6  7
7  6  5  4  3  2  1

After rotate 3 element

4  5  6  7  1  2  3
3  2  1  7  6  5  4 ``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.