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

C语言实现单链表常见面试题

2017-06-27 17:55 387 查看
前面我们已经实现了单链表的基本功能,今天来实现一下单链表的一些常见的面试题

前面的英文注释的代码是单链表基本功能的实现(因为输入法的问题,所以是英文,不是装逼)后面的中文注释的就是单链表的一些常见的面试题了,代码是绝对经过验证的,大部分情况下应该不会出错。如果有错误的地方,欢迎联系我一起学习交流。下面就是我们要完成那些单链表的面试题:

1. ⽐较顺序表和链表的优缺点,说说它们分别在什么场景下使⽤?
2. 从尾到头打印单链表
3. 删除⼀个⽆头单链表的⾮尾节点
4. 在⽆头单链表的⼀个⾮头节点前插⼊⼀个节点
5. 单链表实现约瑟夫环
6. 逆置/反转单链表
7. 单链表排序(冒泡排序&快速排序)
8. 合并两个有序链表,合并后依然有序
9. 查找单链表的中间节点,要求只能遍历⼀次链表
10. 查找单链表的倒数第k个节点, 要求只能遍历⼀次链表
11. 判断单链表是否带环?若带环,求环的长度?求环的⼊⼜点?
并计算每个算法

的时间复杂度&空间复杂度。
12. 判断两个链表是否相交,若相交,求交点。(假设链表不带环)
13. 判断两个链表是否相交,若相交,求交点。(假设链表可能带环)【升级版】

头文件:

#ifndef _LINK_LIST_H_
#define _LINK_LIST_H_

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int DataType; //the type of the data in the linklist

typedef struct Node //the struct of the of the node in the linklist
{
DataType _data; //Data domain
struct Node *_next; //Pointer domain

}Node,*pNode,*pList;

//Init the linklist
void InitLinkList(pList *pplist);

//insert a new node in the back of the linklist
void PushBack(pList *pplist,DataType x);

//delete a node from the back of the linklist
void PopBack(pList *pplist);

//display the linklist
void Display(pList pplist);

//insert a new node in the head of the linklist
void PushFront(pList *pplist,DataType x);

//delete a node from the head of the linklis
void PopFront(pList *pplist);

//destroy the linklist
void DestroyLinklist(pList *pplist);

//find the pos of the data in the linklist
pNode Find(const pList * pplist, DataType x);

//insert new node in the Specify location
int Insert(pList* plist, pNode pos, DataType x);

//delete a node in the Specify location
int Erase(pList *plist, pNode pos);

//delete the first appearance node that it's data is x
int Remove(pList *plist, DataType x);

//delete the all appearance node that it's data is x
void RemoveAll(pList *pplist, DataType x);

//bubble sort of the linklist from small to big
void BubbleSort(pList* pplist);
//reverse printing the linklist
void ReversePrint(pList plist);
//删除无头单链表的非尾结点
void EraseNotTail(pNode pos);

//在无头单链表的非头结点前插入一个元素
void InsertFrontNode(pNode pos, DataType x);
//约瑟夫环问题
void JosephCycle(pList* pplist, int k);
//逆序单向链表
void ReverseList(pList* pplist);

//合并两个有序列表
//pList Merge(pList* p1, pList* p2);
pList Merge(const pList* p1, const pList* p2);
//查找单链表的中间节点,要求只能遍历一次链表
pNode FindMidNode(pList pplist);

//查找单链表的倒数第k个节点,要求只能遍历一次链表
pNode FindKNode(pList plist, int k);
//判断链表时候带环
pNode CheckCircle(pList pplist);
//求环的长度
int GetCircleLength(pNode meet);

//求环的入口点
pNode GetCycleEntryNode(pList pplist, pNode meet);
//判断两条单项链表是否相交,若相交返回交点处的结点
pNode GetCrossNode(pList list1, pList list2);
#endif//_LINK_LIST_H_

功能实现:

#include"LinkList.h"

//Init the linklist
void InitLinkList(pList *pplist)
{
*pplist=NULL;
}
//Create a new node
pNode BuyNode(DataType x)
{
pNode p=(pNode)malloc(sizeof(Node));//create a new node
if(p==NULL)//create failed
{
perror("BuyNode():malloc");
}
//create successful
p->_data=x;
p->_next=NULL;
return p;

}

