# Convert a doubly linked list spiral form

Here given code implementation process.

``````//C Program
//Convert a doubly linked list spiral form
#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 rear
while (temp != NULL) {
//print Node value
printf("%3d", temp->data);
temp = temp->prev;
}
}

}
void frequency(int key) {
return;
}
int result = 0;
while (temp != NULL) {
if (temp->data == key) {
result++;
}
temp = temp->next;
}
printf("\nFrequency of node %d is : %d", key, result);

}
//Converting a doubly linked list into spiral form
void spiral() {

//When less than three nodes
return;
}
struct Node *current = head, *auxiliary = NULL, *temp = NULL;

while (current != NULL && tail != NULL) {
if (current == tail) {
current->next = NULL;
break;
} else if (current->next == tail) {
tail->next = NULL;
break;
}
auxiliary = current->next;

current->next = tail;

temp = tail->prev;

tail->prev = current;
tail->next = auxiliary;
if (auxiliary != NULL) {
auxiliary->prev = tail;
}
current = auxiliary;
tail = temp;
}

}
int main() {

insert(1);
insert(2);
insert(3);
insert(4);
insert(5);
insert(6);
insert(7);
insert(8);

printf("\nBefore Convert spiral form");
//display all node
display();

spiral();

printf("\nAfter Convert spiral form");
//display all node
display();
return 0;
}```
```

#### Output

``````Before Convert spiral form
1  2  3  4  5  6  7  8
8  7  6  5  4  3  2  1
After Convert spiral form
1  8  2  7  3  6  4  5
5  4  6  3  7  2  8  1``````
``````/*
C++ Program
Convert a doubly linked list spiral form
*/

#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;
}
};
public:
Node *tail;
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 spiral() {
return;
}
Node *current = this->head, *auxiliary = NULL, *temp = NULL;
while (current != NULL && this->tail != NULL) {
if (current == this->tail) {
current->next = NULL;
break;
} else
if (current->next == this->tail) {
this->tail->next = NULL;
break;
}
auxiliary = current->next;
current->next = this->tail;
temp = this->tail->prev;
this->tail->prev = current;
this->tail->next = auxiliary;
if (auxiliary != NULL) {
auxiliary->prev = this->tail;
}
current = auxiliary;
this->tail = temp;
}
}
};
int main() {
obj.insert(1);
obj.insert(2);
obj.insert(3);
obj.insert(4);
obj.insert(5);
obj.insert(6);
obj.insert(7);
obj.insert(8);
cout << "\nBefore Convert spiral form";
obj.display();
obj.spiral();
cout << "\nAfter Convert spiral form";
obj.display();
return 0;
}```
```

#### Output

``````Before Convert spiral form
1  2  3  4  5  6  7  8
8  7  6  5  4  3  2  1
After Convert spiral form
1  8  2  7  3  6  4  5
5  4  6  3  7  2  8  1 ``````
``````/*
Java Program
Convert a doubly linked list spiral form
*/
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;
}

}
//Converting a doubly linked list into spiral form
public void spiral() {

return;
}
Node current = head, auxiliary = null, temp = null;

while (current != null && tail != null) {

if (current == tail) {
current.next = null;
break;
} else
if (current.next == tail) {
tail.next = null;
break;
}
auxiliary = current.next;
current.next = tail;
temp = tail.prev;
tail.prev = current;
tail.next = auxiliary;

if (auxiliary != null) {
auxiliary.prev = tail;
}
current = auxiliary;
tail = temp;
}
}

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);
obj.insert(8);

System.out.print("\nBefore Convert spiral form");
//display all node
obj.display();

obj.spiral();

System.out.print("\nAfter Convert spiral form");
obj.display();
}

}```
```

#### Output

``````Before Convert spiral form
1  2  3  4  5  6  7  8
8  7  6  5  4  3  2  1
After Convert spiral form
1  8  2  7  3  6  4  5
5  4  6  3  7  2  8  1 ``````
``````/*
C# Program
Convert a doubly linked list spiral form
*/
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;
}

}
//Converting a doubly linked list into spiral form
public void spiral() {

return;
}
Node current = head, auxiliary = null, temp = null;

while (current != null && tail != null) {

if (current == tail) {
current.next = null;
break;
} else
if (current.next == tail) {
tail.next = null;
break;
}
auxiliary = current.next;
current.next = tail;
temp = tail.prev;
tail.prev = current;
tail.next = auxiliary;

if (auxiliary != null) {
auxiliary.prev = tail;
}
current = auxiliary;
tail = temp;
}
}

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);
obj.insert(8);

Console.Write("\nBefore Convert spiral form");
//display all node
obj.display();

obj.spiral();

Console.Write("\nAfter Convert spiral form");
obj.display();
}

}```
```

#### Output

``````Before Convert spiral form
1  2  3  4  5  6  7  8
8  7  6  5  4  3  2  1
After Convert spiral form
1  8  2  7  3  6  4  5
5  4  6  3  7  2  8  1 ``````
``````# Python 3 Program
# Convert a doubly linked list spiral form

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 spiral(self) :
return

auxiliary = None
temp = None
while (current != None and self.tail != None) :
if (current == self.tail) :
current.next = None
break
elif (current.next == self.tail) :
self.tail.next = None
break

auxiliary = current.next
current.next = self.tail
temp = self.tail.prev
self.tail.prev = current
self.tail.next = auxiliary
if (auxiliary != None) :
auxiliary.prev = self.tail

