ListNode.h:
#pragma once
struct ListNode
{
int val;
ListNode* next;
ListNode(int value) :
val(value), next(nullptr) {}
ListNode() {}
};
ListNode* CreateListNode(int value);
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext);
void PrintListNode(ListNode* pNode);
void PrintList(ListNode* pHead);
void DestroyList(ListNode* pHead);
void AddToTail(ListNode** pHead, int value);
void RemoveNode(ListNode** pHead, int value);
ListNode.cpp:
#include "ListNode.h"
#include <stdio.h>
#include <stdlib.h>
ListNode* CreateListNode(int value)
{
ListNode* pNode = new ListNode();
pNode->val = value;
pNode->next = nullptr;
return pNode;
}
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext)
{
if (pCurrent == nullptr)
{
printf("Error to connect two nodes.\n");
exit(1);
}
pCurrent->next = pNext;
}
void PrintListNode(ListNode* pNode)
{
if (pNode == nullptr)
{
printf("The node is nullptr\n");
}
else
{
printf("The key in node is %d.\n", pNode->val);
}
}
void PrintList(ListNode* pHead)
{
printf("PrintList starts.\n");
ListNode* pNode = pHead;
while (pNode != nullptr)
{
printf("%d\t", pNode->val);
pNode = pNode->next;
}
printf("\nPrintList ends.\n");
}
void DestroyList(ListNode* pHead)
{
ListNode* pNode = pHead;
while (pNode != nullptr)
{
pHead = pHead->next;
delete pNode;
pNode = pHead;
}
}
void AddToTail(ListNode** pHead, int value)
{
ListNode* pNew = new ListNode();
pNew->val = value;
pNew->next = nullptr;
if (*pHead == nullptr)
{
*pHead = pNew;
}
else
{
ListNode* pNode = *pHead;
while (pNode->next != nullptr)
pNode = pNode->next;
pNode->next = pNew;
}
}
void RemoveNode(ListNode** pHead, int value)
{
if (pHead == nullptr || *pHead == nullptr)
return;
ListNode* pToBeDeleted = nullptr;
if ((*pHead)->val == value)
{
pToBeDeleted = *pHead;
*pHead = (*pHead)->next;
}
else
{
ListNode* pNode = *pHead;
while (pNode->next != nullptr && pNode->next->val != value)
pNode = pNode->next;
if (pNode->next != nullptr && pNode->next->val == value)
{
pToBeDeleted = pNode->next;
pNode->next = pNode->next->next;
}
}
if (pToBeDeleted != nullptr)
{
delete pToBeDeleted;
pToBeDeleted = nullptr;
}
}
RandomListNode.h:
#pragma once
struct RandomListNode
{
int label;
RandomListNode* next;
RandomListNode* random;
RandomListNode(int x) :
label(x), next(NULL), random(NULL) {}
};
RandomListNode* CreateNode(int nValue);
void BuildNodes(RandomListNode* pNode, RandomListNode* pNext, RandomListNode* pSibling);
void PrintList(RandomListNode* pHead);
RandomListNode.cpp:
#include <cstdio>
#include "RandomListNode.h"
RandomListNode* CreateNode(int nValue)
{
RandomListNode* pNode = new RandomListNode(nValue);
return pNode;
}
void BuildNodes(RandomListNode* pNode, RandomListNode* pNext, RandomListNode* pSibling)
{
if (pNode != nullptr)
{
pNode->next = pNext;
pNode->random = pSibling;
}
}
void PrintList(RandomListNode* pHead)
{
RandomListNode* pNode = pHead;
while (pNode != nullptr)
{
printf("The value of this node is: %d.\n", pNode->label);
if (pNode->random != nullptr)
printf("The value of its sibling is: %d.\n", pNode->random->label);
else
printf("This node does not have a sibling.\n");
printf("\n");
pNode = pNode->next;
}
}
TreeNode.h:
#pragma once
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) :
val(x), left(nullptr), right(nullptr) {}
};
TreeNode* CreateBinaryTreeNode(int value);
void ConnectTreeNodes(TreeNode* pParent, TreeNode* pLeft, TreeNode* pRight);
void PrintTreeNode(const TreeNode* pNode);
void PrintTree(const TreeNode* pRoot);
void DestroyTree(TreeNode* pRoot);
TreeNode.cpp:
#include <cstdio>
#include "TreeNode.h"
TreeNode* CreateBinaryTreeNode(int value)
{
TreeNode* pNode = new TreeNode(value);
return pNode;
}
void ConnectTreeNodes(TreeNode* pParent, TreeNode* pLeft, TreeNode* pRight)
{
if (pParent != nullptr)
{
pParent->left = pLeft;
pParent->right = pRight;
}
}
void PrintTreeNode(const TreeNode* pNode)
{
if (pNode != nullptr)
{
printf("value of this node is: %d\n", pNode->val);
if (pNode->left != nullptr)
printf("value of its left child is: %d.\n", pNode->left->val);
else
printf("left child is nullptr.\n");
if (pNode->right != nullptr)
printf("value of its right child is: %d.\n", pNode->right->val);
else
printf("right child is nullptr.\n");
}
else
{
printf("this node is nullptr.\n");
}
printf("\n");
}
void PrintTree(const TreeNode* pRoot)
{
PrintTreeNode(pRoot);
if (pRoot != nullptr)
{
if (pRoot->left != nullptr)
PrintTree(pRoot->left);
if (pRoot->right != nullptr)
PrintTree(pRoot->right);
}
}
void DestroyTree(TreeNode* pRoot)
{
if (pRoot != nullptr)
{
TreeNode* pLeft = pRoot->left;
TreeNode* pRight = pRoot->right;
delete pRoot;
pRoot = nullptr;
DestroyTree(pLeft);
DestroyTree(pRight);
}
}