//insert a new node in the back of the linklist
void PushBack(pList *pplist,DataType x)
{
pNode pnode=NULL;
pNode p=*pplist;
assert(pplist!=NULL);
pnode=BuyNode(x);//create a new node
if(*pplist==NULL)//null linklist,don't have any number
{
*pplist=pnode;
return;
}
//at least have two or more data in this linklist
while(p->_next)//find the last node
{
p=p->_next;

}
p->_next=pnode;
}
//delete a node from the back of the linklist
void PopBack(pList *pplist)
{
pNode p=*pplist;
assert(pplist!=NULL&&*pplist!=NULL);
if(p->_next==NULL)//only have a node inlinklist
{
*pplist=NULL;
return;
}
//at least have two node in linklist
while(p->_next->_next)//find the last node
{
p=p->_next;
}
free(p->_next);
p->_next=NULL;
}
//display the linklist
void Display(pList pplist)
{
pNode p=pplist;
if(p==NULL)//while the linklist is empty
{
printf("no data in linklist!\n");
return;
}
while(p)
{
printf("%d—>",p->_data);
p=p->_next;
}
printf("over\n");
}
//insert a new node in the head of the linklist
void PushFront(pList *pplist,DataType x)
{
pNode pnode=NULL;
pNode p = NULL;
assert(pplist!=NULL);
pnode=BuyNode(x);
if(*pplist==NULL)//if the linklist is NULL;
{
*pplist=pnode;
return;
}
//at least have two numbers in the linklist

p=*pplist;
*pplist=pnode;
(*pplist)->_next=p;
}
//delete a node from the head of the linklist
void PopFront(pList *pplist)
{
pNode p=*pplist;
assert(pplist!=NULL&&*pplist!=NULL);
if((*pplist)->_next==NULL)//only have one node in the linklist
{
*pplist=NULL;
return;
}
//at least have two node in the linklist
*pplist=(*pplist)->_next;
free(p);
p=NULL;
}

//destroy the linklist
void DestroyLinklist(pList *pplist)
{
assert(pplist!=NULL);
while(*pplist)
{
pNode p=*pplist;
*pplist=(*pplist)->_next;
free(p);
}
*pplist=NULL;
}
//find the pos of the data in the linklist
pNode Find(const pList *pplist, DataType x)
{
pNode p=*pplist;
assert(pplist!=NULL);
while(p)
{
if(p->_data==x)
{
return p;
}
p=p->_next;
}
return NULL;
}
//insert new node in the Specify location
int Insert(pList* pplist, pNode pos, DataType x)
{
pNode pnode=*pplist;
pNode new_node=NULL;
assert(pplist!=NULL);
new_node=BuyNode(x);
if((*pplist)->_next==NULL)//only have one node in the linklist
{
if(*pplist==pos)//insert the pos node in the front of the first node
{
new_node->_next=*pplist;
*pplist=new_node;
return 1;
}
return -1;
}
//at least have two node on the linklist
if(*pplist==pos)//if the pos node is the first node
{
new_node->_next=*pplist;
*pplist=new_node;
return 1;
}
else{//the pos node isn't the first node
while(pnode->_next)
{
if(pnode->_next==pos)//find the frontal node of the pos node
{
new_node->_next=pnode->_next;
pnode->_next=new_node;
return 1;
}
pnode=pnode->_next;
}
return -1;
}
}
//delete a node in the Specify location
int Erase(pList *pplist, pNode pos)
{
pNode p=*pplist;
assert(pplist!=NULL);
if((*pplist)->_next==NULL)//only have one node in the linklist
{
if(pos==*pplist)
{
*pplist=NULL;
return 1;
}
return -1;
}
if(*pplist==pos)//if the pos node is the first node in the linklist
{
pNode ret=*pplist;
*pplist=(*pplist)->_next;
free(ret);
ret=NULL;
return 1;
}
else//the pos node isn't the first node in the linklist
{
while(p->_next)
{
if(p->_next==pos)
{
pNode ret=p->_next;
p->_next=p->_next->_next;
free(ret);
ret=NULL;
return 1;
}
p=p->_next;
}
return -1;
}
}
//delete the first appearance node that it's data is x
int Remove(pList *pplist, DataType x)
{
pNode p_find=NULL;
assert(pplist!=NULL);
p_find=Find(pplist,x);
if(p_find==NULL)
{
printf("This data isn't exist in the linklist or the linklist is empty!\n");
return -1;

}
//the pos must found in the linklist
return Erase(pplist,p_find);
}
//delete the all appearance node that it's data is x
void RemoveAll(pLi
e251
st *pplist, DataType x)
{
pNode p_find=*pplist;
assert(pplist!=NULL);
while((p_find=Find(pplist,x)))//find the data in the linklist
{
Erase(pplist,p_find);//Erase the node that we find in the linklist
}
}
//sort of the linklist from small to big
void BubbleSort(pList *pplist)
{
pNode p_one=*pplist;
pNode p_two=NULL;
assert(pplist!=NULL);
if((*pplist)->_next==NULL)
{
return;
}
while(p_one->_next)//First cycle
{
p_two=p_one->_next;

while(p_two)//Second cycle
{
if(p_one->_data>p_two->_data)//swap
{
DataType tmp=p_one->_data;
p_one->_data=p_two->_data;
p_two->_data=tmp;
}
p_two=p_two->_next;
}
p_one=p_one->_next;
}
}
//reverse printing the linklist
void ReversePrint(pList pplist)
{
//采用递归的方法实现单链表的逆序打印
pNode ret = pplist;
if(pplist == NULL)
{
return;
}
if(pplist->_next != NULL)
{
ret = pplist->_next;
ReversePrint(ret);
}
printf("%d—>",pplist->_data);

}

