Posted on by Kalkicode

# Convert a doubly linked list into spiral form in c

C program for Convert a doubly linked list into spiral form. Here problem description and explanation.

``````// Include header file
#include <stdio.h>
#include <stdlib.h>
// C program for
// Convert a doubly linked list spiral form
{
// Define useful field of LinkNode
int data;

{
// Create dynamic memory of LinkNode
if (me == NULL)
{
// Failed to create memory
return NULL;
}
me->data = data;
me->next = NULL;
me->prev = NULL;
return me;
}
{
// Define useful field of DoublyLinkedList

{
// Create dynamic memory of DoublyLinkedList
if (me == NULL)
{
// Failed to create memory
return NULL;
}
// Set inital value
me->tail = NULL;
return me;
}
// Insert new node at end position
void insert(DoublyLinkedList * me, int value)
{
// Create a node
{
me->tail = node;
return;
}
// Add node at last position
me->tail->next = node;
node->prev = me->tail;
me->tail = node;
}
// Display node element of doubly linked list
{
{
}
else
{
// Get first node of linked list
while (temp != NULL)
{
// Display node value
printf(" %d ", temp->data);
// Visit to next node
temp = temp->next;
}
// Get last node of linked list
temp = me->tail;
while (temp != NULL)
{
// Display node value
printf(" %d ", temp->data);
// Visit to prev node
temp = temp->prev;
}
}
}
//  Converting a doubly linked list into spiral form
{
{
return;
}
// Auxiliary variables
// spiral view is combination first and last node
// Example 1 2 3 4 5
//         1       5  [Get corner nodes]
//                    [1,4] result
//           2 3 4
//           2   4    [Get corner nodes]
//                    [1,5,2,4] result
//             3      [Get corner nodes]
//                    In this case only one
//                    [1,5,2,4,3] result
// ------------------------------------
// Process using head and tail node
while (current != NULL && me->tail != NULL)
{
if ((current == me->tail))
{
// last result node
current->next = NULL;
return;
}
else if ((current->next == me->tail))
{
me->tail->next = NULL;
return;
}
auxiliary = current->next;
current->next = me->tail;
temp = me->tail->prev;
me->tail->prev = current;
me->tail->next = auxiliary;
if ((auxiliary != NULL))
{
auxiliary->prev = me->tail;
}
current = auxiliary;
// new last node
me->tail = temp;
}
}
int main()
{
// Insert following linked list nodes
insert(dll, 1);
insert(dll, 2);
insert(dll, 3);
insert(dll, 4);
insert(dll, 5);
insert(dll, 6);
insert(dll, 7);
insert(dll, 8);
printf("Before spiral conversion :");
display(dll);
spiral(dll);
printf("\nAfter spiral conversion :");
display(dll);
}``````

Output

``````Before spiral conversion :
Linked List Head to Tail : 1  2  3  4  5  6  7  8
Linked List Tail to Head : 8  7  6  5  4  3  2  1
After spiral conversion :
Linked List Head to Tail : 1  8  2  7  3  6  4  5
Linked List Tail to Head : 5  4  6  3  7  2  8  1``````

## Comment

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.

Categories
Relative Post