一、
二叉排序树的建立(由括号表达的字符串),然后转化为双向有序链表
#include<iostream>
#include<malloc.h>
#include<string>
using namespace std;
struct BSTreeNode
{
int m_nValue;
BSTreeNode *m_pLeft;
BSTreeNode *m_pRight;
};
class BSTreeLink
{
BSTreeNode *head;
BSTreeNode *tail;
BSTreeNode *root;
public:
//BSTreeLink();
//BSTreeLink(BSTreeNode *root);
BSTreeLink(char *str);
~BSTreeLink();
BSTreeNode GetRoot(){ return *root; }
BSTreeNode GetHead(){ return *head; }
BSTreeNode GetTail(){ return *tail; }
friend void InorderBSTree(BSTreeNode *root);
friend void heap(BSTreeNode *head, BSTreeNode *tail, BSTreeNode *root);
//void TreeLink();
void DispBSLink();
};
/*BSTreeLink::BSTreeLink()
{
root = NULL;
}
BSTreeLink::BSTreeLink(BSTreeNode *root)
{
this->root = root;
}*/
BSTreeLink::BSTreeLink(char *str)
{
BSTreeNode *Stack[50], *p = NULL;
int top = -1, k, j = 0;
root = NULL;
char ch = str[j];
while (ch != '\0')
{
switch (ch)
{
case'(':
k = 1; top++;
Stack[top] = p;
break;
case',':
k = 2;
break;
case')':
top--;
break;
default:
p = (BSTreeNode*)malloc(sizeof(BSTreeNode));
p->m_nValue = ch;
p->m_pLeft = p->m_pRight = NULL;
if (NULL == root)
root = p;
else
switch (k)
{
case 1:
Stack[top]->m_pLeft = p;
case 2:
Stack[top]->m_pRight = p;
}
}
j++;
ch = str[j];
}
}
void InorderBSTree(BSTreeNode *root)
{
if (root != NULL)
{
InorderBSTree(root->m_pLeft);
cout << root->m_nValue << " ";
InorderBSTree(root->m_pRight);
}
}
void heap(BSTreeNode *head, BSTreeNode *tail, BSTreeNode *root)
{
BSTreeNode *lt , *rt;
if (root->m_pLeft != NULL&&root->m_pLeft->m_pRight != NULL)
lt = root->m_pLeft->m_pRight;
else if (root->m_pLeft != NULL)
lt = root->m_pLeft;
else
lt = NULL;
if (root->m_pRight != NULL&&root->m_pRight->m_pLeft != NULL)
rt = root->m_pRight->m_pLeft;
else if (root->m_pRight != NULL)
rt = root->m_pRight;
else
rt = NULL;
if (NULL == root)
{
head = tail = NULL;
return;
}
heap(head, lt, root->m_pLeft);
heap(rt, tail, root->m_pRight);
if (NULL == lt)
{
head = root;
head->m_pLeft = NULL;
}
else
{
lt->m_pRight = root->m_pLeft;
root->m_pLeft = lt->m_pRight;
}
if (NULL == rt)
{
tail = root;
tail->m_pRight = NULL;
}
else
{
root->m_pRight = rt->m_pLeft;
rt->m_pLeft = root->m_pRight;
}
}
void BSTreeLink::DispBSLink()
{
BSTreeNode *temp;
temp = head;
while (NULL != temp)
{
cout << temp->m_nValue << " ";
temp = temp->m_pRight;
}
cout << endl;
}
/*BSTreeNode* TreeLink(BSTreeNode *root)
{
BSTreeNode *head, *tail;
heap(head, tail, root);
return head;
}*/
void main()
{
BSTreeNode *head=NULL, *tail=NULL, *root=NULL;
char *str = "10(6(4,8),14(12,16))";
BSTreeLink Bl(str);
*head = Bl.GetHead();
*tail = Bl.GetTail();
*root = Bl.GetRoot();
InorderBSTree(root);
heap(head, tail, root);
Bl.DispBSLink();
}
unresolved external symbol "public: __thiscall BSTreeLink::~BSTreeLink(void)" (??1BSTreeLink@@QAE@XZ) referenced in function _main
1>E:\CProject\Projects\ConsoleApplication8\Debug\ConsoleApplication8.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
二、
迷宫问题,只有四个方向能走
#include<iostream>
using namespace std;
#define N 10
struct Direction
{
int x, y;
int d;
};
struct
{
int m, n;
}offset[4] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; //按照可能取值取数组大小,本题路径四个方向
int b[6][6] = { { 0, 0, 0, 0, 0, 0 }, { 0, 1, 1, 1, 0, 0 }, { 0, 1, 0, 1, 1, 0 },
{ 0, 1, 1, 1, 0, 0 }, { 0, 0, 1, 1, 1, 0 }, { 0, 0, 0, 0, 0, 0 } };
class MGPath
{
Direction Stack[50];
Direction PathWay[50]; //自行定义大小为MaxSize,小心越界
int mg[N][N];
int d;
int top;
int length;
public:
MGPath();
MGPath(int a[N][N]); //应注意在a[][]中入口出口的确定,即入口出口的初始坐标
//可增加函数来确定void begend(int x1,int x2,int y1,int y2)
//此处省略按照题目默认的来
~MGPath(){}
void Push(int x, int y, int d);
void mgpath();
};
MGPath::MGPath() //默认迷宫按照本题指定赋值
{
top = -1;
d = 0;
length = 50;
int i, j;
for (i = 0; i < 6; i++)
for (j = 0; j < 6; j++)
mg[i][j] = b[i][j];
}
/*MGPath::MGPath(int a[N][N]) //此处自己设定迷宫,若用自己设定的迷宫则需自己指定出口入口
{
top = -1;
d = 0;
length = 50;
int i, j;
for (i = 0; i < 10;i++)
for (j = 0; j < 10; j++)
mg[i][j] = a[i][j];
}*/
void MGPath::Push(int x, int y, int d) //迷宫坐标入栈
{
top++;
Stack[top].x = x;
Stack[top].y = y;
Stack[top].d = d;
mg[x][y] = 0;
}
void MGPath::mgpath()
{
int a, b, g, h, di;
int i;
int find;
Push(1, 1, 0); //a,b初始坐标,di初始方向
while (top > -1)
{
a = Stack[top].x;
b = Stack[top].y;
di = Stack[top].d; ///////////////////
if (a == 4 && b == 4)
{
for (i = 0; i <= top; i++)
cout << '(' << Stack[i].x << ',' << Stack[i].y << ')' << endl;
cout << endl;
if (top + 1 < length)
{
for (i = 0; i <= top; i++)
PathWay[i] = Stack[i];
length = top + 1;
}
mg[a][b] = 1;
top--;
a = Stack[top].x;
b = Stack[top].y;
di = Stack[top].d + 1;
}
find = 0;
while (di <= 3&&(find==0))
{
g = a + offset[di].m;
h = b + offset[di].n;
if (mg[g][h] == 1)
find = 1;
else
di++;
}
if (!(find))
{
top--;
mg[a][b] = 1;
}
else
{
Stack[top].d = di;
Push(g, h, 0);
}
}
for (i = 0; i < length;i++)
cout << '(' << PathWay[i].x << ',' << PathWay[i].y << ')' << endl;
}
void main()
{
MGPath Mp;
Mp.mgpath();
}
#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct node
{
char data;
node *lchild;
node *rchild;
}BTNode;
void CreateBTNode(BTNode *&b, char *str)
{
BTNode *Stack[100], *p = NULL;
int top = -1, k, j = 0;
b = NULL;
char ch;
ch = str[j];
while (ch != '\0')
{
switch (ch)
{
case'(':k = 1; top++;Stack[top] = p;break;
case',':k = 2;break;
case')':top--;break;
default:p = (BTNode*)malloc(sizeof(BTNode));
p->data = ch;
p->lchild = p->rchild = NULL;
if (NULL == b)
b = p;
else
{
if (1==k) //这里有问题,本来用switch
Stack[top]->lchild = p;//,改了if还是不行
else
Stack[top]->rchild = p;
}
}
j++;
ch = str[j];
}
}
void Inorder(BTNode *b)
{
if (NULL != b)
{
Inorder(b->lchild);
cout << b->data << " ";
Inorder(b->rchild);
}
}
void main()
{
char *str = "10(6(4,8),14(12,16))";
BTNode *b;
CreateBTNode(b, str);
Inorder(b);
}
三、
层层嵌套的循环模式如何简化
打印出n的所有可能和的组合,如n=3
int n = 3;
int a[3], sumi = 0;
for (a[0] = 1; a[0] <= n - sumi; a[0]++)
{
cout << n << '=' << a[0];
sumi += a[0];
for (a[1] = 1; a[1] <= n - sumi; a[1]++)
{
cout << '+' << a[1];
sumi += a[1];
for (a[2] = 1; a[2] <= n - sumi; a[2]++)
{
cout << '+' << a[2];
sumi += a[2];
}
}
}
cout << endl << sumi << endl;
代码是错的。。。
四、
递归的恢复环境什么时候必要什么时候不必要
不回答,但是请允许我吐槽——哪个公司还在用void main这样的声明方式?
说明这家公司真的很大
@ayiis: 很大很大,大概有两三个篮球场大;很高很高,大概有两三层楼那么高。。。
貌似没人知道,要命
发的时候拍下版啊,最怕这种乱序的代码。。