-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexList.cpp
More file actions
59 lines (47 loc) · 1.65 KB
/
ComplexList.cpp
File metadata and controls
59 lines (47 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*******************************************************************
Copyright(c) 2016, Harry He
All rights reserved.
Distributed under the BSD license.
(See accompanying file LICENSE.txt at
https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
*******************************************************************/
//==================================================================
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================
// 面试题35:复杂链表的复制
// 题目:请实现函数ComplexListNode* Clone(ComplexListNode* pHead),复
// 制一个复杂链表。在复杂链表中,每个结点除了有一个m_pNext指针指向下一个
// 结点外,还有一个m_pSibling 指向链表中的任意结点或者nullptr。
#include <cstdio>
#include "ComplexList.h"
ComplexListNode* CreateNode(int nValue)
{
ComplexListNode* pNode = new ComplexListNode();
pNode->m_nValue = nValue;
pNode->m_pNext = nullptr;
pNode->m_pSibling = nullptr;
return pNode;
}
void BuildNodes(ComplexListNode* pNode, ComplexListNode* pNext, ComplexListNode* pSibling)
{
if(pNode != nullptr)
{
pNode->m_pNext = pNext;
pNode->m_pSibling = pSibling;
}
}
void PrintList(ComplexListNode* pHead)
{
ComplexListNode* pNode = pHead;
while(pNode != nullptr)
{
printf("The value of this node is: %d.\n", pNode->m_nValue);
if(pNode->m_pSibling != nullptr)
printf("The value of its sibling is: %d.\n", pNode->m_pSibling->m_nValue);
else
printf("This node does not have a sibling.\n");
printf("\n");
pNode = pNode->m_pNext;
}
}