您的位置:首页 > 编程语言 > C语言/C++

C++实现单链表(2) 一些函数的实现

2015-11-20 00:46 483 查看
void PrintTailtoHead(ListNode * pHead) //逆向输出链表

{

if (pHead == NULL)

{

return;

}

else

{

PrintTailtoHead(pHead->_next);

printf("%d->", pHead->_data);

}

/*

if(pHead)

{

PrintTailtoHead(pHead->_next);

printf("%d->", pHead->_data);

}

*/

}

ListNode * SearchMidNode(ListNode * pHead) //寻找中间结点

{

ListNode * fast = pHead, *slow = pHead;

assert(pHead);

while (fast!=NULL && fast->_next != NULL)

{

fast = fast->_next;

slow = slow->_next;

fast = fast->_next;

}

return slow; //如果是一个结点直接返回slow

}

void PrevInsert(ListNode* &pos, DataType x) //无头链表的前插 先后插再交换数据

{

assert(pos);

ListNode *tmp = BuyNode(x);

tmp->_next = pos->_next;

pos->_next = tmp;

//利用没有用的x 节省空间

x = pos->_data;

pos->_data = tmp->_data;

tmp->_data = x;

/*

assert(pos);

ListNode *tmp = BuyNode(pos->_data);

tmp->_next = pos->_next;

pos->_next = tmp;

//利用没有用的x 节省空间

pos->_data=x;

*/

}

//链表冒泡排序

void BubbleSort(ListNode * pHead)

{

ListNode * post = NULL; //设置监视哨

ListNode * cur = pHead;

int flag = 0;

if (cur == NULL||cur->_next==NULL)

return;

while (1)

{

cur = pHead;

flag = 0;

while (cur->_next != post)

{

if (cur->_data > cur->_next->_data)

{

//交换

flag = 1;

DataType tmp = cur->_data;

cur->_data = cur->_next->_data;

cur->_next->_data = tmp;

}

cur = cur->_next;

}

post = cur;

if (!flag||post==pHead)

break;

}

}

//归并排序

ListNode * MergeList(ListNode * pHead1, ListNode * pHead2)

{

ListNode *pList1Cur=pHead1, *pList2Cur=pHead2;

if (pHead1 == NULL)

return pHead2;

if (pHead2 == NULL)

return pHead2;

ListNode * newHead;

if (pHead1->_data <= pHead2->_data)

{

newHead = pHead1;

pList1Cur = pList1Cur->_next;

}

else

{

newHead = pHead2;

pList2Cur = pList2Cur->_next;

}

ListNode* tail = newHead;

while (pList1Cur&&pList2Cur)

{

if (pList1Cur->_data < pList2Cur->_data)

{

tail->_next = pList1Cur;

pList1Cur = pList1Cur->_next;

}

else

{

tail->_next = pList2Cur;

pList2Cur = pList2Cur->_next;

}

tail = tail->_next;

}

if (pList1Cur!=
NULL)

{

tail->_next = pList1Cur;

}

else

{

tail->_next = pList2Cur;

}

return newHead;

}

ListNode * FindTailNode(ListNode * pHead, int k) //寻找倒数第k个结点

{

ListNode * fast = pHead, *slow = pHead;

int count = k;

while (count--)

{

if (fast == NULL)

return NULL;

fast = fast->_next;

}

while (fast != NULL)

{

fast = fast->_next;

slow = slow->_next;

}

return slow;

}

//判断一个单链表带环 注意不要出现NULL==NULL这种情况

//快慢指针解决

int IsCycle(ListNode *pHead) //带环返回1 不带环返回0 错误返回-1

{

ListNode *fast = pHead, *slow = pHead; //快慢指针

if (pHead == NULL)

return -1;

fast = fast->_next->_next;

slow = slow->_next;

while (fast != NULL&&fast->_next != NULL&&fast != slow)

{

fast = fast->_next->_next;

slow = slow->_next;

}

if (fast == slow)

return 1;

else

return 0;

}

//带环则环中的一个结点指针 不带环返回NULL 判断是否有环的另一个版本

ListNode* IsCycle2(ListNode *pHead)

{

ListNode *fast = pHead, *slow = pHead; //快慢指针

if (pHead == NULL)

return NULL;

fast = fast->_next->_next;

slow = slow->_next;

while (fast != NULL&&fast->_next != NULL&&fast != slow)

{

fast = fast->_next->_next;

slow = slow->_next;

}

if (fast == slow)

return fast;

else

return NULL;

}

int CycleLength(ListNode *pHead) //返回环的长度 没有环返回0 有环返回 1 - n

{

ListNode *fast = pHead, *slow = pHead; //快慢指针

if (pHead == NULL)

return -1;

fast = fast->_next->_next;

slow = slow->_next;

while (fast != NULL&&fast->_next != NULL&&fast != slow)

{

fast = fast->_next->_next;

slow = slow->_next;

}

if (fast == slow)

{

ListNode *sign = fast;

int count = 0;

fast = fast->_next;

while (sign != fast)

{

count++;

fast = fast->_next;

}

return count+1;

}

return 0;

}

//另一个版本的求环长度的函数

int CycleLength2(ListNode *pHead)

{

ListNode * node = IsCycle2(pHead);

if (node == NULL)

return 0;

else

{

int count = 0;

ListNode * tmp = node->_next;

while (tmp != node)

{

count++;

tmp = tmp->_next;

}

return count + 1;

}

}

ListNode * Entrance(ListNode *pHead) //求环入口的结点 返回指向入口结点的指针

{

ListNode * entrance = NULL; //返回值

//首先先找到环的一个指针 把它当作一个链表的头

ListNode* CycleHead = IsCycle2(pHead);

//然后求这个“链表”和pHead两个的交点即可

//当然,不能用写好的判断一般链表是否相交的函数 因为链表没有尾

//大前提是已经有环了 若无环返回NULL

ListNode * cur1 = pHead, *cur2 = CycleHead;

//这样做费时

while (cur1 != cur2&&cur1!=NULL)

{

int len = CycleLength2(CycleHead); //获取环长度

while (cur1 != cur2&&len--)

{

cur2 = cur2->_next;

}

if (cur1 == cur2)

return cur1;

else

cur1 = cur1->_next;

}

if (cur1 == cur2)

return cur1;

return NULL;

}

//判断两个链表是否相交

int IsIntersect(ListNode* pHead1, ListNode* pHead2)

{

ListNode * cur1 = pHead1, *cur2 = pHead2;

ListNode * prevCur1=pHead1,* prevCur2=pHead2;

if (cur1 == NULL)

return 0;

if (cur2 == NULL)

return 0;

while (cur1 != NULL&&cur2 != NULL&&prevCur1!=prevCur2)

{

prevCur1 = cur1;

prevCur2 = cur2;

cur1 = cur1->_next;

cur2 = cur2->_next;

}

if (prevCur1 == prevCur2)

return 1;

else

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: