这是链表的基本操作以及链表的面试题的全部内容,可供大家下载哟!
里面也写好了测试函数,大家可以看一下,测试一下!
直接上代码:
头文件:
#ifndef __LINKLIST_H__
#define __LINKLIST_H__
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int DataType;
typedef struct Node
{
DataType data;
struct Node* next;
}Node, *pNode, List, *pList;
typedef struct ComplexNode
{
DataType data;
struct ComplexNode* next;
struct ComplexNode* random;
}ComplexNode;
void InitLinkList(pList* ppList);
void DestoryLinkList(pList* ppList);
void PushBack(pList* ppList, DataType d);
void PopBack(pList* ppList);
void PushFront(pList* ppList, DataType d);
void PopFront(pList* ppList);
pNode Find(pList pList, DataType d);
void Insert(pList* ppList, pNode pos,DataType d);
void Erase(pList* ppList, pNode pos);
void Remove(pList* ppList, DataType d);
void RemoveAll(pList* ppList, DataType d);
void EraseNotTailNode(pNode pos);
void PrintLinkList(pList pList);
int GetLinkListLength(pList pList);
//链表面试题
//1.逆序输出链表
void PrintTailToHead(pList pList);
void PrintTailToHead_recursion(pList pList);
//2.逆序链表(遍历一次,时间复杂度:O(n))
void ReverseLinkList(pList* ppList);
//3.没有头指针删除一个不是尾结点的结点(替换删除)
void EraseNodeNotTail(pNode pos);
//4.没有头指针插入一个结点(替换插入)
void InsertNode(pNode pos, DataType d);
//5.约瑟夫环
void JosephusCycle(pList* ppList, int k);
//6.单链表的排序(冒泡排序)
void BubbleSort(pList pList);
//7.合并两个有序链表,合并后依然有序
pList Merge(pList pList1, pList pList2);
//递归
pList Merge_R(pList pList1, pList pList2);
//8.查找链表的中间结点,要求只能遍历一次链表
pNode FindMidNode(pList pList);
//9.查找单链表的倒数第k个结点,要求只能遍历一次链表
pNode FindLastKNode(pList pList, int k);
//10.判断单链表是否带环?若带环,求环的长度?求环的入口点
pNode CheckCycle(pList pList);
int GetCycleLength(pList meet);
pNode StartCycle(pList pList, pNode meet);
//11.判断两个链表是否相交,若相交,求交点
bool CheckMeet(pList pList1, pList pList2);
pNode GetMeet(pList pList1, pList pList2);
//12.复杂链表的复制
//一个链表的每个节点,有一个指向next指针指向下一个节点,
//还有一个random指针指向这个链表中的一个随机结点或者NULL
//现在要求实现复制这个链表,返回复制后的新链表
ComplexNode* BuyNode_Complex(DataType d);
void PrintComplexList(ComplexNode* node1);
ComplexNode* CopyComplexList(ComplexNode* List);
//13.求两个有序单链表的交集以及差集
pList Intersection(pList pList1, pList pList2);
pList Difset(pList pList1, pList pList2);
#endif //__LINKLIST_H__
函数的定义:
#include "LinkLIst.h"
void InitLinkList(pList* ppList)
{
assert(ppList != NULL);
*ppList = NULL;
}
void DestoryLinkList(pList* ppList)
{
pNode del = NULL;
pNode cur = NULL;
assert(ppList != NULL);
cur = *ppList;
while (cur != NULL)
{
del = cur;
cur = cur->next;
free(del);
del = NULL;
}
*ppList = NULL;
}
void PrintLinkList(pList pList)
{
pNode cur = pList;
while (cur != NULL)
{
printf("%d-->", cur->data);
cur = cur->next;
}
printf("over\n");
}
int GetLinkListLength(pList pList)
{
int count = 0;
if (pList == NULL)//无结点
{
return 0;
}
else//有结点
{
while (pList != NULL)
{
count++;
pList = pList->next;
}
return count;
}
}
pList BuyNode(DataType d)
{
pNode newNode = (pNode)malloc(sizeof(Node));
newNode->data = d;
newNode->next = NULL;
return newNode;
}
void PushBack(pList* ppList, DataType d)
{
pNode newNode = NULL;
assert(ppList != NULL);
newNode = BuyNode(d);
if (*ppList == NULL) //第一个结点的插入
{
*ppList = newNode;
}
else //多于一个结点
{
pNode cur = *ppList;
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = newNode;
}
}
void PopBack(pList* ppList)
{
assert(ppList != NULL);
if (*ppList == NULL)//空
{
return;
}
else if ((*ppList)->next == NULL)//一个结点
{
pNode del = *ppList;
*ppList = del->next;
free(del);
del = NULL;
}
else//大于一个结点
{
pNode cur = *ppList;
while (cur->next->next != NULL)
{
cur = cur->next;
}
free(cur->next);
cur->next = NULL;
}
}
void PushFront(pList* ppList, DataType d)
{
pNode newNode = NULL;
assert(ppList != NULL);
newNode = BuyNode(d);
newNode->next = *ppList;
*ppList = newNode;
}
void PopFront(pList* ppList)
{
assert(ppList != NULL);
if (*ppList == NULL)//无结点,不进行删除
{
return;
}
else
{
pNode del = *ppList;
*ppList = del->next;
free(del);
del = NULL;
}
}
pNode Find(pList pList, DataType d)
{
if (pList == NULL)//无结点
{
return NULL;
}
else//有结点
{
pNode cur = pList;
while (cur && cur->data != d)
{
cur = cur->next;
}
if (cur != NULL)//找到
{
return cur;
}
else//没找到
{
return NULL;
}
}
}
void Insert(pList* ppList, pNode pos, DataType d)
{
pNode newNode = NULL;
assert(ppList != NULL);
assert(pos != NULL);
newNode = BuyNode(d);
if (*ppList == NULL)//无结点
{
return;
}
else //有结点
{
pNode cur = *ppList;
if (pos == cur)//pos是第一个结点
{
newNode->next = *ppList;
*ppList = newNode;
}
else//有多于一个结点
{
while (cur->next != pos)
{
cur = cur->next;
}
newNode->next = cur->next;
cur->next = newNode;
}
}
}
void Erase(pList* ppList, pNode pos)
{
assert(ppList != NULL);
assert(pos != NULL);
if (*ppList == NULL)//无结点
{
return;
}
else //有结点
{
pNode cur = *ppList;
if (pos == cur)//pos是第一个结点
{
pNode del = *ppList;
*ppList = del->next;
free(del);
del = NULL;
}
else//有多于一个结点
{
while ((cur->next != NULL) && (cur->next != pos))
{
cur = cur->next;
}
if (cur->next != NULL)//找到要删除的结点
{
cur->next = pos->next;
free(pos);
pos = NULL;
}
}
}
}
void Remove(pList* ppList, DataType d)
{
assert(ppList != NULL);
if (*ppList == NULL)//无结点
{
return;
}
else//有结点
{
pNode cur = *ppList;
if (cur->data == d)//第一个结点就是要删除节点
{
pNode del = cur;
*ppList = del->next;
free(del);
del = NULL;
}
else //第一个结点不是要删除节点
{
while ((cur->next != NULL) && (cur->next->data != d))
{
cur = cur->next;
}
if (cur->next != NULL)//找到要删除的结点
{
pNode del = cur->next;
cur->next = del->next;
free(del);
del = NULL;
}
}
}
}
void RemoveAll(pList* ppList, DataType d)
{
assert(ppList != NULL);
if (*ppList == NULL)//无结点
{
return;
}
else//有结点
{
pNode cur = *ppList;
while (cur != NULL)
{
if (cur->data == d)//找到要删的结点
{
pNode del = NULL;
if (cur == *ppList)//要删除的结点是第一个
{
del = cur;
cur = cur->next;
*ppList = del->next;
free(del);
del = NULL;
}
else //要删除的结点不是第一个
{
pNode prev = *ppList;
while (prev->next != cur)
{
prev = prev->next;
}
del = cur;
cur = cur->next;
prev->next = del->next;
free(del);
del = NULL;
}
}
else
{
cur = cur->next;
}
}
}
}
void EraseNotTailNode(pNode pos)
{
pNode del = NULL;
assert(pos != NULL);
del = pos->next;
pos->data = del->data;
pos->next = del->next;
free(del);
del = NULL;
}
//递归写法
void PrintTailToHead_recursion(pList pList)
{
if (pList == NULL)
{
return;
}
PrintTailToHead_recursion(pList->next);
printf("%d ", pList->data);
}
//非递归写法
void PrintTailToHead(pList pList)
{
pNode cur = NULL;
pNode tail = NULL;
while (tail != pList)
{
cur = pList;
while (cur->next != tail)
{
cur = cur->next;
}
tail = cur;
printf("%d-->", cur->data);
}
printf("over\n");
}
//逆置/反转单链表
void ReverseLinkList(pList* ppList)
{
pNode cur = NULL;
pNode tmp = NULL;
assert(ppList != NULL);
cur = *ppList;
tmp = cur->next;
if ((cur == NULL) && (cur->next == NULL))//有一个结点或者没有节点
{
return;
}
else//大于一个结点
{
pNode head = NULL;
while (cur != NULL)
{
cur->next = head;
head = cur;
cur = tmp;
if (tmp != NULL)
{
tmp = tmp->next;
}
}
*ppList = head;
}
}
void EraseNodeNotTail(pNode pos)
{
pNode del = NULL;
assert(pos != NULL);
//替换删除法
del = pos->next;
pos->data = del->data;
pos->next = del->next;
free(del);
del = NULL;
}
void InsertNode(pNode pos, DataType d)
{
pNode newNode = NULL;
assert(pos != NULL);
//替换插入法
newNode = BuyNode(pos->data);
newNode->next = pos->next;
pos->next = newNode;
pos->data = d;
}
//void JosephusCycle(pList* ppList, int k)
//{
// pNode tail = NULL;
// pNode cur = NULL;
// assert(ppList != NULL);
// tail = *ppList;
// cur = *ppList;
//
// //建环
// while (tail->next != NULL)
// {
// tail = tail->next;
// }
// tail->next = *ppList;
//
// //求幸存者
// while (cur != cur->next)
// {
// pNode del = NULL;
// int count = k;
// while (--count)
// {
// cur = cur->next;
// }
// printf("自杀:%d\n", cur->data);
// //替换删除
// del = cur->next;
// cur->data = del->data;
// cur->next = del->next;
// free(del);
// del = NULL;
// }
// printf("幸存者:%d\n", cur->data);
//}
//约瑟夫环(练习)
void JosephusCycle(pList* pplist, int k)
{
pNode tail = NULL;
pNode cur = NULL;
assert(pplist != NULL);
//建环
cur = *pplist;
tail = *pplist;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = cur;
//进行运算
while (cur != cur->next)
{
//删除第k个结点
int count = k;//保证每次k不会改变
while (--count)
{
cur = cur->next;
}
printf("自杀:%d ", cur->data);
printf("\n");
//替换删除法
pNode del = cur->next;
cur->data = del->data;
cur->next = del->next;
free(del);
del = NULL;
}
printf("幸存者:%d\n", cur->data);
}
void BubbleSort(pList pList)
{
pNode cur = NULL;
pNode next = NULL;
pNode tail = NULL;
for (cur = pList; cur->next != NULL; cur = cur->next)//次数
{
for (next = pList; next->next != tail; next = next->next)
{
if (next->data > next->next->data)
{
DataType tmp = next->data;
next->data = next->next->data;
next->next->data = tmp;
}
}
tail = next;
}
}
pList Merge(pList pList1, pList pList2)
{
pList newList = NULL;
pNode tail = NULL;
pNode cur1 = pList1;
pNode cur2 = pList2;
if (cur1->data < cur2->data)
{
newList = cur1;
tail = cur1;
cur1 = cur1->next;
}
else
{
newList = cur2;
tail = cur2;
cur2 = cur2->next;
}
while (cur1 != NULL && cur2 != NULL)
{
if (cur1->data < cur2->data)
{
tail->next = cur1;
tail = cur1;
cur1 = cur1->next;
}
else
{
tail->next = cur2;
tail = cur2;
cur2 = cur2->next;
}
}
if (cur1 != NULL)
{
tail->next = cur1;
}
else
{
tail->next = cur2;
}
return newList;
}
pList Merge_R(pList pList1, pList pList2)
{
pNode newList = NULL;
if (pList1 == NULL)
{
return pList2;
}
else if (pList2 == NULL)
{
return pList1;
}
else
{
if (pList1->data > pList2->data)
{
newList = pList2;
newList->next = Merge_R(pList1, pList2->next);
}
else
{
newList = pList1;
newList->next = Merge_R(pList1->next, pList2);
}
return newList;
}
}
//快慢遍历法
pNode FindMidNode(pList pList)
{
pNode fast = pList;
pNode slow = pList;
while (fast != NULL && fast->next != NULL)
{
fast = fast->next->next;
slow = slow->next;
}
return slow;
}
pNode FindLastKNode(pList pList, int k)
{
pNode fast = pList;
pNode slow = pList;
if (GetLinkListLength(pList) < k)
{
return NULL;
}
else
{
//快的先走k-1次
while (--k)
{
fast = fast->next;
}
//同时走
while (fast->next != NULL)
{
fast = fast->next;
slow = slow->next;
}
return slow;
}
}
pNode CheckCycle(pList pList)
{
pNode fast = pList;
pNode slow = pList;
fast = fast->next->next;
slow = slow->next;
while (slow != fast && fast != NULL)
{
fast = fast->next->next;
slow = slow->next;
}
if (fast == NULL)
{
return NULL;
}
return slow;
}
int GetCycleLength(pList meet)
{
int len = 1;
pNode cur = meet->next;
while (cur != meet)
{
cur = cur->next;
len++;
}
return len;
}
pNode StartCycle(pList pList, pNode meet)
{
while (pList != meet)
{
pList = pList->next;
meet = meet->next;
}
return pList;
}
bool CheckMeet(pList pList1, pList pList2)
{
bool flag = true;
while (pList1->next != NULL)
{
pList1 = pList1->next;
}
while (pList2->next != NULL)
{
pList2 = pList2->next;
}
if (pList1 == pList2)
{
return true;
}
else
{
return false;
}
}
pNode GetMeet(pList pList1, pList pList2)
{
int len1 = GetLinkListLength(pList1);
int len2 = GetLinkListLength(pList2);
int d = len1 - len2 > 0 ? len1 - len2 : len2 - len1;
if (len1 > len2)
{
while (d--)
{
pList1 = pList1->next;
}
}
else
{
while (d--)
{
pList2 = pList2->next;
}
}
while (pList1 != pList2)
{
pList1 = pList1->next;
pList2 = pList2->next;
}
return pList1;
}
ComplexNode* BuyNode_Complex(DataType d)
{
ComplexNode* newNode = (ComplexNode*)malloc(sizeof(ComplexNode));
if (newNode == NULL)
{
printf("error!");
}
newNode->data = d;
newNode->next = NULL;
newNode->random = NULL;
return newNode;
}
void PrintComplexList(ComplexNode* node1)
{
while (node1 != NULL)
{
printf("%d:", node1->data);
if (node1->random != NULL)
{
printf("%d-->", node1->random->data);
}
else
{
printf("NULL-->");
}
node1 = node1->next;
}
printf("over!\n");
}
ComplexNode* CopyComplexList(ComplexNode* List)
{
ComplexNode* cur = List;
ComplexNode* next = NULL;
ComplexNode* newList = NULL;
//给每一个结点后面再加一个结点
while (cur != NULL)
{
ComplexNode* newNode = BuyNode_Complex(cur->data);
newNode->next = cur->next;
cur->next = newNode;
cur = newNode->next;
}
//增加random指针域,新的结点的random指向原先random的next
cur = List;
next = cur->next;
while (next->next != NULL)
{
next->random = cur->random->next;
cur = next->next;
next = cur->next;
}
//断开原先的结点与现在结点的连接,将现在的结点连接起来
cur = List->next;
next = cur->next;
newList = cur;
while (next != NULL)
{
cur->next = next->next;
cur = next->next;
next = cur->next;
}
//结束,返回复制的复杂链表
return newList;
}
pList Intersection(pList pList1, pList pList2)
{
if (pList1 == NULL || pList2 == NULL)
{
return NULL;
}
pList newList = BuyNode(0);//添加头结点,这样就不用在后面进行第一次找相等的节点了,可以统一查找
pNode tail = newList;
while (pList1 != NULL && pList2 != NULL)
{
//两个数值相等,同时进行
if (pList1->data == pList2->data)
{
tail->next = pList1;
tail = pList1;
pList1 = pList1->next;
pList2 = pList2->next;
}
//pList1->data > pList2->data,pList1向前进行
else if (pList1->data < pList2->data)
{
pList1 = pList1->next;
}
else
{
pList2 = pList2->next;
}
}
return newList;
}
pList Difset(pList pList1, pList pList2)
{
if (pList1 == NULL)
{
return pList2;
}
if (pList2 == NULL)
{
return pList1;
}
pList newList = BuyNode(0);
pNode tail = newList;
while (pList1 != NULL && pList2 != NULL)
{
if (pList1->data == pList2->data)
{
pList1 = pList1->next;
pList2 = pList2->next;
}
else if (pList1->data < pList2->data)
{
tail->next = pList1;
tail = pList1;
pList1 = pList1->next;
}
else
{
tail->next = pList2;
tail = pList2;
pList2 = pList2->next;
}
}
if (pList1 != NULL)
{
tail->next = pList1;
}
else
{
tail->next = pList2;
}
return newList;
}
最后就是测试函数了:
#include "LinkLIst.h"
void TestBackLinkList()
{
int count = 0;
pList pList = NULL;
InitLinkList(&pList);
PushBack(&pList, 1);
PushBack(&pList, 2);
PushBack(&pList, 3);
PushBack(&pList, 4);
count = GetLinkListLength(pList);
printf("链表长度为:%d\n", count);
PrintLinkList(pList);// 1 2 3 4
PopBack(&pList);
PrintLinkList(pList);//1 2 3
PopBack(&pList);
PrintLinkList(pList);//1 2
PopBack(&pList);
PrintLinkList(pList);//1
DestoryLinkList(&pList);//over
PrintLinkList(pList);
}
void TestFrontLinkList()
{
pList pList = NULL;
InitLinkList(&pList);
PushFront(&pList, 1);
PushFront(&pList, 2);
PushFront(&pList, 3);
PushFront(&pList, 4);
PrintLinkList(pList);//4 3 2 1
PopFront(&pList);
PrintLinkList(pList);//3 2 1
DestoryLinkList(&pList);
PrintLinkList(pList);
}
void TestFind()
{
pList pList = NULL;
pNode pos = NULL;
InitLinkList(&pList);
PushBack(&pList, 1);
PushBack(&pList, 2);
PushBack(&pList, 3);
PushBack(&pList, 4);
PrintLinkList(pList);//1 2 3 4
pos = Find(pList, 5);
if (pos != NULL)
{
printf("%d\n", pos->data);
}
DestoryLinkList(&pList);
}
void TestInsert()
{
pList pList = NULL;
pNode pos = NULL;
InitLinkList(&pList);
PushBack(&pList, 1);
PushBack(&pList, 2);
PushBack(&pList, 3);
PushBack(&pList, 4);
PrintLinkList(pList);//1 2 3 4
pos = Find(pList, 1);
if (pos != NULL)
{
Insert(&pList, pos, 7);
PrintLinkList(pList);//1 2 7 3 4
}
DestoryLinkList(&pList);
}
void TestErase()
{
pList pList = NULL;
pNode pos = NULL;
InitLinkList(&pList);
PushBack(&pList, 1);
PushBack(&pList, 2);
PushBack(&pList, 3);
PushBack(&pList, 4);
PrintLinkList(pList);//1 2 3 4
pos = Find(pList, 1);
if (pos != NULL)
{
Erase(&pList, pos);
PrintLinkList(pList);//2 3 4
}
DestoryLinkList(&pList);
}
void TestRemove()
{
pList pList = NULL;
pNode pos = NULL;
InitLinkList(&pList);
PushBack(&pList, 1);
PushBack(&pList, 2);
PushBack(&pList, 3);
PushBack(&pList, 4);
PushBack(&pList, 2);
PrintLinkList(pList);//1 2 3 4
Remove(&pList, 1);
PrintLinkList(pList);//2 3 4 2
RemoveAll(&pList, 2);
PrintLinkList(pList);//3 4
DestoryLinkList(&pList);
}
void TestEraseNotTailNode()
{
pList pList = NULL;
pNode pos = NULL;
InitLinkList(&pList);
PushBack(&pList, 1);
PushBack(&pList, 2);
PushBack(&pList, 3);
PushBack(&pList, 4);
PushBack(&pList, 2);
PrintLinkList(pList);//1 2 3 4 2
pos = Find(pList, 2);
if (pos != NULL)
{
EraseNotTailNode(pos);
PrintLinkList(pList);//1 3 4 2
}
DestoryLinkList(&pList);
}
void TestReverseLinkList()
{
pList pList = NULL;
pNode pos = NULL;
InitLinkList(&pList);
PushBack(&pList, 1);
PushBack(&pList, 2);
PushBack(&pList, 3);
PushBack(&pList, 4);
PushBack(&pList, 5);
PrintLinkList(pList);//1 2 3 4 5
ReverseLinkList(&pList);
PrintLinkList(pList);//5 4 3 2 1
DestoryLinkList(&pList);
}
void TestPrintTailToHead()
{
pList pList = NULL;
pNode pos = NULL;
InitLinkList(&pList);
PushBack(&pList, 1);
PushBack(&pList, 2);
PushBack(&pList, 3);
PushBack(&pList, 4);
PushBack(&pList, 5);
PrintLinkList(pList);//1 2 3 4 5
PrintTailToHead(pList);//5 4 3 2 1
PrintTailToHead_recursion(pList);//5 4 3 2 1
DestoryLinkList(&pList);
}
void TestEraseNodeNotTail()
{
pList pList = NULL;
pNode pos = NULL;
InitLinkList(&pList);
PushBack(&pList, 1);
PushBack(&pList, 2);
PushBack(&pList, 3);
PushBack(&pList, 4);
PushBack(&pList, 5);
PrintLinkList(pList);//1 2 3 4 5
pos = Find(pList, 2);
if (pos != NULL)
{
EraseNodeNotTail(pos);
}
PrintLinkList(pList);//1 3 4 5
}
void TestInsertNode()
{
pList pList = NULL;
pNode pos = NULL;
InitLinkList(&pList);
PushBack(&pList, 1);
PushBack(&pList, 2);
PushBack(&pList, 3);
PushBack(&pList, 4);
PushBack(&pList, 5);
PrintLinkList(pList);//1 2 3 4 5
pos = Find(pList, 2);
if (pos != NULL)
{
InsertNode(pos, 3);
}
PrintLinkList(pList);//1 3 2 3 4 5
}
void TestJosephusCycle()
{
int i = 0;
pList pList = NULL;
pNode tail = NULL;
InitLinkList(&pList);
for (i = 1; i <= 5; i++)
{
PushBack(&pList, i);
}
连成环
//tail = pList;
//while (tail->next != NULL)
//{
// tail = tail->next;
//}
//tail->next = pList;
//进行检验
JosephusCycle(&pList, 3);
}
void TestBubbleSort()
{
int i = 0;
pList pList = NULL;
for (i = 5; i >= 0; i--)
{
PushBack(&pList, i);
}
PrintLinkList(pList);//5 4 3 2 1 0
BubbleSort(pList);
PrintLinkList(pList);//0 1 2 3 4 5
DestoryLinkList(&pList);
}
void TestMerge()
{
int i = 0;
pList newList = NULL;
pList pList1 = NULL;
pList pList2 = NULL;
for (i = 0; i < 9; i += 2)
{
PushBack(&pList1, i);
}
for (i = 1; i < 9; i += 1)
{
PushBack(&pList2, i);
}
PrintLinkList(pList1);//0 2 4 6 8
PrintLinkList(pList2);//1 3 5 7
//newList = Merge(pList1, pList2);
newList = Merge_R(pList1, pList2);
PrintLinkList(newList);
}
void TestFindMidNode()
{
int i = 0;
pNode pos = NULL;
pList pList = NULL;
for (i = 0; i < 1; i++)
{
PushBack(&pList, i + 1);
}
PrintLinkList(pList);
pos = FindMidNode(pList);
if (pos != NULL)
{
printf("%d\n", pos->data);
}
DestoryLinkList(&pList);
}
void TestFindLastKNode()
{
int i = 0;
int k = 0;
pList pList = NULL;
pNode pos = NULL;
for (i = 0; i < 5; i++)
{
PushBack(&pList, i + 1);
}
PrintLinkList(pList);
pos = FindLastKNode(pList, 6);
if (pos != NULL)
{
printf("%d\n", pos->data);
}
}
void TestCheckCycle()
{
int i = 0;
int length = 0;
pNode start = NULL;
pNode meet = NULL;
pNode tmp1 = NULL;
pNode tmp2 = NULL;
pList pList = NULL;
for (i = 1; i < 9; i++)
{
PushBack(&pList, i);
}
PrintLinkList(pList);
//构成环
tmp1 = Find(pList, 3);
tmp2 = Find(pList, 6);
tmp2->next = tmp1;
meet = CheckCycle(pList);
if (meet != NULL)
{
printf("该单链表有环!\n");
length = GetCycleLength(meet);
printf("环的长度为:%d\n", length);
start = StartCycle(pList, meet);
printf("环的入口点:%d\n", start->data);
}
}
void TestCheckMeet()
{
int i = 0;
bool flag = true;
pNode meet = NULL;
pNode tmp1 = NULL;
pNode tmp2 = NULL;
pList pList1 = NULL;
pList pList2 = NULL;
for (i = 1; i < 9; i++)
{
PushBack(&pList1, i);
if (i <= 5)
{
PushBack(&pList2, i);
}
}
//构建相交链表
tmp1 = Find(pList2, 5);
tmp2 = Find(pList1, 6);
tmp1->next = tmp2;
//判断两个链表是否相交,并且求交点
flag = CheckMeet(pList1, pList2);
if (flag == true)
{
printf("两个链表相交!\n");
meet = GetMeet(pList1, pList2);
printf("交点为:%d\n", meet->data);
}
}
void TestComplexListCopyList()
{
ComplexNode* copyList = NULL;
ComplexNode* node1 = BuyNode_Complex(1);
ComplexNode* node2 = BuyNode_Complex(2);
ComplexNode* node3 = BuyNode_Complex(3);
ComplexNode* node4 = BuyNode_Complex(4);
ComplexNode* node5 = BuyNode_Complex(5);
//连接各个结点的next域
node1->next = node2;
node2->next = node3;
node3->next = node4;
node4->next = node5;
//设置各个结点的random域
node1->random = node3;
node2->random = node4;
node3->random = node2;
node4->random = node5;
node5->random = NULL;
PrintComplexList(node1);
copyList = CopyComplexList(node1);//node1为该复杂链表的起始结点
PrintComplexList(copyList);
}
void TestIntersection()
{
int i = 0;
pList IntersectionList = NULL;
pList DifsetList = NULL;
pList pList1 = NULL;
pList pList2 = NULL;
PushBack(&pList1, 1);
PushBack(&pList1, 3);
PushBack(&pList1, 4);
PushBack(&pList1, 6);
PushBack(&pList1, 7);
PushBack(&pList2, 1);
PushBack(&pList2, 2);
PushBack(&pList2, 4);
PushBack(&pList2, 5);
PushBack(&pList2, 7);
//IntersectionList = Intersection(pList1, pList2);
DifsetList = Difset(pList1, pList2);
//PrintLinkList(IntersectionList);
PrintLinkList(DifsetList);
}
int main()
{
//TestBackLinkList();
//TestFrontLinkList();
//TestFind();
//TestInsert();
//TestErase();
//TestRemove();
//TestEraseNotTailNode();
//TestReverseLinkList();
//TestPrintTailToHead();
//TestEraseNodeNotTail();
//TestInsertNode();
//TestJosephusCycle();
//TestBubbleSort();
//TestMerge();
//TestFindMidNode();
//TestFindLastKNode();
//TestCheckCycle();
//TestCheckMeet();
//TestComplexListCopyList();
TestIntersection();
return 0;
}