Home Tutorial Datastructure Doubly Linked List

 
 

Share on Google+Share on Google+
Doubly Linked List
Posted on: May 1, 2010 at 12:00 AM
Advertisement
This tutorial demonstrate how to implement doubly linked list.

Code:

# include <stdio.h>
# include <stdlib.h>
struct dnode
{
int data;
struct dnode *prev, *next;
};

struct dnode *insert(struct dnode *p , struct dnode **q, int n)
{
struct dnode *temp;

 if(p==NULL)
	{
		p=(struct dnode *)malloc(sizeof(struct dnode));
		if(p==NULL)
		{
		printf("Error Occurred\n");
		exit(0);
		}
		 p->data = n;
		 p-> prev = p->next =NULL;
		 *q =p;
	 }
	 else
	 {
	 temp =  (struct dnode *)malloc(sizeof(struct dnode));

	if(temp == NULL)
	{
		printf("Error Occurred\n");
		exit(0);
	}
		 temp->data = n;
		 temp->prev = (*q);
		 temp->next = NULL;
		 (*q)->next = temp;
		 (*q) = temp;
 }
	  return (p);
}

void printnode( struct dnode *p  )
{
	printf("All data of created list is\n");
	while (p!= NULL)
	{
		printf("%d\t",p-> data);
		p = p->next;
	}
}

void main()
{
	int n;
	int x;
	struct dnode *start = NULL ;
	struct dnode *end = NULL;
	printf("Enter the number of nodes to be created \n");
	scanf("%d",&n);
	while ( n-- > 0 )
	{
	printf( "Enter the data values for each node\n");
	scanf("%d",&x);
	start = insert ( start , &end,x );
	}
	printnode( start );
}

Output:

Download this code

Advertisement

Related Tags for Doubly Linked List:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 1, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com