前言:
博主最近刚从VS Code转到VS 2022,但发现自定义的方法和VS Code有些不同,故出一期VS 2022自定义头文件的博客,时间仓促,请不吝赐教
如何联系我?[email protected]
正文:
以"lnode.h"为例,您需要保证在头文件和源文件内均创建名为"lnode"的头文件和源文件;
lnode.h示例:
#pragma once
#include <iostream>
#include <vector>
const int MaxSize = 1e3 + 10;
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode* next;
} LNode;
void CreateLNodeRear(LNode*& L, ElemType a[], int n);
void InitLNode(LNode*& L);
void DestroyLNode(LNode*& L);
bool IfEmpty(LNode* L);
int LNodeLen(LNode* L);
void DisplayLNode(LNode* L);
bool GetElem(LNode* L, int i, ElemType& e);
int LocateElem(LNode* L, ElemType e);
bool InsertLNode(LNode*& L, int i, ElemType e);
bool DeleteLNode(LNode*& L, int i, ElemType& e);
显然,函数的声明,类的定义,结构体的定义都写在头文件内;
注意,using namespace std 最好不要写在头文件内。
推荐阅读:[C++] 头文件中不要用using namespace std
lnode.cpp示例:
#include <iostream>
#include <vector>
#include"lnode.h"
using namespace std;
void CreateLNodeRear(LNode*& L, ElemType a[], int n)
{
LNode* s, * rear;
L = (LNode*)malloc(sizeof(LNode));
rear = L;
for (int i = 0; i < n; i++)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = a[i];
rear->next = s;
rear = s;
}
rear->next = NULL;
}
void InitLNode(LNode*& L)
{
L = (LNode*)malloc(sizeof(LNode));
L->next = NULL;
}
void DestroyLNode(LNode*& L)
{
LNode* pre = L, * p = L->next;
while (p != NULL)
{
free(pre);
pre = p;
p = pre->next;
}
free(pre);
}
bool IfEmpty(LNode* L)
{
return (L->next == NULL);
}
int LNodeLen(LNode* L)
{
LNode* p = L->next;
int cnt = 0;
while (p != NULL)
{
cnt++;
p = p->next;
}
return cnt;
}
void DisplayLNode(LNode* L)
{
LNode* p = L->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
bool GetElem(LNode* L, int i, ElemType& e)
{
LNode* s = L; // s指向头结点,j置0;
int j = 0;
if (i <= 0)
return false;
while (j < i && s != NULL)
{
j++;
s = s->next;
}
if (s == NULL)
return false;
else
{
e = s->data;
return true;
}
}
int LocateElem(LNode* L, ElemType e)
{
LNode* s = L->next;
int j = 1; //数据结点指向首结点,j置为1(表明此时位置指向1)
while (s != NULL && s->data != e)
{
s = s->next;
j++;
}
if (s == NULL)
return (0);
else
{
return (j);
}
}
bool InsertLNode(LNode*& L, int i, ElemType e)
{
LNode* p = L, * s;
int j = 0;
if (i <= 0)
return false;
while (j < i - 1 && p != NULL)
{
j++;
p = p->next;
}
if (p == NULL)
return false;
else
{
s = (LNode*)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
}
bool DeleteLNode(LNode*& L, int i, ElemType& e)
{
LNode* p = L, * q;
int j = 0;
if (i <= 0)
return false;
while (j < i - 1 && p != NULL)
{
j++;
p = p->next;
}
if (p == NULL)
return false;
else
{
q = p->next;
if (q == NULL)
return false;
e = q->data;
p->next = p->next->next;
free(q);
return true;
}
}
显然,lnode.cpp内仅包含函数的实现过程,using namespace std 和头文件;
注意,lnode.cpp内一定要包含:
#include"lnode.h"
main.cpp示例:
#include<iostream>
#include<vector>
#include"lnode.h"
using namespace std;
int main()
{
LNode* L = nullptr;
h1:
cout << "1.CreateLNodeRear;2.InitLNode;3.DestroyLNode;4.IfEmpty;5.DisplayLNode;" << endl;
cout << "6.GetElem;7.LocateElem;8.InsertLNode;9.DeleteLNode;10.LNodeLen" << endl;
int flag = 0;
cin >> flag;
if (flag == 1)
{
ElemType num;
vector<ElemType> array;
cout << "请输入待输入数组array,空格间隔,回车结束" << endl;
while (1)
{
cin >> num;
array.push_back(num);
if (cin.get() == '\n')
break;
}
int len = array.size();
int a[MaxSize];
for (int i = 0; i < len; i++)
a[i] = array[i];
CreateLNodeRear(L, a, len);
}
else if (flag == 2)
{
InitLNode(L);
}
else if (flag == 3)
{
DestroyLNode(L);
}
else if (flag == 4)
{
if (IfEmpty(L))
cout << "Empty" << endl;
else
cout << "Not Empty" << endl;
}
else if (flag == 5)
{
DisplayLNode(L);
}
else if (flag == 6)
{
int i = 0, e = 0;
cout << "请输入i的值" << endl;
cin >> i;
if (GetElem(L, i, e))
cout << e << endl;
else
cout << "error!" << endl;
}
else if (flag == 7)
{
int e;
cout << "请输入待查找的e" << endl;
cin >> e;
if (LocateElem(L, e) == 0)
cout << "error!" << endl;
else
cout << LocateElem(L, e) << endl;
}
else if (flag == 8)
{
int i = 0, e = 0;
cout << "请输入i的值" << endl;
cin >> i;
cout << "请输入e的值" << endl;
cin >> e;
InsertLNode(L, i, e);
}
else if (flag == 9)
{
int i = 0, e = 0;
cout << "请输入i的值" << endl;
cin >> i;
if (!DeleteLNode(L, i, e))
cout << "error!" << endl;
else
cout << "Finished." << endl;
}
else if (flag == 10)
{
cout << LNodeLen(L) << endl;
}
else
{
cout << "未识别指令" << endl;
}
cout << "是否继续?Y/N" << endl;
char c;
cin >> c;
if (c == 'Y')
goto h1;
else
return 0;
}
显然,main.cpp内亦要包含:
#include"lnode.h"
可以把”lnode.h“想成从main.cpp函数中提取的一部分,而将lnode.cpp想象成函数的实现部分。