PROGRAM TO INSERT IN THE END OF DOUBLY LINKED LIST
/****************************************************/ /*PROGRAM TO INSERT IN THE END OF DOUBLY LINKED LIST*/ /****************************************************/
#include < stdio.h> #include < conio.h> #include < malloc.h> #include < process.h> #include < ctype.h>
struct doubly_list { int info; struct doubly_list *prev; struct doubly_list *next; }*first,*last,*newnode,*ptr;
void main() { int item,i; char ch; clrscr(); newnode=(struct doubly_list*)malloc(sizeof(struct doubly_list)); first=newnode; last=newnode; newnode->prev=NULL; do { printf("\nEnter data: "); scanf("%d",&item); newnode->info=item; printf("\nDo you want to create another node:(y/n)"); fflush(stdin); scanf("%c",&ch); if(tolower(ch)=='y') { newnode->next=(struct doubly_list*)malloc(sizeof(struct doubly_list)); newnode->next->prev=newnode; newnode=newnode->next; last=newnode; } else { newnode->next=NULL; } }while(tolower(ch)!='n'); printf(“\nDoubly Linked List is:”); ptr=first; i=1; while(ptr!=NULL) { printf("\nNode %d : %d",i,ptr->info); ptr=ptr->next; i++; } printf("\nEnter the item tobe inserted: "); scanf("%d",&item); newnode=(struct doubly_list*)malloc(sizeof(struct doubly_list)); newnode->info=item; if(first==NULL) { first=newnode; last=newnode; newnode->prev=NULL; newnode->next=NULL; } else { newnode->next=NULL; newnode->prev=last; last->next=newnode; last=newnode; } printf("\nAfter insertion in the end the linked list is:\n"); ptr=first; i=1; while(ptr!=NULL) { printf("\nNode %d : %d",i,ptr->info); ptr=ptr->next; i++; } getch(); }
http://
http://
Contributed by:
Rohit kakria
I am software developer
Resourse address on xpode.com
http://www.xpode.com/Print.aspx?Articleid=353
Click here to go on website
|