Showing posts with label Doubly Linked List. Show all posts
Showing posts with label Doubly Linked List. Show all posts

Saturday, January 16, 2021

Write a C program to copy one linked list to another and count the number of 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(" %u--%s", node,node->info);
node = node->next;
}
printf("\n");
}
void displayR (struct list *e,struct list *s)
{
node = e->prev;
while (node!=s)
{
printf("  %s",node->info);
node = node->prev;
}
printf("\n");
}
void merge(struct list *s,struct list *e, struct list *s1,struct list *e1)
{
int i=0;
s=s->next;

while(s!=e)
{
s1->next=(struct list*)malloc(sizeof(struct list));
s1->next->prev=s1;
s1=s1->next;
 strcpy(s1->info,s->info);
 s=s->next;
 s1->next=e1;
e1->prev=s1;

 i++;
}
printf("\nNumber of Nodes: %d",i);
}
void main()
{
struct list *start,*end,*start1,*end1;
clrscr();
start= (struct list *) malloc(sizeof(struct list));
start->next=NULL;
end= (struct list *) malloc(sizeof(struct list));
end->next=NULL;
start1= (struct list *) malloc(sizeof(struct list));
start1->next=NULL;
end1= (struct list *) malloc(sizeof(struct list));
end1->next=NULL;
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);
merge(start,end,start1,end1);
printf("\n Copied List from L ->R\n");
displayL(start1,end1);
printf("\n list from R to L after deletion\n");
displayR(end1,start1);

getch();

}


Deletion Of All Nodes From A Doubly Linked List

 
# include < stdio.h>
# include < stdlib.h>
struct link
{
char info[20];
struct link *next;
};
struct link *start,*p,*node;
int i=1;
void create()
{
char ch='y';
node=start;
while(ch != 'n')
{
node->next = (struct link* ) malloc(sizeof(struct link));
node = node->next;
printf("\n Enter string for node: %d: ",i);
scanf("%s", node->info);
node->next = NULL;
i++;
printf("\n Enter choice-- 'n' for break: ");
ch = getche();
}
}
void display(struct link *n)
{
node=n;
while(node)
{
printf("\n 0x%x ", node);
printf("%s\n",node->info);
node=node->next;
}
}
void delNodes(struct link *n)
{
node=n;
p = n->next;

while(p)
{

node->next = p->next;
printf("Deleting %s\n",p->info);
free(p);
p = node->next;
}
do
{
free(start);
start=start->next;
}while(start);
}

void main()
{
clrscr();
start = (struct link* ) malloc(sizeof(struct link));
printf("\n Enter string for node: %d: ",i);
scanf("%s", start->info);
start->next = NULL;
create();
printf("\nAfter Creation...");
display(start);
delNodes(start);
printf("\nAfter Deletion...");
display(start);
getch();

}


Thursday, January 14, 2021

C function to delete odd position elements from doubly linked list

 
# include < stdio.h>
# include < stdio.h>
struct list
{
int age;
struct list *next;
struct list *prev;
};
struct list *new1,*node;
void create(struct list *s,struct list *e)
{
char ch;
node=s;
node->next = (struct list *) malloc(sizeof(struct list));
node->next->prev= node;
node = node->next;
node->next=e;
e->prev=node;
printf("\n Enter the numeric value:- ");
scanf("%d",&node->age);

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 numeric value:- ");
scanf("%d",&node->age);
printf("\n Enter choice--'n' for break: ");
ch = getche();
}
node->next = e;
e->prev=node;

}
void displayL (struct list *s,struct list *e)
{
node = s->next;
while (node!=e)
{
printf("\n%d", node->age);
node = node->next;
}
printf("\n");
}
void displayR (struct list *e,struct list *s)
{
node = e->prev;
while (node!=s)
{
printf("\n%d",node->age);
node = node->prev;
}
printf("\n");
}
void delA(struct list *s,struct list *e)
{
int c=1,counter;
printf("\nEnter the location of the node to be deleted:");
scanf("%d",&counter);
node=s->next;
while(node->next!=e)
{
if(c==counter)
break;
c++;
node=node->next;
}
node->prev->next=node->next;
node->next->prev=node->prev;
free(node);
}
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("\nDeleting the First node\n");
delA(start,end);
printf("\n now the listfrom L ->R\n");
displayL(start,end);
printf("\n list from R to L after deletion\n");
displayR(end,start);
getch();

}


Tuesday, June 12, 2012

C program to delete any node of a doubly linked list


