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
typedef struct LinkNode
{
// Define useful field of LinkNode
int data;
struct LinkNode * next;
struct LinkNode * prev;
}LinkNode;
LinkNode * getLinkNode(int data)
{
// Create dynamic memory of LinkNode
LinkNode * me = (LinkNode * ) malloc(sizeof(LinkNode));
if (me == NULL)
{
// Failed to create memory
return NULL;
}
me->data = data;
me->next = NULL;
me->prev = NULL;
return me;
}
typedef struct DoublyLinkedList
{
// Define useful field of DoublyLinkedList
struct LinkNode * head;
struct LinkNode * tail;
}DoublyLinkedList;
DoublyLinkedList * getDoublyLinkedList()
{
// Create dynamic memory of DoublyLinkedList
DoublyLinkedList * me =
(DoublyLinkedList * ) malloc(sizeof(DoublyLinkedList));
if (me == NULL)
{
// Failed to create memory
return NULL;
}
// Set inital value
me->head = NULL;
me->tail = NULL;
return me;
}
// Insert new node at end position
void insert(DoublyLinkedList * me, int value)
{
// Create a node
LinkNode * node = getLinkNode(value);
if ((me->head == NULL))
{
// Add first node
me->head = 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
void display(DoublyLinkedList * me)
{
if ((me->head == NULL))
{
printf("Empty Linked List");
}
else
{
printf("\nLinked List Head to Tail :");
// Get first node of linked list
LinkNode * temp = me->head;
// iterate linked list
while (temp != NULL)
{
// Display node value
printf(" %d ", temp->data);
// Visit to next node
temp = temp->next;
}
printf("\nLinked List Tail to Head :");
// Get last node of linked list
temp = me->tail;
// iterate linked list
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
void spiral(DoublyLinkedList * me)
{
if ((me->head == NULL || me->head->next == NULL ||
me->head->next->next == NULL))
{
return;
}
// Auxiliary variables
LinkNode * current = me->head;
LinkNode * auxiliary = NULL;
LinkNode * temp = NULL;
// 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()
{
DoublyLinkedList * dll = getDoublyLinkedList();
// 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
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.
New Comment