Wednesday, June 6, 2012

C program on inserting a node at front and rear end of doubly linked list


In this program of doubly linked list in C language, we will insert a new node at the extreme right of the list

Algorithm for inserting node in doubly linked list at the front end

1. Allocate new node and enter data.
2. Access the first node address (next to left list pointer) of the doubly linked list.
3. Set the next pointer of new node to point the first node of the doubly linked list.
4. Set the previous pointer of the first node to point the new node.
5. Set the previous pointer of the new node to point the left list pointer of the doubly linked list.
6. Set the next pointer of left list pointer of doubly linked list to point the new node.

# include < stdio.h >
# include < stdlib.h >
struct list
{
char info[20];
struct list *next;
struct list *prev;
};
struct list *new1,*node;
void create(struct list *s,struct list *e)
{
char ch;
node=s;
printf("\nWant to create a node(y/n):");
ch=getche();
while (ch != 'n')
{
node->next = (struct list *) malloc(sizeof(struct list));
node->next->prev= node;
node = node->next;
printf("\n Enter the string value:- ");
gets(node->info);
node->next = e;
e->prev=node;
printf("\n Enter choice--'n' for break: ");
ch = getche();
}
}
void displayL (struct list *s,struct list *e)
{
node = s->next;
while (node!=e)
{
printf(" 0x%x--%s", node,node->info);
node = node->next;
}
printf("\n");
}
void displayR (struct list *e,struct list *s)
{
node = e->prev;
while (node!=s)
{
printf(" 0x%x--%s", node,node->info);
node = node->prev;
}
printf("\n");
}
void insertL(struct list *s, struct list *e)
{
struct list *new1;
new1 = (struct list *) malloc(sizeof(struct list));
printf("\nEnter the new value:");
gets(new1->info);
node=e->prev;
node->next=new1;
new1->prev=node;
new1->next=e;
e->prev=new1;
}
void main()
{
struct list *start,*end;
clrscr();
start=(struct list *) malloc(sizeof(struct list));;
end=(struct list *) malloc(sizeof(struct list));;
create(start,end);
printf("\n Created list is as follows(L ->R)\n");
displayL(start,end);
printf("\n Created list displayed from R->L\n");
displayR(end,start);
printf("\nInserting a new location at the rear end\n");
insertL(start,end);
printf("\n now the listfrom L ->R\n");
displayL(start,end);
printf("\n list from R to L after insertion\n");
displayR(end,start);
getch();
}



Program on inserting new node at rear end of doubly linked list

 Steps taken to add a node in doubly linked list at the rear end

1. Allocate new node and enter data.
2. Access the last node address of the doubly linked list using the right side list. pointer, no traversal is required.
3. Set the previous pointer of the new node to point the last node.
4. Set the next pointer of last node of doubly linked list to point the new node.
5. Set the next pointer of new node to point the right list pointer of the doubly linked list.
6. Set the previous pointer of right list pointer to point the new node.

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner