標頭檔案的編寫和引用
我用的是 Dev-c++ 編寫標頭檔案
首先寫標頭檔案裡面的函式,
然後儲存,記得字尾寫 .h 就行
例:(順序表的標頭檔案)
#define MAXSize 50
#define OVERFLOW -2
#define ElemType int
using namespace std;
typedef struct List{
ElemType *elem;
int length;
}SqList;
void InitList(SqList &L)
{ //構造一個空的順序表
L.elem=new ElemType[MAXSize];
if (!L.elem)
exit(OVERFLOW);
L.length=0;
}
void ClearList(SqList &L)
{ //清空線性表,不銷燬
L.length=0;
}
int ListLength (SqList L)
{ //求線性表長度
return (L.length);
}
bool ListInsert (SqList &L, int i , ElemType e)
{ //線上性表L中第i個數據元素之前插入新資料元素e
int j;
if (i<1||i>L.length+1)
return false;
if (L.length>=MAXSize)
return false;
for (j=L.length;j>=i;--j)
L.elem[j]=L.elem[j-1];
L.elem[i-1]=e;
++L.length;
return true;
}
ElemType GetElem(SqList L, int i)
{ //線上性表L中求序號為i的元素,該元素作為函式返回值
if (i<1||i>L.length){
printf("i不在[1..n]範圍內");
exit(OVERFLOW);
}
return (L.elem[i-1]);
}
#define MAXSize 50
#define OVERFLOW -2
#define ElemType int
using namespace std;
typedef struct List{
ElemType *elem;
int length;
}SqList;
void InitList(SqList &L)
{ //構造一個空的順序表
L.elem=new ElemType[MAXSize];
if (!L.elem)
exit(OVERFLOW);
L.length=0;
}
void ClearList(SqList &L)
{ //清空線性表,不銷燬
L.length=0;
}
int ListLength (SqList L)
{ //求線性表長度
return (L.length);
}
bool ListInsert (SqList &L, int i , ElemType e)
{ //線上性表L中第i個數據元素之前插入新資料元素e
int j;
if (i<1||i>L.length+1)
return false;
if (L.length>=MAXSize)
return false;
for (j=L.length;j>=i;--j)
L.elem[j]=L.elem[j-1];
L.elem[i-1]=e;
++L.length;
return true;
}
ElemType GetElem(SqList L, int i)
{ //線上性表L中求序號為i的元素,該元素作為函式返回值
if (i<1||i>L.length){
printf("i不在[1..n]範圍內");
exit(OVERFLOW);
}
return (L.elem[i-1]);
}
然後儲存
標頭檔案就寫好了,然後引用
方法1:
#include"E:\List.h" // #include" 標頭檔案存放路徑+標頭檔案名"
方法2:
將編寫的標頭檔案 和 正準備呼叫 該標頭檔案 的專案檔案放在同一個資料夾裡面