Create and traverse Doubly Linked List

Last Updated on

Create a Doubly linked list

traverse Doubly Linked List

1. create a node template with the basic code given below.

struct dll
{
	struct dll *prev;
	int data;
	struct dll *next
};

*prev pointer has the address of the previous node for from the actual current node

*next pointer hs address of the next node for the actual current node.

2.use typedef keyword to create an alias name  for structure dll (data type).here in the below code new datatype known as the node is created using the typedef keyword

typedef struct dll node;

3 .write a function for creating a newnode using malloc function and taking node data value from the user with in the function

node* getnode()
{
	node * newnode;
	newnode = (node *) malloc(sizeof(node));
	printf("\n Enter data: ");
	scanf("%d", &newnode -> data);
	newnode -> prev = NULL;
	newnode -> next = NULL;
	return newnode;
}

In the above code getnode() function returns the node type of pointer, it returns the newnode from the function .when the getnode function is called in the program this creates a node in the memory.

create two more pointers known as *newnode,*temp inside createnodes function, use FOR loop for condition (i<no of nodes).

4.call getnode( ) function for the newnode pointer ,inside for loop

newnode = getnode();

5. create an if block for condition (head==NULL) then assign newnode to the head

head = newnode;
node with two address fields

for the else block

6.to add next nodes, initialize temp with a head pointer

initialize temp with a head pointer

7.use while condition (temp->next!=NULL), so that the temp will be at the last or if there is only one node the temp pointer will e there in the first place

if there are more then one node, this while condition is useful, inside while loop change temp from one node to the next node until it points to the last node

while(temp -> next!=NULL)
temp = temp -> next;

8.link temp node next address to the newnode

link temp node next address

9.link newnode previous address to the temp node

link newnode previous address to the temp node

Program to create Doubly linked list

void createlist(int n)
{
	int i;
	node *newnode;
	node *temp;
	for(i = 0; i < n; i++)
	{
		newnode = getnode();
		if(start == NULL)
			head = newnode;
		else
		{
			temp = head;
			while(temp -> right)
				temp = temp -> right;
			temp -> right = newnode;
			newnode -> left = temp;
		}
	}
}

Traverse doubly linked list from the beginning

1.create a *temp pointer from node structure, initialize this temp pointer with head

traverse doubly linked list from the beginning

2.access every data using temp pointer ,move temp pointer by replacing next nodes address (temp=temp->next ) until the condition (temp!=NULL)

Create and traverse Doubly Linked List 1
Create and traverse Doubly Linked List 2

3.display every node data, when the particular node has temp pointer

Program to traverse doubly linked list from the beginning

void traversebeg()
{
	node *temp;
	temp = head;
	printf("\n The contents of List: ");
	if(head == NULL )
		printf("\n Empty List");
	else
	{
		while(temp != NULL)
		{
			printf("\t %d ", temp -> data);
			temp = temp -> right;
		 }
	}
}

traverse doubly linked list from the ending

1.create a *temp pointer from node structure, initialize this temp pointer with head

create temp pointer to head

2.move temp pointer to the ending of the doubly linked list, move temp pointer by replacing next nodes address (temp=temp->next ) until the condition (temp!=NULL)

traverse doubly linked list from the ending

3.display the data from the node, then move temp pointer in reverse from ending to beginning buy replacing previous node address (temp=temp->prev ) until the condition (temp!=NULL)

 move temp pointer in reverse from ending to beginning
Create and traverse Doubly Linked List 3

Program to traverse doubly linked list from the ending

void traverseend()
{
	node *temp;
	temp = head;
	printf("\n The contents of List: ");
	if(head == NULL)
		printf("\n Empty List");
	else
	{
		while(temp -> right != NULL)
			temp = temp -> right;
	 }
	while(temp != NULL)
	{
		printf("\t%d", temp -> data);
		temp = temp -> left;
	}
}