Previously we have seen how to delete the last node and first node of a doubly linked list. In both the above cases no traversal was required. To delete any location node of a doubly linked list, traversal is required but unlike linear linked list only one moving pointer is needed here. This moving pointer in the doubly linked list will trace out the location and from that point the node will be disconnected from the doubly linked list and removed.

Here is the program

# include < stdio.h>
# include < stdio.h>
struct list
{
int age;
struct list *next;
struct list *prev;
};
struct list *new1,*node;
void create(struct list *s,struct list *e)
{
char ch;
node=s;
node->next = (struct list *) malloc(sizeof(struct list));
node->next->prev= node;
node = node->next;
node->next=e;
e->prev=node;
printf("\n Enter the numeric value:- ");
scanf("%d",&node->age);

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 numeric value:- ");
scanf("%d",&node->age);
printf("\n Enter choice--'n' for break: ");
ch = getche();
}
node->next = e;
e->prev=node;

}
void displayL (struct list *s,struct list *e)
{
node = s->next;
while (node!=e)
{
printf("\n%d", node->age);
node = node->next;
}
printf("\n");
}
void displayR (struct list *e,struct list *s)
{
node = e->prev;
while (node!=s)
{
printf("\n%d",node->age);
node = node->prev;
}
printf("\n");
}
void delA(struct list *s,struct list *e)
{
int c=1,counter;
printf("\nEnter the location of the node to be deleted:");
scanf("%d",&counter);
node=s->next;
while(node->next!=e)
{
if(c==counter)
break;
c++;
node=node->next;
}
node->prev->next=node->next;
node->next->prev=node->prev;
free(node);
}
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("\nDeleting the First node\n");
delA(start,end);
printf("\n now the listfrom L ->R\n");
displayL(start,end);
printf("\n list from R to L after deletion\n");
displayR(end,start);
getch();
}


C program to delete the first node of a doubly linked list


Previously we have seen how to delete the last node of a doubly linked list. To delete the first node, no traversal is required. Simply connect the left header node with the second node of the doubly linked list to disconnect the node from the list and remove it.

Here is the program

# include < stdio.h >
# include < stdlib.h >
struct list
{
char ch;
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 any character:- ");
scanf(“%c”,&node->ch);
printf("\n Enter choice--'n' for break: ");
ch = getche();
}
node->next = e;
e->prev=node;

}
void displayL (struct list *s,struct list *e)
{
node = s->next;
while (node!=e)
{
printf("\n 0x%x--%c", node,node->ch);
node = node->next;
}
printf("\n");
}
void displayR (struct list *e,struct list *s)
{
node = e->prev;
while (node!=s)
{
printf("\n%c",node->ch);
node = node->prev;
}
printf("\n");
}
void delF(struct list *s)
{
node=s->next;
node->prev->next=node->next;
node->next->prev=node->prev;
free(node);
}
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 Displaying the list from left to right\n");
displayL(start,end);
printf("\n Displaying the list from right to left\n");
displayR(end,start);
printf("\nFirst node deleted\n");
delF(start);
printf("\n now the list from left to right\n");
displayL(start,end);
printf("\nNow the list from right to left\n");
displayR(end,start);
getch();
}

Monday, June 11, 2012

C Program for deletion of node from a circular doubly linked list


Today I will discuss about the program to delete a node from a circular doubly linked list.

#include< stdio.h >
#include< stdlib.h >
struct list
{
int num;
struct list *next;
struct list *prev;
};
struct list *node;
int i=0;
void create(struct list *n)
{
char ch='y';
node=n;
do
{
i++;
fflush(stdin);
node->next=(struct list *)malloc(sizeof(struct list));
node->next->prev=node;
node=node->next;
printf("\Enter value:-");
scanf("%d",&node->num);
fflush(stdin);
printf("\Any more (y/n)");
scanf("%c",&ch);
} while(ch!='n');
node->next=n;
n->prev=node;
}
void displayL(struct list *n)
{
node=n->next;
while(node!=n)
{
printf("%d\n",node->num);
node=node->next;
}
}
void displayR(struct list *n)
{
node=n->prev;
while(node!=n)
{
printf("%d\n",node->num);
node=node->prev;
}
}
void delL(struct list *n)
{
node=n->next;
while(node->next!=n)
{
node=node->next;
}
node->prev->next=node->next;
node->next->prev=node->prev;
free(node);
i--;
}
void delA(struct list *n)
{
int c=1,count;
node=n->next;
while(1)
{
printf("\nWhich location to be deleted:");
scanf("%d",&count);
if(count>=0 && count<=i)
break;
else
printf("\nRe-enter the location, you have only %d nodes.",i);
}
while(node!=n)
{
if(c==count)
break;
node=node->next;
c++;
}
node- >prev- >next=node->next;
node->next->prev=node->prev;
free(node);
}
void delF(struct list *n)
{
struct list *p;
node=n->next;
n->next=node->next;
node->next->prev=n;
free(node);
i--;
}