//删除无头单链表的非尾结点
void EraseNotTail(pNode pos)
{
pNode del = NULL;
assert(pos != NULL && pos->_next != NULL);
pos->_data = pos->_next->_data;
pos->_next = pos->_next->_next;
free(del);
del = NULL;
}
//在无头单链表的非头结点前插入一个元素
void InsertFrontNode(pNode pos, DataType x)
{
pNode new_pnode = NULL;
DataType tmp = 0;
assert(pos != NULL && pos->_next !=NULL);
new_pnode = BuyNode(x);
new_pnode->_next = pos->_next;
pos->_next = new_pnode;
tmp = pos->_data;
pos->_data = pos->_next->_data;
pos->_next->_data = tmp;
}

//约瑟夫环问题
void JosephCycle(pList* pplist, int k)
{
pNode ret = *pplist;
pNode del = NULL;
int i=0;
assert(pplist != NULL);
while(ret->_next != ret)
{
for(i=0;i<k-2;i++)
{
ret=ret->_next;
}
del=ret->_next;
printf("delete:%d—>",del->_data);
ret->_next = ret->_next->_next;
free(del);
del = NULL;
}
printf("surplus:%d ",ret->_data);
}

//逆序单向链表
void ReverseList(pList* pplist)
{
pNode pbehind = NULL;
pNode pfront = NULL;
assert(pplist != NULL);
if(*pplist == NULL && (*pplist)->_next == NULL)
{
return;
}
pfront = (*pplist);
pbehind = pfront->_next;
(*pplist)->_next = NULL;
while(pbehind)
{
pfront = pbehind;
pbehind = pbehind->_next;
pfront->_next= *pplist;
*pplist = pfront;
}
}

//合并两个有序列表
pList Merge(const pList* p1, const pList* p2)
{
pList pplist = NULL;
pNode point1 = *p1;
pNode point2 = *p2;
assert(p1 != NULL && p2 != NULL);
if(*p1 == NULL)
{
return *p2;
}
if(*p2 == NULL)
{
return *p1;
}
while((point1 != NULL) && (point2 !=NULL))
{

if(point1->_data< point2->_data)
{
PushBack(&pplist,point1->_data);
point1 = point1->_next;
}
else
{
PushBack(&pplist,point2->_data);
point2 = point2->_next;
}
}
if(point1 != NULL)
{
while(point1)
{
PushBack(&pplist,point1->_data);
point1 = point1->_next;
}
}
else
{
while(point2)
{
PushBack(&pplist,point2->_data);
point2 = point2->_next;
}
}
return pplist;

}
//查找单链表的中间节点,要求只能遍历一次链表
pNode FindMidNode(pList pplist)
{
pNode fast = NULL;
pNode slow = NULL;
assert(pplist != NULL);
if(pplist == NULL || pplist->_next == NULL)
{
return pplist;
}
fast = pplist;
slow = pplist;
while(fast != NULL && fast->_next != NULL)
{
fast = fast->_next->_next;
slow = slow->_next;
}
return slow;
}

