Saturday, June 2, 2012

Dynamic insertion of new created nodes at last or any location of a linked list


Today we'll work on programs on linked list where newly created node will be inserted in the last node and at any location.

How to proceed for the program on insertion of new node at last location

 To insert a new node at the rear end of a linked list, you have to create a node and then with the help of loop, traverse through the linked list to find the last node. In the next pointer of the last node assign the address of the new node and at the same time assign NULL pointer at the internal or next address pointer of the last node..

# include < stdio.h>
# include < stdlib.h>
struct link
{
char info[20];
struct link *next;
};

int i;
int number;
struct link *start=NULL, *p, *new1,*node;
void create()
{
char ch='y';
start = (struct link* ) malloc(sizeof(struct link));
printf("\n Input the node: %d: ", (i+1));
gets(start->info);
start->next = NULL;
node=start;
/* Point to the start of the list */
i = 0;
while(ch != 'n')
{
node->next = (struct link* ) malloc(sizeof(struct link));
node = node->next;
printf("\n Input the node: %d: ", (i+1));
gets(node->info);
node->next = NULL;
fflush(stdin);
printf("\n Input choice n for break: ");
ch = getche();
i++;
}
}
void insertion()
{
node = start->next;
p = start;
while(node)
{
node = node->next;
p= p->next;
}
if(node == NULL)
{
new1 = (struct link* ) malloc(sizeof(struct link));
new1->next = node ;
p->next = new1;
printf("\n Input the last node value: ");
gets(new1->info);
}
}
void display()
{
node=start;
/*node is holding the address of the first node of the linked list*/
while (node)
{
printf(" %s", node->info);
node = node->next;
}
}
void main()
{
clrscr();
create();
printf("\n Created list is as follows:\n");
display();
insertion();
printf("\n After Inserting a node list is as follows:\n");
display();
getch();
}

Analysis of the above program

 In this above program all the structure type pointers used to traverse the linked list are declared external variables, means they can be accessed from any function of the program.

‘start’ is the list pointer of this linked list. List pointer is the pointer which holds the base address of the linked list. The other moving external pointers of this program are ‘node’ and ‘p’. Within the function ‘void create()’, first node of the linked list is created outside the loop body and afterwards nodes are created as per choice of user.

Insertion of a new node at the end of the linked list is performed in ‘void insert ()’ function. Two moving pointers ‘node’ and ‘p’ are made to point the 1st node and 2nd node of the linked list respectively. As we know that internal pointer of the last node of a linked list contains NULL, both the pointers are allowed to traverse the linked list until pointer ‘p’ reaches NULL. At this point the other pointer ‘node’ is pointing the last node of the linked list. The newly created node is inserted in the linked list in between the locations pointed by ‘node’ and ‘p’.

2 comments:

  1. WAP to display automorphic numbers from 1 to n by using functions

    ReplyDelete
    Replies
    1. Please keep in touch, program will be posted within 1-2 days.

      Delete

Subscribe via email

Enter your email address:

Delivered by FeedBurner