void main()
{
struct list *start;
char ch='F';
clrscr();
start=(struct list*)malloc(sizeof(struct list));
/* header node without any value */
create(start);
printf("Now display the value of the circular lists(L to R):-\n");
displayL(start);
printf("Now display the value of the circular lists(L to R):-\n");
displayR(start);
while(ch!='E')
{
do
{
printf("\nPress 'F' for first location deletion, 'L' for last location deletion ,\n'A' for any location deletion and 'E' for exit:");
ch=toupper(getche());
}while(strchr("FLAE",ch)==NULL);
if(ch=='F'&& i!=0)
{
delF(start);
printf("\nAfter deletion\n");
printf("Now display the value of the circular lists(L to R):-\n");
displayL(start);
printf("Now display the value of the circular lists(L to R):-\n");
displayR(start);
}
else if(ch=='L'&& i!=0)
{
delL(start);
printf("\nAfter deletion\n");
printf("Now display the value of the circular lists(L to R):-\n");
displayL(start);
printf("Now display the value of the circular lists(L to R):-\n");
displayR(start);
}
else if(ch=='A'&& i!=0)
{
delA(start);
printf("\nAfter deletion\n");
printf("Now display the value of the circular lists(L to R):-\n");
displayL(start);
printf("Now display the value of the circular lists(L to R):-\n");
displayR(start);
}
else if(ch=='E')
break;
else
printf("\nOnly header node is existing, deletion not possible");
}
getch();
}

Detail study of this circular doubly linked list program

  
In this program firstly I have created a header node without any value in the main () function and the header node is passed to create () function as an argument. Prototype of the create () function is void create (struct list *n). In this function, nodes are created and the connection is done using a do while loop.

After creating the circular doubly linked list, the list is displayed from both the ends using displayL () and displayR () functions. Prototype of both the functions are same, void displayL (struct list *n) and void displayR (struct list *n). While loop is used inside the functions body to display the list from both sides.

Next inside the main () function body a while loop is used to take choice from user whether he/she wants to delete any node and from which location. Another do while loop is used inside the body of the above while loop to confirm correct input. Three deletion functions are defined in this program. Function prototypes are follows:
void delA (struct list *n), void delF (struct list *n) and void delL (struct list *n). These three functions are used to delete node from any location, first location and last location of the list and after every deletion operation the list is displayed from both the ends.

Circular Doubly linked list in C Programming


Today I will discuss on circular doubly linked list.

Program on circular doubly linked list without any header node.

 #include< stdio.h >
#include< stdlib.h >
struct list
{
int num;
struct list *next;
struct list *prev;
};
struct list *node;
void create(struct list *n)
{
char ch;
node=n;
printf("\nWant to create location(y/n):-");
scanf("%c",&ch);
fflush(stdin);
while(ch!='n')
{
fflush(stdin);
node->next=(struct list *)malloc(sizeof(struct list));
node->next->prev=node;
node=node->next;
printf("\Enter value:-");
scanf("%d",&node->num);
fflush(stdin);
printf("\Any more (y/n)");
scanf("%c",&ch);
}
node->next=n;
n->prev=node;
}
void display(struct list *n)
{
node=n->next;
while(node!=n)
{
printf("%d\n",node->num);
node=node->next;
}
}
void main()
{
struct list *start=NULL;
clrscr();
create(start);
printf("Now display the value of the circular lists:-\n");
display(start);
getch();
}


 Another program on circular doubly linked list with header node


In this program insertion of new created node at any location of the circular doubly linked list can be done.

#include< stdio.h >
#include< stdlib.h >
struct list
{
int num;
struct list *next;
struct list *prev;
};
struct list *node;
void create(struct list *n)
{
char ch;
node=n;
printf("\nWant to create location(y/n):-");
scanf("%c",&ch);
fflush(stdin);
while(ch!='n')
{
fflush(stdin);
node->next=(struct list *)malloc(sizeof(struct list));
node->next->prev=node;
node=node->next;
printf("\Enter value:-");
scanf("%d",&node->num);
fflush(stdin);
printf("\Any more (y/n)");
scanf("%c",&ch);
}
node->next=n;
n->prev=node;
}
void display(struct list *n)
{
node=n->next;
while(node!=n)
{
printf("%d\n",node->num);
node=node->next;
}
}
void insert(struct list *n)
{
struct list *node,*new1;
int c=1,count;
node=n;
new1=(struct list*)malloc(sizeof(struct list));
printf("\nEnter the location where the new location will be inserted:");
scanf("%d",&count);
printf("\Enter value:-");
scanf("%d",&new1->num);
do
{
if(c==count)
break;
node=node->next;
c++;
}while(node!=n);
new1->next=node->next;
node->next->prev=new1;
node->next=new1;
new1->prev=node;
}