//查找单链表的倒数第k个节点,要求只能遍历一次链表
pNode FindKNode(pList pplist, int k)
{
pNode fast = NULL;
pNode slow = NULL;
assert(pplist != NULL);
fast = pplist;
slow = pplist;
while(k)
{
if(fast == NULL)
{
break;
}
k--;
fast = fast->_next;
}
if(k != 0)
{
return NULL;
}
while(fast)
{
slow = slow->_next;
fast = fast->_next;
}
return slow;
}

//判断链表时候带环
pNode CheckCircle(pList pplist)
{
pNode fast = NULL;
pNode slow = NULL;
assert(pplist != NULL);
fast = pplist;
slow = pplist;
while(fast != NULL && slow != NULL && fast->_next != NULL &&slow->_next != NULL)
{
fast = fast->_next->_next;
slow = slow->_next;
if(fast == slow)
{
return fast;
}
}
return NULL;
}

//求环的长度
int GetCircleLength(pNode meet)
{
//注意此处的meet是在判断单链表是否带环的时候的快指针和慢指针的相遇结点
//该函数的设计思路是从相遇结点作为开始,慢指针每次前进一步,而快指针
//每次前进两步,那么在第二圈的时候快指针就会再次与慢指针相遇,在整个
//过程中我们设置一个计数器对慢指针计数就可以计算出环的长度
int count = 0;
pNode fast = NULL;
pNode slow = NULL;
assert(meet != NULL);
fast = meet;
slow = meet;
do
{
fast=fast->_next->_next;
slow = slow->_next;
count++;
}
while(fast != slow);
return count;

}

