// 凭第一感觉写的版本,算法和思路有好的想法再优化
/* 交换链表任意两个元素 */
void SwapItem(LinkedList *head, int posA, int posB)//从非头结点开始交换操作
{
//保证至少存在两个节点
assert(head && head->next && head->next->next);
assert(posA>=1 && posB>=1);
if (posA == posB)
{
printf("\nerror: %d,交换位置相同,不做任何操作!\n", __LINE__);
getchar();
//return;
exit(1);
}
//保证交换位置合理性,参数2,3写反位置也没关系
int first,second;
if (posA<posB)
{
first = posA, second = posB;
}
else
{
first = posB;
second = posA;
}
if ((second - first) == 1) //A与B是相邻的两个节点
{
LinkedList *prev, *nodeA, *nodeB, *post;
int i = 1;
prev = head;
nodeA = prev->next;
nodeB = nodeA->next;
while (i<first && nodeB) //定位到节点A、B的位置
{
prev = prev->next;
no