Search

Tuesday, February 25, 2014
0 comments

C Program to implement doubly linked list for Data Structrue in 'C'

7:24 AMTuesday, February 25, 2014
C Program to implement doubly linked list:

#include<stdio.h>
#include<alloc.h>
#include<stdlib.h>
#include<conio.h>
struct dlnode
{
Int info;
struct dlnode *link;
struct dlnode *prev;
}; typedef struct dlnode dln;
main( )
{
dln *root, *temp, *new, *ptr; char choice=’y’;
root=NULL;
while(choice= =’y’)
{
new=(dln*)malloc(sizeof(dln);
if(new= =NULL)
{
printf(“Memory allocation error…”); exit(0);
}
new->link=NULL; new->prev=NULL;
printf(“\nEnter node information :”);
scanf(“%d”,&new->info);
if(root= =NULL)
{
root=new;    temp=root;
}
else
{
new->prev=temp;   temp->link=new;
temp=new;
}
printf(“Do you want to continue…(y/n)”);
choice=getche();
}
printf(“\nDoubly linked list is:\n\n”);
/* Doubly linked list normal singly linked list if it is traversed using link field of the node*/
ptr=root;
while(ptr=NULL)
{
printf(“%d”,ptr->info);
ptr=ptr->link;
}
printf(“\n\nDoubly linked list(reverse order):\n\n”);
/*The pointer temp is pointing to the last node. So, we can use it traverse in reverse order*/
while(temp!=NULL)
{
printf(“%d”,temp->info);
temp=temp->prev;
}
getch();
}

Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top