//求环的入口点
pNode GetCycleEntryNode(pList pplist, pNode meet)
{
pNode fast = NULL;
pNode slow = NULL;
assert(pplist != NULL && meet != NULL);
fast = meet;
slow = pplist;
while(fast != slow)
{
fast = fast->_next;
slow = slow->_next;
}
return slow;
}
//已知两个单链表不带环,判断是否相交,若相交求交点
pNode getCrossNoCycle(pList list1, pList list2)
{
pNode plist1 = list1;
pNode plist2 = list2;
int length1 = 0;
int length2 = 0;
int step = 0;
while(plist1)//计算list1的长度
{
length1++;
plist1 = plist1->_next;
}
while(plist2)//计算list2的长度
{
length2++;
plist2 = plist2->_next;
}
//让长度长的链表先走step步
if(length1 > length2)
{
step = length1 - length2;
plist1 = list1;
plist2 = list2;
while(step--)
{
plist1 = plist1->_next;
}
}
else
{
plist1 = list1;
plist2 = list2;
step = length2 - length1;
while(step--)
{
plist2 = plist2->_next;
}
}
//长的链表已经走了step步,两链表一起开始单步走,当两链表有元素相等的时候说明走到了相交点处
while(plist1 !=plist2)
{
if(plist1 == NULL || plist2 == NULL)//如果出现了其中一个链表走完了还没有走到相遇点处说明两个单链表不相交
{
return NULL;
}
plist1 = plist1->_next;
plist2 = plist2->_next;
}

return plist1;
}
//已知两个单链表带环,判断是否相交,若相交求交点
pNode getCrossCycle(pList list1, pList list2,pNode meet1,pNode meet2)
{
pNode entry1 = NULL;
pNode entry2 = NULL;
int length = GetCircleLength(list1);//求出其中一个单链表的换的长度
//求出两个单链表的入口点
entry1 = GetCycleEntryNode(list1,meet1);
entry2 = GetCycleEntryNode(list2,meet2);
if(entry1 == entry2)//当两单链表的环入口点相同时说明两单链表必定相交
{
pNode plist1 = list1;
pNode plist2 = list2;
int length1 = 0;
int length2 = 0;
int step = 0;
while(plist1 != entry1)//求出list1到环入口结点的长度
{
length1++;
plist1 = plist1->_next;
}
while(plist2 != entry2)//求出list2到换入口点的长度
{
length2++;
plist2 = plist2->_next;
}
//让长度长的链表先走step步
if(length1 > length2)
{
step = length1 - length2;
plist1 = list1;
plist2 = list2;
while(step--)
{
plist1 = plist1->_next;
}
}
else
{
plist1 = list1;
plist2 = list2;
step = length2 - length1;
while(step--)
{
plist2 = plist2->_next;
}
}
//长的链表已经走了step步,两链表一起开始单步走,当两链表有元素相等的时候说明走到了相交点处
while(plist1 !=plist2)
{
plist1 = plist1->_next;
plist2 = plist2->_next;
}

}//两个单链表的环入口点相同的情况end
else//两个单链表的环的入口点不同
{
while(entry1 != entry2)
{
length--;
entry1 = entry1->_next;
if(length==0)//当单链表list已经在环里走了一圈还没有遇到list2的环入口结点,说明这两个单链表不相交
{
return NULL;
}
}
return entry1;
}//两个单链表的环入口点不同情况end
return NULL;
}
//判断两条单项链表是否相交,若相交返回交点处的结点
pNode GetCrossNode(pList list1, pList list2)
{
pNode meet1 = NULL;//该指针用于判断链表1是否带环如果带环记录相遇结点
pNode meet2 = NULL;//该指针用于判断链表2是否带环如果带环记录相遇结点
if(list1 == NULL || list2 == NULL)
{
return NULL;
}
//判断链表是否带环
if(( meet1 = CheckCircle(list1))&&(meet2 = CheckCircle(list2)))//带环的情况
{
return getCrossCycle(list1,list2,meet1,meet2);
}
else//不带环
{
return getCrossNoCycle(list1,list2);

}
}

测试代码:
#include"linklist.h"

//testing of PopBack and PushBack
void test1()
{
pList pplist;
InitLinkList(&pplist);
printf("PushBack:1,2,3,4\n");
PushBack(&pplist,1);
PushBack(&pplist,2);
PushBack(&pplist,3);
PushBack(&pplist,4);
Display(pplist);
printf("PopBack:\n");
PopBack(&pplist);
Display(pplist);
printf("PopBack:\n");
PopBack(&pplist);
Display(pplist);
printf("PopBack:\n");
PopBack(&pplist);
Display(pplist);
printf("PopBack:\n");
PopBack(&pplist);
Display(pplist);
printf("PushBack:\n");
PushBack(&pplist,7);
Display(pplist);
printf("PushBack:\n");
PushBack(&pplist,8);
Display(pplist);
DestroyLinklist(&pplist);
}

