# C++实现单链表（2） 一些函数的实现

166人阅读 评论(0)

{
if (pHead == NULL)
{
return;
}
else
{
}
/*
{
}
*/
}
ListNode * SearchMidNode(ListNode * pHead)  //寻找中间结点
{
ListNode * fast = pHead, *slow = 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)
{
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;
break;

}
}
//归并排序
ListNode * MergeList(ListNode * pHead1, ListNode * pHead2)
{
if (pHead1 == NULL)
if (pHead2 == NULL)
{
pList1Cur = pList1Cur->_next;
}
else
{
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;
}
}

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 *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;
}

//另一个版本的求环长度的函数
{
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;   //返回值
//首先先找到环的一个指针 把它当作一个链表的头
//当然，不能用写好的判断一般链表是否相交的函数 因为链表没有尾
//大前提是已经有环了 若无环返回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;
}

//判断两个链表是否相交
{
ListNode * cur1 = pHead1, *cur2 = 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;
}

2
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：10812次
• 积分：440
• 等级：
• 排名：千里之外
• 原创：32篇
• 转载：2篇
• 译文：0篇
• 评论：2条
文章分类
评论排行
最新评论