您的位置:首页 > 理论基础 > 数据结构算法

数据结构 第一章 绪论

2016-03-08 18:09 330 查看

程序构成说明

教材涉及的存储结构,c开头.h扩展名,c1-1.h是第1章第1种存储结构。

存储结构的基本操作,bo开头.cpp扩展名。

调用基本操作的主程序,mian开头。

实现算法的程序,algo开头。

本章程序包含的文件说明

Header.h头文件包含OK,ERROR等函数结构状态代码,Status类型,常用头文件。

C1-1.h头文件采用动态分配顺序存储结构。

Bo1-1.cpp是抽象数据类型Triplet和ElemType的8个基本操作函数,包含如下

1.InitTriplet():构建三元组。

2.DestoryTriplet():销毁三元组

3.Get():从三元组中得到第i个元素值。

4.Put():改变第i个元素值为e。

5.IsAscending():是否升序排列。

6.IsDescending():是否降序排列。

7.Max():返回最大值。

8.Min():返回最小值。

Main1-1.cpp是检验bo1-1.cpp各项操作是否正确的主函数。

编译需要注意的一点

当bo1-1.cpp加入到项目中,而且main文件中包含#include “bo1-1.cpp”,编译会在bo1-1.cpp中出错。

解决方案:将bo1-1.cpp移除项目,即可以编译通过。因为项目中所有的.cpp文件都会编译一次,生成一个.obj文件,然后所有的.obj文件链接起来就生成了可执行的程序。bo1-1.cpp源程序不完整,故不能编译通过,即使加入在bo1-1.cpp中加入头文件使之完整,而在main.cpp中包含了bo1-1.cpp,会出现bo1-1.obj重定义错误。

调试

F9在需要运行到的行设置断点,F5是运行到断点位置,F10不进入函数单步逐过程,F11进入函数单步逐语句,SHIFT+F11跳出F11进入的函数块,CTRL+F10运行到光标位置。

源码(4个)

1.header.h

/************************************
FileName: Header.h
Author: Zhengjun Zhao
Version: V1.0
Date: 2016/3/3
Description: This file include the common header file, the condition code of function
results and the data type.
Ohter: NULL
History:
1.Date:
Author:
Modification:
/************************************/

// common header file
#include <string.h>
#include <ctype.h>
#include <malloc.h>         // molloc()
#include <limits.h>         // INT_MAX
#include <stdio.h>          // EOF
#include <stdlib.h>         // atoi()
#include <io.h>             // eof()
#include <math.h>           // floor(), ceil(), abs()
#include <process.h>        // exit()
#include <iostream>       // cout, cin

// the condition code of function results
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1

// the data type
typedef int Status;         // Status is the function type, and the value of the function results
// is the condition code
typedef int Boolean;

using namespace std;


2.c1-1.h

// c1-1.h
typedef ElemType * Triplet;         // InitTriplet allocate three data storage space这里写代码片


3.bo1-1.cpp

/************************************
FileName: bo1-1.cpp
Author: Zhengjun Zhao
Version: V1.0
Date: 2016/3/3
Description: This file is the example 1-7 in the book,
include 8 operations

Ohter: NULL
History:
1.Date:
Author:
Modification:
/************************************/

Status InitTriplet(Triplet &T, ElemType v1, ElemType v2, ElemType v3)
{
// create a triplet datastruct
if (!(T = (ElemType *)malloc(3 * sizeof(ElemType))))
exit(OVERFLOW);
T[0] = v1;
T[1] = v2;
T[2] = v3;
return OK;
}

Status DestoryTriplet(Triplet &T)
{
// destory the triplet
free(T);
T = NULL;
return OK;
}

Status Get(Triplet T, int i, ElemType &e)
{
// get the value of the ith from triplet to e
if (i<1 || i>3)
{
return ERROR;
}
e = T[i - 1];
return OK;
}

Status Put(Triplet &T, int i, ElemType e)
{
// change the value of the ith from triplet into e
if (i<1 || i>3)
{
return ERROR;
}
T[i - 1] = e;
return OK;
}

Status IsAscending(Triplet T)
{
// if the triplet is ascending,return OK
return (T[0] < T[1] && T[1] < T[2]);
}

Status IsDescending(Triplet T)
{
// if the triplet is descending,return OK
return (T[0] > T[1] && T[1] > T[2]);
}

Status Max(Triplet T, ElemType &e)
{
// e equal the max of triplet
e = (T[0] >= T[1] ? (T[0] >= T[2] ? T[0] : T[2]) :
(T[1] >= T[2] ? T[1] : T[2]));
return OK;
}

Status Min(Triplet T, ElemType &e)
{
// e equal the min of triplet
e = (T[0] <= T[1] ? (T[0] <= T[2] ? T[0] : T[2]) :
(T[1] <= T[2] ? T[1] : T[2]));
return OK;
}


4.main1-1.cpp

/************************************
FileName: main1-1.cpp
Author: Zhengjun Zhao
Version: V1.0
Date: 2016/3/5
Description: bo1-1.cpp is verified by correct this file

Ohter: NULL
History:
1.Date:
Author:
Modification:
/************************************/

#include "Header.h"

typedef int ElemType;
#include "c1-1.h"

#include "bo1-1.cpp"

using namespace std;

void main()
{
Triplet T;
ElemType m;
Status i;

i = InitTriplet(T, 5, 7, 9);

cout << "init success";
cout << " T[0] =  " << T[0]
<< "\n T[1] =  " << T[1]
<< "\n T[2] =  " << T[2] << endl;

Get(T, 2, i);
cout << " T[1] = " << i << endl;

Put(T, 1, 3);
cout << " T[0] = " << T[0] << endl;

cout << " T[0] =  " << T[0]
<< "\n T[1] =  " << T[1]
<< "\n T[2] =  " << T[2] << endl;
i = IsAscending(T);
cout << i << endl;
i = IsDescending(T);
cout << i << endl;
Max(T, i);
cout << "max = " << i << endl;
Min(T, i);
cout << "min = " << i << endl;

DestoryTriplet(T);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构