//testing of PopFront and PushFront
void test2()
{
pList pplist;
InitLinkList(&pplist);
printf("PushFront:1,2,3,4\n");
PushFront(&pplist,1);
PushFront(&pplist,2);
PushFront(&pplist,3);
PushFront(&pplist,4);
Display(pplist);
printf("PopFront:\n");
PopFront(&pplist);
Display(pplist);
printf("PopFront:\n");
PopFront(&pplist);
Display(pplist);
printf("PopFront:\n");
PopFront(&pplist);
Display(pplist);
printf("PopFront:\n");
PopFront(&pplist);
Display(pplist);
printf("PushFront:\n");
PushFront(&pplist,7);
Display(pplist);
printf("PushFront:\n");
PushFront(&pplist,8);
Display(pplist);
DestroyLinklist(&pplist);
}
//testing Find,Insert and Erase
void test3()
{
pNode p_find=NULL;
pList pplist;
InitLinkList(&pplist);
printf("PushFront:1,2,3,4\n");
PushFront(&pplist,1);
PushFront(&pplist,2);
PushFront(&pplist,3);
PushFront(&pplist,4);
Display(pplist);
p_find=Find(&pplist,3);
if(p_find)
{
printf("find the number: %d\n",p_find->_data);

}else{
printf("no this number in the linklist or the linklist is empty!\n");
}

//Insert the 9 to the pos of p_find in the linklist
printf("Insert 9 in the node that data is 3 :\n");
Insert(&pplist,p_find,9);
Display(pplist);
//Insert the 88 to the pos of p_find in the linklist
p_find=Find(&pplist,1);
printf("Insert 88 in the node that data is 1 :\n");
Insert(&pplist,p_find,88);
Display(pplist);
//Erease the 4 to the pos of p_find in the linklist
p_find=Find(&pplist,4);
printf("Erease 4 :\n");
Erase(&pplist,p_find);
Display(pplist);
//Erease the 9 to the pos of p_find in the linklist
p_find=Find(&pplist,9);
printf("Erease 9 :\n");
Erase(&pplist,p_find);
Display(pplist);

DestroyLinklist(&pplist);
}
//testing Remove ,RemoveAll and Sort
void test4()
{
pNode p_find=NULL;
pList pplist;
InitLinkList(&pplist);
printf("PushFront 8,2,6,4:\n");
PushFront(&pplist,8);
PushFront(&pplist,2);
PushFront(&pplist,6);
PushFront(&pplist,4);
Display(pplist);
//Remove 3
printf("Remove 3:\n");
Remove(&pplist,3);
Display(pplist);

//Remove 2
printf("Remove 2:\n");
Remove(&pplist,2);
Display(pplist);
printf("PushBack 12,3,5,12,5,12 :\n");
PushBack(&pplist,12);
PushBack(&pplist,3);
PushBack(&pplist,5);
PushBack(&pplist,12);
PushBack(&pplist,5);
PushBack(&pplist,12);
Display(pplist);
printf("RemoveAll 12:\n");
RemoveAll(&pplist,12);
Display(pplist);
printf("Sort :\n");
BubbleSort(&pplist);
Display(pplist);
DestroyLinklist(&pplist);
}

//testing of the ReversePrint EraseNotTail InsertFrontNode
void test6()
{
pNode p_find=NULL;
pList pplist;
InitLinkList(&pplist);
printf("PushFront 8,2,6,4:\n");
//逆序打印空的单链表
ReversePrint(pplist);
PushFront(&pplist,8);
PushFront(&pplist,2);
PushFront(&pplist,6);
PushFront(&pplist,4);
Display(pplist);
printf("逆序打印单链表\n");
ReversePrint(pplist);
printf("\n删除无头单链表的非尾结点\n");
p_find = Find(&pplist,6);
EraseNotTail(p_find);
Display(pplist);
printf("在无头单链表的非头结点处插入一个新的结点\n");
p_find = Find(&pplist,2);
InsertFrontNode(p_find,99);
Display(pplist);
DestroyLinklist(&pplist);
}

//testing JosephCycle
void test7()
{
pNode p_find=NULL;
pList pplist;
InitLinkList(&pplist);
PushFront(&pplist,4);
PushFront(&pplist,3);
PushFront(&pplist,2);
PushFront(&pplist,1);
PushBack(&pplist,5);
/*PushBack(&pplist,6);
PushBack(&pplist,7);
PushBack(&pplist,8);
PushBack(&pplist,9);
PushBack(&pplist,10)*/;
Display(pplist);
printf("约瑟夫环:\n");
Find(&pplist,5)->_next = Find(&pplist,1);
JosephCycle(&pplist, 3);
}

//testing ReverseList
void test8()
{
pNode p_find=NULL;
pList pplist;
InitLinkList(&pplist);
PushFront(&pplist,4);
PushFront(&pplist,3);
PushFront(&pplist,2);
PushFront(&pplist,1);
PushBack(&pplist,5);
PushBack(&pplist,6);
PushBack(&pplist,7);
PushBack(&pplist,8);
PushBack(&pplist,9);
PushBack(&pplist,10);
Display(pplist);
printf("逆置单链表:\n");
ReverseList(&pplist);
Display(pplist);
DestroyLinklist(&pplist);
}

//testing Merge FindMidNode
void test9()
{
pNode p_find=NULL;
pList pplist;
pList p1;
pList p2;
pNode p1_find = NULL;
pNode p2_find = NULL;
pNode p3_find = NULL;
InitLinkList(&p1);
InitLinkList(&p2);
PushFront(&p1,100);
PushFront(&p1,99);
PushFront(&p1,5);
PushFront(&p1,3);
PushFront(&p1,1);
Display(p1);
printf("查找单链表1中的中间结点\n");
p1_find = FindMidNode(p1);
printf("%d\n",p1_find->_data);
PushBack(&p2,2);
PushBack(&p2,2);
PushBack(&p2,2);
PushBack(&p2,8);
PushBack(&p2,10);
Display(p2);
printf("查找单链表2中的中间结点\n");
p2_find = FindMidNode(p2);
printf("%d\n",p2_find->_data);
printf("合并有序单链表,合并后仍有序:\n");
pplist = Merge(&p1 ,&p2);
Display(pplist);
printf("查找合并后的单链表中的中间结点\n");
p3_find = FindMidNode(pplist);
printf("%d\n",p3_find->_data);
DestroyLinklist(&p1);
DestroyLinklist(&p2);
DestroyLinklist(&pplist);

}

//testing FindKNode
void test10()
{
pNode p_find=NULL;
pList pplist;
InitLinkList(&pplist);
PushFront(&pplist,4);
PushFront(&pplist,3);
PushFront(&pplist,2);
PushFront(&pplist,1);
PushBack(&pplist,5);
PushBack(&pplist,6);
PushBack(&pplist,7);
PushBack(&pplist,8);
PushBack(&pplist,9);
PushBack(&pplist,10);
Display(pplist);
printf("查找单链表中的倒数第K个结点:\n");
p_find = FindKNode(pplist,5);
if(p_find != NULL)
{
printf("%d\n",p_find->_data);
}
DestroyLinklist(&pplist);
}
//testing CheckCircle GetCircleLength GetCycleEntryNode
void test11()
{
int length = 0;
pNode p_find=NULL;
pNode p_entry = NULL;
pList pplist;
InitLinkList(&pplist);
PushFront(&pplist,4);
PushFront(&pplist,3);
PushFront(&pplist,2);
PushFront(&pplist,1);
PushBack(&pplist,5);
PushBack(&pplist,6);
PushBack(&pplist,7);
PushBack(&pplist,8);
/*PushBack(&pplist,9);
PushBack(&pplist,10);*/
Display(pplist);
printf("判断链表是否带环:\n");
Find(&pplist,8)->_next = Find(&pplist,4);
p_find = CheckCircle(pplist);
if(p_find != NULL)
{
printf("%d\n",p_find->_data);
}
length = GetCircleLength(p_find);
printf("环的长度为:%d\n",length);
printf("环的入口点为:\n");
p_entry = GetCycleEntryNode(pplist,p_find);
if(p_entry != NULL)
{
printf("%d\n",p_entry->_data);
}

}

//testing CheckCross
void test12()
{
pNode p_find=NULL;
pList p1;
pList p2;
pNode p1_find = NULL;
pNode p2_find = NULL;
pNode p3_find = NULL;
InitLinkList(&p1);
InitLinkList(&p2);
PushFront(&p1,9);
PushFront(&p1,7);
PushFront(&p1,5);
PushFront(&p1,3);
PushFront(&p1,1);
//Display(p1);
PushBack(&p2,2);
PushBack(&p2,4);
PushBack(&p2,6);
PushBack(&p2,8);
PushBack(&p2,10);
//Display(p2);
Find(&p2,10)->_next = Find(&p2,4);
Find(&p1,9)->_next = Find(&p2,6);
//Display(p1);
//Display(p2);
p_find = GetCrossNode(p1,p2);
if(p_find != NULL)
{
printf("该单链表相交\t交点为%d\n",p_find->_data);
}
}
int main()
{
//test1();

//test2();

//test3();

//test4();

//test5();

//test6();

//test7();

//test8();

//test9();

//test10();

//test11();

test12();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  面试题 链表 c语言