Skip to main content

Anticlockwise spiral conversion of doubly linked list in c

C program for Anticlockwise spiral conversion of doubly linked list. Here problem description and explanation.

// Include header file
#include <stdio.h>
#include <stdlib.h>

// C program for
// Convert a doubly linked list anticlockwise spiral form

// This is Linked List Node
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 * ref = (LinkNode * ) malloc(sizeof(LinkNode));
	if (ref == NULL)
	{
		// Failed to create memory 
		return NULL;
	}
	ref->data = data;
	ref->next = NULL;
	ref->prev = NULL;
	return ref;
}

typedef struct DoublyLinkedList
{
	// Define useful field of DoublyLinkedList
	struct LinkNode * head;
	struct LinkNode * tail;
}DoublyLinkedList;

DoublyLinkedList * getDoublyLinkedList()
{
	// Create dynamic memory of DoublyLinkedList
	DoublyLinkedList * ref = 
      (DoublyLinkedList * ) malloc(sizeof(DoublyLinkedList));
	if (ref == NULL)
	{
		// Failed to create memory 
		return NULL;
	}
	// Set head and tail
	ref->head = NULL;
	ref->tail = NULL;
	return ref;
}
// Insert new node at end position
void insert(DoublyLinkedList * ref, int value)
{
	// Create a node
	LinkNode * node = getLinkNode(value);
	if (ref->head == NULL)
	{
		// Add first node
		ref->head = node;
		ref->tail = node;
		return;
	}
	// Add node at last position
	ref->tail->next = node;
	node->prev = ref->tail;
	ref->tail = node;
}
// Display node element of doubly linked list
void display(DoublyLinkedList * ref)
{
	if (ref->head == NULL)
	{
		printf("Empty Linked List");
	}
	else
	{
		printf("\nLinked List Head to Tail :");
		// Get first node of linked list
		LinkNode * temp = ref->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 = ref->tail;
		// iterate linked list 
		while (temp != NULL)
		{
			// Display node value
			printf(" %d ", temp->data);
			// Visit to prev node
			temp = temp->prev;
		}
	}
}
void spiralTransform(DoublyLinkedList * ref)
{
	if (ref->head == NULL || ref->head->next == NULL)
	{
		return;
	}
	LinkNode * last = ref->tail;
	LinkNode * current = ref->head;
	LinkNode * auxiliary = NULL;
	LinkNode * temp = NULL;
	LinkNode * back = NULL;
	// Set new head
	ref->head = ref->tail;
	while (current != NULL && last != NULL)
	{
		if (current == last)
		{
			current->next = NULL;
			current->prev = back;
			ref->tail = current;
			// Set new last node
			return;
		}
		else if (current->next == last)
		{
			current->next = NULL;
			current->prev = last;
			last->next = current;
			last->prev = back;
			// Set new last node
			ref->tail = current;
			return;
		}
		// Swap link
		auxiliary = current->next;
		temp = last->prev;
		current->prev = last;
		last->next = current;
		current->next = temp;
		last->prev = back;
		back = current;
		current = auxiliary;
		last = 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);
	insert(dll, 9);
	printf("\nBefore anticlockwise spiral conversion");
	// Display all node
	display(dll);
	printf("\nAfter anticlockwise spiral conversion");
	spiralTransform(dll);
	display(dll);
}

Output

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




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.

New Comment