current = auxiliary
self.tail = temp

def main() :
obj.insert(1)
obj.insert(2)
obj.insert(3)
obj.insert(4)
obj.insert(5)
obj.insert(6)
obj.insert(7)
obj.insert(8)
print("\nBefore Convert spiral form")
obj.display()
obj.spiral()
print("\n\nAfter Convert spiral form")
obj.display()

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

#### Output

``````Before Convert spiral form
1  2  3  4  5  6  7  8
8  7  6  5  4  3  2  1
After Convert spiral form
1  8  2  7  3  6  4  5
5  4  6  3  7  2  8  1 ``````
``````# Ruby Program
# Convert a doubly linked list spiral form

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 spiral()
return
end
auxiliary = nil
temp = nil
while (current != nil and @tail != nil)
if (current == @tail)
current.next = nil
break
elsif (current.next == @tail)
@tail.next = nil
break
end
auxiliary = current.next
current.next = @tail
temp = @tail.prev
@tail.prev = current
@tail.next = auxiliary
if (auxiliary != nil)
auxiliary.prev = @tail
end
current = auxiliary
@tail = temp
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)
obj.insert(8)
print("\nBefore Convert spiral form")
obj.display()
obj.spiral()
print("\nAfter Convert spiral form")
obj.display()
end

main()```
```

#### Output

``````Before Convert spiral form
1  2  3  4  5  6  7  8
8  7  6  5  4  3  2  1
After Convert spiral form
1  8  2  7  3  6  4  5
5  4  6  3  7  2  8  1 ``````
``````<?php
/*
Php Program
Convert a doubly linked list spiral form
*/

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 spiral() {
return;
}
\$auxiliary = null;
\$temp = null;
while (\$current != null && \$this->tail != null) {
if (\$current == \$this->tail) {
\$current->next = null;
break;
} else
if (\$current->next == \$this->tail) {
\$this->tail->next = null;
break;
}
\$auxiliary = \$current->next;
\$current->next = \$this->tail;
\$temp = \$this->tail->prev;
\$this->tail->prev = \$current;
\$this->tail->next = \$auxiliary;
if (\$auxiliary != null) {
\$auxiliary->prev = \$this->tail;
}
\$current = \$auxiliary;
\$this->tail = \$temp;
}
}
}
function main() {
\$obj->insert(1);
\$obj->insert(2);
\$obj->insert(3);
\$obj->insert(4);
\$obj->insert(5);
\$obj->insert(6);
\$obj->insert(7);
\$obj->insert(8);
echo("\nBefore Convert spiral form");
\$obj->display();
\$obj->spiral();
echo("\nAfter Convert spiral form");
\$obj->display();
}
main();```
```

#### Output

``````Before Convert spiral form
1  2  3  4  5  6  7  8
8  7  6  5  4  3  2  1
After Convert spiral form
1  8  2  7  3  6  4  5
5  4  6  3  7  2  8  1 ``````
``````/*
Node Js Program
Convert a doubly linked list spiral form
*/

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;
}
}
spiral() {
return;
}
var auxiliary = null;
var temp = null;
while (current != null && this.tail != null) {
if (current == this.tail) {
current.next = null;
break;
} else
if (current.next == this.tail) {
this.tail.next = null;
break;
}
auxiliary = current.next;
current.next = this.tail;
temp = this.tail.prev;
this.tail.prev = current;
this.tail.next = auxiliary;
if (auxiliary != null) {
auxiliary.prev = this.tail;
}
current = auxiliary;
this.tail = temp;
}
}
}

function main() {
obj.insert(1);
obj.insert(2);
obj.insert(3);
obj.insert(4);
obj.insert(5);
obj.insert(6);
obj.insert(7);
obj.insert(8);
process.stdout.write("\nBefore Convert spiral form");
obj.display();
obj.spiral();
process.stdout.write("\nAfter Convert spiral form");
obj.display();
}

main();```
```

#### Output

``````Before Convert spiral form
1  2  3  4  5  6  7  8
8  7  6  5  4  3  2  1
After Convert spiral form
1  8  2  7  3  6  4  5
5  4  6  3  7  2  8  1 ``````
``````/*
Swift 4 Program
Convert a doubly linked list spiral form
*/

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;
}
}
//Convert Linked List is spiral form
func spiral() {
return;
}
var auxiliary: Node?  = nil;
var temp: Node?  = nil;
while (current != nil && self.tail != nil) {
if (current === self.tail) {
current!.next = nil;
break;
} else
if (current!.next === self.tail) {
self.tail!.next = nil;
break;
}
auxiliary = current!.next;
current!.next = self.tail;
temp = self.tail!.prev;
self.tail!.prev = current;
self.tail!.next = auxiliary;
if (auxiliary != nil) {
auxiliary!.prev = self.tail;
}
current = auxiliary;
self.tail = temp;
}
}
}
func main() {
obj.insert(1);
obj.insert(2);
obj.insert(3);
obj.insert(4);
obj.insert(5);
obj.insert(6);
obj.insert(7);
obj.insert(8);
print("\nBefore Convert spiral form");
obj.display();
obj.spiral();
print("\n\nAfter Convert spiral form");
obj.display();
}
main();```
```

#### Output

``````Before Convert spiral form
1  2  3  4  5  6  7  8
8  7  6  5  4  3  2  1
After Convert spiral form
1  8  2  7  3  6  4  5