向循环有序链表内插入一个节点

Node* insert(Node* aNode, int x)
{
	if (aNode == NULL)
	{
		aNode = new Node(x);
		aNode->next = aNode;
		return aNode;
	}

	Node *prev = aNode;
	Node *p = prev->next;

	do 
	{
        if (prev->value <= x && x <= p->value)
		{
			break;
		}
		else if (prev->value >= p->value && (x >= prev->value || x <= p->value))
		{
			break;
		}
		else
		{
			prev = p;
			p = prev->next;
		}
	}
	while (prev != aNode);

	Node *newNode = new Node(x);
	newNode->next = prev->next;
	prev->next = newNode;

	return aNode;
}

你可能感兴趣的:(向循环有序链表内插入一个节点)