提交 3d2d9357 创建 作者: 宋海霞's avatar 宋海霞

template

上级
流水线 #477 已失败 于阶段
/Debug
/Release
*.bak
image: "registry.cn-hangzhou.aliyuncs.com/engintime/ubuntu_16.04_cpp:latest"
stages:
- make
- case1
- teacher-check
variables:
TEMPLATE_REPO: "engintime/ds-lab/Project-Template/Lab012.git"
.codecode-runner: &codecode-runner
tags:
- ubuntu-16.04
- short-job
make:
stage: make
<<: *codecode-runner
script:
- make
- execscore.sh 40
only:
- master
case1:
stage: case1
<<: *codecode-runner
script:
- make
- ./app > user_output.txt
- diff output.txt user_output.txt -b -B -y -i --suppress-common-lines
- execscore.sh 100
only:
- master
teacher-check:
stage: teacher-check
<<: *codecode-runner
script:
- make
- ./app
- git clone ${CODECODE_PROTOCOL}gitlab-ci-token:${CI_JOB_TOKEN}@${CODECODE_DOMAIN}/${TEMPLATE_REPO} template
- diff template/.gitlab-ci.yml .gitlab-ci.yml -b -B -y -i --suppress-common-lines
- diff template/output.txt output.txt -b -B -y -i --suppress-common-lines
- fileidentity.sh
only:
- master
when: manual
allow_failure: false
#include "Stack.h"
/*
功能:
初始化栈。
参数:
pS -- 栈的指针
*/
void InitStack(Stack* pS)
{
pS->top = -1;
}
/*
功能:
将元素入栈。
参数:
pS -- 栈的指针
Elem -- 入栈的元素
返回值:
如果插入成功返回入栈元素的值。
如果插入失败返回 -1。
*/
struct BiThrNode* Push(Stack* pS, struct BiThrNode* Elem)
{
//
// 栈满,入栈失败。
//
if(MAX_STACK_LENGTH < pS->top)
return 0;
pS->top++;
pS->buffer[pS->top] = Elem; // 将元素插入栈顶
return Elem;
}
/*
功能:
将栈顶元素出栈
参数:
pS -- 栈的指针
返回值:
如果出栈成功返回出栈元素的值。
如果出栈失败返回 -1。
*/
struct BiThrNode* Pop(Stack* pS)
{
struct BiThrNode* Elem;
//
// 栈为空,出栈失败
//
if(StackEmpty(pS))
return 0;
Elem = pS->buffer[pS->top];
pS->top--;
return Elem;
}
/*
功能:
判断栈是否为空。
参数:
pQ -- 栈的指针
返回值:
如果栈空返回 1(真)
如果栈非空返回 0(假)
*/
int StackEmpty(Stack* pS)
{
return -1 == pS->top ? 1 : 0;
}
#ifndef _STACK_H_
#define _STACK_H_
//
// 在此处包含 C 标准库头文件
//
//
// 在此处包含其他头文件
//
//
// 在此处定义数据结构
//
#define MAX_STACK_LENGTH 64 // 栈的最大长度
// 栈
struct BiThrNode;
typedef struct Stack{
struct BiThrNode* buffer[MAX_STACK_LENGTH]; // 栈的缓冲区
int top; // 指示栈顶的位置,而不是栈中元素的个数
}Stack;
//
// 在此处声明函数
//
void InitStack(Stack* pS);
struct BiThrNode* Pop(Stack* pS);
int StackEmpty(Stack* pS);
struct BiThrNode* Push(Stack* pS, struct BiThrNode* Elem);
//
// 在此处声明全局变量
//
#endif /* _STACK_H_ */
<?xml version="1.0" encoding="gb2312"?>
<OSLProject Version="1.00" Name="ThreadBinaryTree" SubjectID="cf4dda31-1275-49ef-9b0f-36a6eff372e4" ProjectTemplateID="ed341ea1-067e-4d61-9c78-52af613a1e74">
<Configurations>
<Configuration Name="Debug">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="_DEBUG" GenerateDebugInformation="-1"/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool" AdditionalDependencies="&quot;$(DSLInstallDir)Dump\lib\ThreadBinaryTree_Demo.o&quot;"/>
<Tool Name="PostBuildEventTool"/>
<VisualContext>
<WatchPoints>
<WatchPoint FunctionName="InOrderThreading" ObserverID="8AC134A6-3124-4027-BE00-56DAF54108C1">
</WatchPoint>
</WatchPoints>
</VisualContext>
</Configuration>
<Configuration Name="Release">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="NDEBUG"/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool"/>
<Tool Name="PostBuildEventTool"/>
</Configuration>
</Configurations>
<Files>
<Filter Name="ͷļ" Filter="h;hpp;hxx">
<File RelativePath=".\Stack.h">
</File>
<File RelativePath=".\ThreadBinaryTree.h">
</File>
</Filter>
<Filter Name="Դļ" Filter="cpp;c;cc;cxx">
<File RelativePath=".\main.c">
</File>
<File RelativePath=".\Stack.c">
</File>
</Filter>
<File RelativePath=".\makefile">
</File>
<File RelativePath=".\output.txt">
</File>
<File RelativePath=".\readme.md">
</File>
</Files>
</OSLProject>
#ifndef _THREADBINARYTREE_H_
#define _THREADBINARYTREE_H_
//
// 在此处包含 C 标准库头文件
//
#include <stdio.h>
//
// 在此处包含其他头文件
//
#include "Stack.h"
//
// 在此处定义数据结构
//
#define MAX_STRING_SIZE 20 // 字符串存储空间的大小
typedef char ElemType;
typedef unsigned int PtrTag;
typedef struct _BiThrNode{
ElemType data; // 二叉树节点的数据
struct _BiThrNode* lchild; // 左孩子指针
struct _BiThrNode* rchild; // 右孩子指针
PtrTag ltag, rtag; // 左右标识域
}BiThrNode, *PBiThrTree;
//
// 在此处声明函数
//
BiThrNode* CreateNode(ElemType data);
PBiThrTree InitTree();
int InOrderThreading(PBiThrTree pHead, PBiThrTree pTree);
void OutputResult(PBiThrTree pHead, PBiThrTree pTree);
//
// 在此处声明全局变量
//
extern Stack stack;
#endif /* _THREADBINARYTREE_H_ */
#include "ThreadBinaryTree.h"
Stack stack; // 栈。用于储存节点
int main(int argc, char* argv[])
{
PBiThrTree pTree; // 线索二叉树指针
PBiThrTree pHead; // 指向线索二叉树的头指针
//
// 初始化栈
//
InitStack(&stack);
//
// 创建线索二叉树
//
pTree = InitTree();
//
// 线索化二叉树
//
pHead = CreateNode(0);
InOrderThreading(pHead, pTree);
return 0;
}
/*
功能:
中序遍历二叉树 pTree ,并将其线索化,pHead 指向其头节点。
参数:
pTree -- 线索二叉树指针。
pHead -- 线索二叉树指针,指向头节点
返回值:
线索化成功返回 1。
线索化失败返回 0。
*/
int InOrderThreading(PBiThrTree pHead, PBiThrTree pTree)
{
BiThrNode* pNode; // 线索二叉树节点指针
BiThrNode* pPre; // pPre 指针指向刚刚访问过的节点
//
// TODO: 在此添加代码
//
OutputResult(pHead, pTree);
return 0;
}
/*
功能:
创建线索二叉树的一个节点。
参数:
data -- 线索二叉树节点保存的数据
返回值:
返回节点指针
*/
BiThrNode* CreateNode(ElemType data)
{
BiThrNode* pNode = (BiThrNode*)malloc(sizeof(BiThrNode));
pNode->data = data;
pNode->lchild = NULL;
pNode->rchild = NULL;
pNode->ltag = 0;
pNode->rtag = 0;
return pNode;
}
/*
功能:
利用二叉树的先序序列创建一棵二叉树。
返回值:
返回二叉树指针
*/
static const char* data = "-+ *b -c d /e"; // 二叉树的先序序列字符串。
// 注意:只使用先序序列并不能确定唯一的二叉树。
// 所以,在叶子节点后面要紧跟两个空格,
// 并且,以字符串末尾的字符 '\0' 表示序列结束。
// 这样,先序序列就可以确定唯一的二叉树了。
static int nIndex = 0; // 二叉树先序序列的下标
PBiThrTree InitTree()
{
BiThrNode* pRootNode;
if('\0' == data[nIndex]) // 二叉树的先序序列字符串结束
pRootNode = NULL;
else
{
//
// 创建父节点
//
pRootNode = (' ' == data[nIndex] ? NULL : CreateNode(data[nIndex])); // 必须忽略空节点
nIndex++;
}
if(pRootNode != NULL)
{
//
// 利用递归实现先序遍历算法
//
pRootNode->lchild = InitTree();
pRootNode->rchild = InitTree();
}
return pRootNode;
}
void OutputResult(PBiThrTree pHead, PBiThrTree pTree)
{
BiThrNode* pNode2;
BiThrNode* pPreNode2;
if(pHead->lchild == NULL)
{
return 0;
}
pNode2 = pHead->lchild;
pPreNode2 = NULL;
while(1)
{
while(1)
{
if(pNode2->ltag == 0 && (pPreNode2 == NULL || pPreNode2 != pNode2->lchild))
{
pNode2 = pNode2->lchild;
}
else if(pNode2->rtag == 0)
{
printf("%c ", pNode2->data);
pNode2 = pNode2->rchild;
}
else
{
break;
}
}
printf("%c ", pNode2->data);
pPreNode2 = pNode2;
pNode2 = pNode2->rchild;
if(pNode2 == pTree)
{
if(pNode2->data != '\0')
printf("%c ", pNode2->data);
pNode2 = pNode2->rchild;
}
if(pNode2 == pHead)
{
break;
}
}
printf("\n");
}
app:main.o Stack.o
gcc main.o Stack.o -o app
Stack.o:Stack.c Stack.h
gcc -c -w -O3 -std=c99 -fsigned-char Stack.c -o Stack.o
main.o:main.c ThreadBinaryTree.h
gcc -c -w -O3 -std=c99 -fsigned-char main.c -o main.o
+ b * c - d - e /
# 阅读实验源代码
**main.c文件**
在main函数中首先创建了一棵二叉树,然后调用函数InOrderThreading对二叉树进行线索化。
在main函数的后面,定义了函数InOrderThreading, 此函数的函数体还不完整,留给读者完成。
**ThreadBinaryTree.h文件**
定义了与线索二叉树相关的数据结构并声明了相关的操作函数和全局变量。注意,此头文件中还包含了栈模块的头文件Stack.h。
**Stack.h文件**
定义了与栈相关的数据结构并声明了相关的操作函数。
**Stack.c文件**
定义了与栈相关的操作函数。
# 在演示模式下调试项目
**按照下面的步骤调试项目:**
1. 按F7生成项目。
2. 在演示模式下,按F5启动调试项目。程序会在观察点函数的开始位置中断。
3. 重复按F5,直到调试过程结束。
在调试的过程中,每执行“演示流程”窗口中的一行后,仔细观察“可视化数据”窗口内容所发生的变化,理解二叉树线索化的执行过程。“可视化数据”窗口显示的数据信息(如下图所示),包括:
- 头结点的详细信息。包括结点地址及前驱后继的指向。
- 二叉树的详细信息。
a) 二叉树节点的值和地址。
b) 使用指针指向了该结点的左孩子和右孩子。
c) 在栈中的结点的填充色为绿色。
d) 用蓝色带箭头的虚线表示出了二叉树的线索。
e) pPre游标指向了刚刚访问过的结点,pNode游标指向了当前的结点。
- 线索化二叉树时使用的栈。显示了已入栈的二叉树节点的地址、值(空地址的值会被忽略)。
![线索二叉树](./img/12.png)
# 编写源代码并通过验证
**按照下面的步骤继续实验:**
1. 为InOrderThreading函数编写源代码。注意,尽量不要使用递归算法,而是使用栈来保存还未线索化的子树的根结点,并尽量使用已定义的局部变量。
2. 按F7生成项目。如果生成失败,根据“输出”窗口中的提示信息修改源代码中的语法错误。
3. 按Alt+F5启动验证。如果验证失败,可以使用“输出”窗口中的“比较”功能,或者在“非演示模式”下按F5启动调试后重复按F10单步调试读者编写的源代码,从而定位错误的位置,然后回到步骤1。
# 教师参考答案
访问教师参考答案[domain relative url](engintime/ds-lab/teachers-packet/Lab012.git)
\ No newline at end of file
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论