Page 54 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 54
Unit 2: Linked Lists
{ Notes
printf(“Error\n”);
exit(0);
}
p-> data = n;
p-> link = p; /* makes the pointer pointing to itself because it
is a circular list*/
}
else
{
temp = p;
/* traverses the existing list to get the pointer to the last node of
it */
while (temp-> link != p)
temp = temp-> link;
temp-> link = (struct node *)malloc(sizeof(struct node)); /*
creates new node using
data value passes as
parameter and puts its
address in the link field
of last node of the
existing list*/
if(temp -> link == NULL)
{
printf(“Error\n”);
exit(0);
}
temp = temp-> link;
temp-> data = n;
temp-> link = p;
}
return (p);
}
void printlist ( struct node *p )
{
struct node *temp;
temp = p;
printf(“The data values in the list are\n”);
if(p!= NULL)
{
LOVELY PROFESSIONAL UNIVERSITY 49