In the following code:
void insert(Node *& aNode, int x) {
if (!aNode) {
aNode = new Node(x);
aNode->next = aNode;
return;
}
Node *p = aNode;
Node *prev = NULL;
do {
prev = p;
p = p->next;
if (x <= p->data && x >= prev->data) break; // For case 1)
if ((prev->data > p->data) && (x < p->data || x > prev->data)) break; // For case 2)
} while (p != aNode); // when back to starting point, then stop. For case 3)
Node *newNode = new Node(x);
newNode->next = p;
prev->next = newNode;
}
What is Node *& aNode?
How should I use this function, I mean, which type of parameter should I pass?
I think this code is C++, not C, and
Node *&aNodeis a reference to a pointer to aNode, so you would pass aNode*to the function, and function would make a reference to that (so the memory location yourNode*is pointing to can change).You may find the Wikipedia article on References (C++) interesting.
A simple example:
@crashmstr’s comment reminded me that I should say how they’re different from pointers. Wikipedia does a better job that I could though: