//////add node at last in doubly linked list/////
/////////////////////////////////////////////////
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
typedef struct node
{
int data;
struct node *pre,*next;
}node;
void insert(node **,int);
void display(node *);
void main()
{
node *last=NULL;
clrscr();
insert(&last,15);
insert(&last,25);
insert(&last,35);
printf("list is \n ");
display(last);
getch();
}
void insert(node **last,int d)
{
node *q;
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL) {
printf("memory overflow");
exit(0); }
if(*last==NULL) {
newnode->next=NULL;
newnode->pre=*last;
*last=newnode; }
else {
q=*last;
while(q->next!=NULL) { q=q->next; }
newnode->pre=q;
newnode->next=NULL;
q->next=newnode;
q=newnode; }
newnode->data=d;
}
void display(node *last)
{
while(last!=NULL)
{
printf(" %d",last->data);
last=last->next;
}
}
No comments:
Post a Comment