[面试备忘]样例代码共用头文件general.h

样例代码共用头文件general.h

包含常用头文件,数据结构定义及全局函数。

ContractedBlock.gif ExpandedBlockStart.gif View Code
#ifndef _GENERAL_H_
#define _GENERAL_H_
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) == MIN((a), (b)) ? (b) : (a))
typedef
struct __node
{
int value;
__node
* next;
} NODE,
*PNODE;
typedef
const NODE* CPNODE;

typedef
struct __tnode
{
char value;
__tnode
* left;
__tnode
* right;
} TNODE,
*PTNODE;
typedef
const TNODE* CPTNODE;

PNODE createList(
int length)
{
if (!length) {
return NULL;
}
PNODE head
= new NODE();
head
->value = 0;
head
->next = NULL;
PNODE tail
= head;
for (int index = 1; index != length; ++index) {
PNODE temp
= new NODE();
temp
->value = index;
temp
->next = NULL;
tail
->next = temp;
tail
= temp;
}
return head;
}

void destroyList(PNODE head)
{
while (head != NULL) {
PNODE nextNode
= head->next;
delete head;
head
= nextNode;
}
}

void printList(CPNODE head)
{
while (head != NULL) {
std::cout
<< head->value << ' ' << std::flush;
head
= head->next;
}
std::cout
<< std::endl;
}

void preOrderP(CPTNODE root)
{
if (root == NULL) {
return;
}
std::cout
<< root->value << ' ' << std::flush;
preOrderP(root
->left);
preOrderP(root
->right);
}

void preOrderPrint(CPTNODE root)
{
preOrderP(root);
std::cout
<< std::endl;
}

void inOrderP(CPTNODE root)
{
if (root == NULL) {
return;
}
inOrderP(root
->left);
std::cout
<< root->value << ' ' << std::flush;
inOrderP(root
->right);
}

void inOrderPrint(CPTNODE root)
{
inOrderP(root);
std::cout
<< std::endl;
}

void postOrderP(CPTNODE root)
{
if (root == NULL) {
return;
}
postOrderP(root
->left);
postOrderP(root
->right);
std::cout
<< root->value << ' ' << std::flush;
}

void postOrderPrint(CPTNODE root)
{
postOrderP(root);
std::cout
<< std::endl;
}

void buildBinaryTree(const char* pPreOrder, const char* pInOrder, int length, PTNODE& root)
{
if (length == 0) {
root
= NULL;
return;
}
root
= new TNODE();
root
->value = pPreOrder[0];
int inOrderRootIndex = 0;
for ( ; inOrderRootIndex != length; ++inOrderRootIndex) {
if (pInOrder[inOrderRootIndex] == pPreOrder[0]) {
break;
}
}
buildBinaryTree(reinterpret_cast
<const char*>(pPreOrder + 1), pInOrder,
inOrderRootIndex, root
->left);
buildBinaryTree(reinterpret_cast
<const char*>(pPreOrder + 1 + inOrderRootIndex),
reinterpret_cast
<const char*>(pInOrder + 1 + inOrderRootIndex),
length
- 1 - inOrderRootIndex, root->right);
}
#endif

  

转载于:https://www.cnblogs.com/lifengzhong/archive/2011/09/05/2167519.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值