Tuesday, June 12, 2012

C Program on linked list using address of list pointer


I have posted number of programs on new node insertion and deletion of node from linked list. Those programs were either using header node in linked list or normal linked list. In linked list with header node the insertion at first location or deletion from the first location can be carried out easily but in linked list without header node, inserting new node at first location or deleting node from first location invokes some precautions. In both the cases the function is defined as returning address of the first node of the linked list after the operation is completed.

Today I will show you how to execute the operations of deletion and insertion on linked list even at the first location without returning values from the function. I have passed the address of the pointer holding the address of first node of the linked list to each function. So after deletion or insertion of node at starting location of the linked list, the address of the first node is assigned to the pointer to pointer and no need to return the new address of the first node of the linked list to void main.


# include < stdio.h>
# include < stdlib.h>
struct list
{
int age;
struct list *next;
};
struct list *new1,*node,*p;
void create(struct list **s)
{
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 = node->next;
printf("\n Enter the numeric value:- ");
scanf("%d",&node->age);
printf("\n Enter choice--'n' for break: ");
ch = getche();
}
node->next = NULL;
}
void displayList (struct list **s)
{
node=*s;
while (node!=NULL)
{
printf("\n%d", node->age);
node = node->next;
}
printf("\n");
}
void delA(struct list **s)
{
int c=1,counter;
printf("\nEnter the location of the node to be deleted:");
scanf("%d",&counter);
if(c==counter)
{
node=(*s)->next;
free(*s);
s=&node;
}
else
{
node=*s;
p=node->next;
while(p->next!=NULL)
{
if(c+1==counter)
break;
c++;
node=node->next;
p=p->next;
}
node->next=p->next;
free(p);
}
}
void insA(struct list **s)
{
int c=1,counter;
printf("\nEnter the location where to insert the new node:");
scanf("%d",&counter);
new1=(struct list *) malloc(sizeof(struct list));
printf("\n Enter the numeric value:- ");
scanf("%d",&new1->age);

if(c==counter)
{
new1->next=*s;
s=&new1;
}
else
{
node=*s;
p=node->next;
while(p!=NULL)
{
if(c+1==counter)
break;
c++;
node=node->next;
p=p->next;
}
node->next=new1;
new1->next=p;
}
}

void main()
{
struct list *start;
clrscr();
start=(struct list *) malloc(sizeof(struct list));
printf("\n Enter the numeric value:- ");
scanf("%d",&start->age);

create(&start);
printf("\n Created list is as follows\n");
displayList(&start);
printf("\nDeleting any node\n");
delA(&start);
printf("\n now the list is\n");
displayList(&start);
printf("\nInserting new node\n");
insA(&start);
printf("\n now the list is\n");
displayList(&start);
getch();
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner