无头单链表面试题

来源:互联网 发布:杭州淘宝大学入学要求 编辑:程序博客网 时间:2024/05/29 08:26

LinkList.h

//逆序打印单项链表 

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 plist);


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


//求环的入口点 
pNode GetCycleEntryNode(pList plist, pNode meet);
//判断两条单项链表时候相交 
int CheckCross(pList list1, pList list2);


//求交点 
pNode GetCrossNode(pList list1, pList list2);


LinkList.c


void ReversePrint(pList plist)
{
pNode cur = plist;
cur = plist;
if (plist == NULL)
return;

    if(plist->_next != NULL)
{
cur = plist->_next;
ReversePrint(cur);


}
printf("%d ",plist->_data);
}


void EraseNotTail(pNode pos)
{
pNode cur = NULL;
assert(pos->_next != NULL);
cur = pos->_next;
pos->_data = cur->_data;
pos->_next = cur->_next;
free(cur);
cur = NULL;
}


void InsertFrontNode(pNode pos, DataType x)
{
DataType tmp;
    pNode newnode = BuyNode(x);
tmp = newnode->_data;
newnode->_data = pos->_data;
pos->_data = tmp;
newnode->_next = pos->_next;
pos->_next = newnode;


}


void JosephCycle(pList* pplist, int k)
{
int i;
pNode cur = NULL;
pNode del = NULL;
assert(pplist);
cur = *pplist;
while (cur->_next != cur)
{
for (i = 0; i < k - 1; i++)
{
cur = cur->_next;
}
del = cur->_next;
cur->_data = del->_data;
cur->_next = del->_next;
free(del);
del = NULL;


}
printf("%d ",cur->_data);


}


void ReverseList(pList* pplist)
{
pNode cur = NULL;
pNode node = NULL;
assert(pplist);
if ((*pplist == NULL) || (*pplist)->_next == NULL)
return;
cur = (*pplist)->_next;
(*pplist)->_next = NULL;
while(cur != NULL)
{
node = cur;
cur = cur->_next;
node->_next = *pplist;
*pplist = node;


}
}


pList Merge(const pList* p1, const pList* p2)
{
pList newlist = NULL;
pNode cur = NULL;
pNode cur1 = NULL;
pNode cur2 = NULL;
assert(p1);
assert(p2);


cur1 = *p1;
cur2 = *p2;
if (*p1 == NULL)
{
return *p2;
}
if (*p2 == NULL)
{
return *p1;
}


if (cur1->_data < cur2->_data)
{
newlist = BuyNode(cur1->_data);
cur = newlist;
cur1 = cur1->_next;
}
else
{
newlist = BuyNode(cur2->_data);
cur = newlist;
cur2 = cur2->_next;
}
while (cur1&&cur2)
{
if (cur1->_data <= cur2->_data)
{
cur->_next = BuyNode(cur1->_data);
cur1 = cur1->_next;
cur = cur->_next;
}
else
{
cur->_next = BuyNode(cur2->_data);
cur2 = cur2->_next;
cur = cur->_next;
}
}
if (cur1 == NULL)
{
while (cur2)
{
cur->_next = BuyNode(cur2->_data);
cur2 = cur2->_next;
cur = cur->_next;
}
}

if (cur2 == NULL)
{
while (cur1)
{
cur->_next = BuyNode(cur1->_data);
cur1 = cur1->_next;
cur = cur->_next;
}
}
return newlist;
}


pNode FindMidNode(pList plist)
{
pNode fast = NULL;
pNode slow = NULL;
fast = plist;
slow = plist;
if (plist == NULL)
return 0;
while ((fast->_next != NULL)&&(fast->_next->_next != NULL))
{
fast = fast->_next->_next;
slow = slow->_next;
}
return slow;
}


pNode FindKNode(pList plist, int k)
{
int i = 0;
pNode fast = NULL;
pNode slow = NULL;
fast = plist;
slow = plist;
if (plist == NULL)
return 0;
for (i = 0; i < k - 1; i++)
{
fast = fast->_next;
}


while (fast->_next != NULL)
{
fast = fast->_next;
slow = slow->_next;
}
return slow;
}


pNode CheckCircle(pList plist)
{
pNode fast = NULL;
pNode slow = NULL;
fast = plist;
slow = plist;
    while (fast && fast->_next)
{
fast = fast->_next->_next;
slow = slow->_next;
if (fast == slow)
{
return fast;
}
}
return NULL;
}


int GetCircleLength(pNode meet)
{
pNode cur = NULL;
cur = meet;
int count = 1;
while (cur->_next != meet)
{
count++;
cur = cur->_next;
}
return count;
}


pNode GetCycleEntryNode(pList plist, pNode meet)
{
pNode cur = NULL;
cur = plist;
while (cur != meet)
{
cur = cur->_next;
meet = meet->_next;
}
return meet;
}




int CheckCross(pList list1, pList list2)
{
pNode cur1 = NULL;
pNode cur2 = NULL;
cur1 = list1;
cur2 = list2;
if ((cur1 == NULL) || (cur2 == NULL))
return 0;
while (cur1->_next)
{
cur1 = cur1->_next;
}
while (cur2->_next)
{
cur2 = cur2->_next;
}
if (cur1 == cur2)
{
return 1;
}
else
return 0;
}




pNode GetCrossNode(pList list1, pList list2)
{
pNode cur1 = NULL;
    pNode cur2 = NULL;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int i = 0;
cur1 = list1;
cur2 = list2;
while(cur1->_next != NULL)
{
cur1 = cur1->_next;
count1++;
}
while (cur2->_next != NULL)
{
cur2 = cur2->_next;
count2++;
}
cur1 = list1;
cur2 = list2;
if (count1 < count2)
{
count3 = count2 - count1;
for (i = 0; i < count3; i++)
{
cur2 = cur2->_next;
}
}
else
{
count3 = count1 - count2;
for (i = 0; i < count3; i++)
{
cur1 = cur1->_next;
}
}
while (cur1 != cur2)
{
cur1 = cur1->_next;
cur2 = cur2->_next;
}
return cur1;


}


原创粉丝点击