void main()
{
struct list *start;
clrscr();
start=(struct list*)malloc(sizeof(struct list));
/* header node without any value */
create(start);
printf("Now display the value of the circular lists:-\n");
display(start);
insert(start);
printf("\nAfter insertion, the list is\n");
display(start);
getch();
}

Saturday, June 9, 2012

C program to delete last node of a doubly linked list


Insertion and deletion of nodes is very essential in data structure programs, whether it is linear linked list or doubly linked list is immaterial. We have seen that insertion or deletion programs on doubly linked lists are better than linear linked list in most of the cases. Exception insertion or deletion on specific location, traversal of the list is not required in doubly linked list.

We have seen that for deletion last node in linear linked list traversal is a compulsory. In doubly linked list the case is not the same. Here we have to simply access the last node address directly using the extreme right list pointer and connections to be set properly before deleting the last node of the doubly linked list.


# include < stdio.h>
# include < stdio.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);
printf("\n Enter choice--'n' for break: ");
ch = getche();
}
node->next = e;
e->prev=node;

}
void displayL (struct list *s,struct list *e)
{
node = s->next;
while (node!=e)
{
printf("\n 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("\n%s",node->info);
node = node->prev;
}
printf("\n");
}
void delL(struct list *e)
{
node=e->prev;
node->prev->next=node->next;
node->next->prev=node->prev;
free(node);
}
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("\nDeleting the last location\n");
delL(end);
printf("\n now the listfrom L ->R\n");
displayL(start,end);
printf("\n list from R to L after deletion\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.


C Program on insertion of new node at any location of doubly linked list
In my previous post, I have discussed about inserting a node at front and rear end of a doubly linked list. This is the continuation of inserting nodes in doubly linked list. In this program a new node will be inserted at user specified position any location.

# 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 insertA(struct list *s)
{
struct list *new1;
int c=1,count;
printf("\nEnter the location:");
scanf("%d",&count);
fflush(stdin);
new1 = (struct list *) malloc(sizeof(struct list));
printf("\nEnter the new value:");
gets(new1->info);
node=s->next;
while(node)
{
if(c==count)
break;
node=node->next;
c++;
}
node->prev->next=new1;
new1->prev=node->prev;
new1->next=node;
node->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 user specified location\n");
insertA(start);
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();
}

Steps taken for inserting node in doubly linked list at the user specified location.

 1. Allocate new node and enter data
2. Access the location where the new node is to be inserted, here traversal is required but unlike linear linked list only one moving pointer is used here for searching the location.
3. Set the next address pointer of the new node to point the next location of the node which the moving pointer is pointing.
4. Set the previous address pointer of next node to point the new node.
5. Set the next address pointer of node of the doubly linked list pointed by the moving pointer to point the new node.
6. Set the previous address pointer of the new node to point the node of the doubly linked list which is pointed by the moving pointer.

Wednesday, June 6, 2012

C Program on insertion of new node at any location of doubly linked list


In my previous post, I have discussed about inserting a node at front and rear end of a doubly linked list. This is the continuation of inserting nodes in doubly linked list. In this program a new node will be inserted at user specified position any location.

# 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 insertA(struct list *s)
{
struct list *new1;
int c=1,count;
printf("\nEnter the location:");
scanf("%d",&count);
fflush(stdin);
new1 = (struct list *) malloc(sizeof(struct list));
printf("\nEnter the new value:");
gets(new1->info);
node=s->next;
while(node)
{
if(c==count)
break;
node=node->next;
c++;
}
node->prev->next=new1;
new1->prev=node->prev;
new1->next=node;
node->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 user specified location\n");
insertA(start);
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();
}

Steps taken for inserting node in doubly linked list at the user specified location.

 1. Allocate new node and enter data
2. Access the location where the new node is to be inserted, here traversal is required but unlike linear linked list only one moving pointer is used here for searching the location.
3. Set the next address pointer of the new node to point the next location of the node which the moving pointer is pointing.
4. Set the previous address pointer of next node to point the new node.
5. Set the next address pointer of node of the doubly linked list pointed by the moving pointer to point the new node.
6. Set the previous address pointer of the new node to point the node of the doubly linked list which is pointed by the moving pointer.

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.

Subscribe via email

Enter your email address:

Delivered by FeedBurner