Posted on by Kalkicode
Code Doubly linked list

Print node from backward and forward direction of doubly linked list in c

C program for Print node from backward and forward direction of doubly linked list. Here problem description and explanation.

// Include header file
#include <stdio.h>
#include <stdlib.h>
/* 
  C program for
  Print node in forward and backward direction 
  in doubly linked list
*/
typedef struct LinkNode
{
	// Define useful field of LinkNode
	int key;
	struct LinkNode * next;
	struct LinkNode * back;
}LinkNode;

LinkNode * getLinkNode(int key)
{
	// Create dynamic memory of LinkNode
	LinkNode * ref = (LinkNode * ) malloc(sizeof(LinkNode));
	if (ref == NULL)
	{
		// Failed to create memory 
		return NULL;
	}
	// Setup initial values of node
	ref->key = key;
	ref->next = NULL;
	ref->back = NULL;
	return ref;
}

typedef struct DoublyLL
{
	// Define useful field of DoublyLL
	struct LinkNode * front;
	struct LinkNode * rear;
}DoublyLL;

DoublyLL * getDoublyLL()
{
	// Create dynamic memory of DoublyLL
	DoublyLL * ref = (DoublyLL * ) malloc(sizeof(DoublyLL));
	if (ref == NULL)
	{
		// Failed to create memory 
		return NULL;
	}
	// Set initial default value
	ref->front = NULL;
	ref->rear = NULL;
	return ref;
}
void addNode(DoublyLL * ref, int value)
{
	// Create a new node
	LinkNode * node = getLinkNode(value);
	if ((ref->front == NULL))
	{
		ref->front = node;
	}
	else
	{
		// Add Node at the end of linked list
		node->back = ref->rear;
		ref->rear->next = node;
	}
	// New last node
	ref->rear = node;
}
// Print linked list node from left to right 
// Forward direction
void forwardDirection(DoublyLL * ref)
{
	if ((ref->front == NULL))
	{
		printf("\n Empty linked list");
		return;
	}
	printf("\n Forward Direction Nodes\n");
	LinkNode * temp = ref->front;
	// iterating linked list elements
	while (temp != NULL)
	{
		// Display node value
		printf(" %d ", temp->key);
		// Visit to next node
		temp = temp->next;
	}
}
// Print linked list node from right to left 
// Backward direction
void backwardDirection(DoublyLL * ref)
{
	if ((ref->rear == NULL))
	{
		printf("\n Empty linked list");
		return;
	}
	printf("\n Backward Direction Nodes\n");
	// In case last node unknown then first find last node
	LinkNode * temp = ref->rear;
	// iterating linked list elements
	while (temp != NULL)
	{
		// Display node value
		printf(" %d ", temp->key);
		// Visit to previous node
		temp = temp->back;
	}
}
int main()
{
	DoublyLL * dll = getDoublyLL();
	// Add nodes
	addNode(dll, 24);
	addNode(dll, 4);
	addNode(dll, 21);
	addNode(dll, 10);
	addNode(dll, 13);
	addNode(dll, 15);
	addNode(dll, 28);
	// Constructing linked list
	// NULL ← 24 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
	//         ↑                            ↑
	//         head                        rear
	forwardDirection(dll);
	backwardDirection(dll);
}

Output

 Forward Direction Nodes
 24  4  21  10  13  15  28
 Backward Direction Nodes
 28  15  13  10  21  4  24

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