I need the solution

void InsertAttheend(int x)
{
struct Node* temp;
struct Node* loc;
temp=new Node();
temp->data=x;
temp->next=head;
loc=head;
if(head==NULL)
{
head=temp;
}
else
{
while(loc->next!=head)
{
loc=loc->next;
}
loc->next=temp;
}
}





Here I face the problem when I want to add a node at the end of the circular linked list....
Please anyone help me
So you need to

a. Keep track of the end of your linked list as it grows.
or
b. Write a function that will traverse your list and return a count of nodes, giving you the size of your list.

Then once you know how big your list is, write a function that will

insert at the end of your list. e.g add a new node at the end, and connect it to the beginning of the list to keep it circular.
> the end of the circular linked list
I wonder where that is.


Your code fails to create a list with a single element.
iirc when the list is empty head==NULL so you've got
1
2
3
4
5
6
7
temp=new Node();
temp->data=x;
temp->next=head; //temp->next = NULL
loc=head;
if(head==NULL) //true
{
   head=temp;
`head->next' does not points to `head', but to `NULL'. Your list is not circular.
Topic archived. No new replies allowed.