1、在无头单链表的一个非头结点前插入一个结点
2、逆置链表
单链表结构以及Find函数参见 2016-1-2 13:56 发表博客
void InsertFront(SListNode*pos, const DataType x) //在无头单链表的一个非头结点前插入一个结点 { //初步思路:在pos后插入元素,再与pos的值互换(若传入的是头结点则也可运行) //还能进行优化为:在pos后插入值为pos->data的结点,再将pos值变为x SListNode *tmp = _BuyNode(pos->data); tmp->next = pos->next; pos->next = tmp; pos->data = x; } void Turn(SListNode*&pHead) //逆置链表 { //思路:从第二个开始顺次将结点放在头结点位置 //assert(pHead);//考虑到可能传入NULL时也合理,所以在这里不能使用断言assert if (pHead) { SListNode*NewHead = pHead; SListNode*cur = pHead; while (cur->next) { SListNode*tmp = cur->next; cur->next = tmp->next; tmp->next = NewHead; NewHead = tmp; } pHead = NewHead; } } void Test5() //InsertFront/Turn { printf("//Test5() InsertFront/Turn \n"); SListNode *LL = NULL; PushBack(LL, 1); PushBack(LL, 2); PushBack(LL, 3); PushBack(LL, 5); PrintNode(LL); InsertFront(Find(LL,1),0); PrintNode(LL); InsertFront(Find(LL, 5), 4); PrintNode(LL); Turn(LL); PrintNode(LL); }