project rebuild, add test code, fix memory leak, etc
This commit is contained in:
58
include/Functions.h
Normal file
58
include/Functions.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include "SqList.h"
|
||||
#include "SqStack.h"
|
||||
|
||||
// 发牌
|
||||
inline void DealCards(int N, SqList<int>*& PA, SqList<int>*& PB)
|
||||
{
|
||||
int* CardPile = new int[N]; // 发牌堆
|
||||
srand((int)time(nullptr)); // 基于当前时间设置随机数种子
|
||||
for (int i = 0; i < N; i++) CardPile[i] = 2; // 默认情况下每个面值有两张牌
|
||||
for (int i = 0; i < N * 2; i++)
|
||||
{
|
||||
int j = 1, k = 1;
|
||||
int num = rand() % N; // 获取一个 0~N-1 的随机数
|
||||
while (true)
|
||||
{
|
||||
if (CardPile[num] != 0) // 如果该数对应面值的牌未发完
|
||||
{
|
||||
if (i % 2 == 0) // 发给PA
|
||||
{
|
||||
InsertElem_i(*PA, j++, num + 1);
|
||||
CardPile[num]--;
|
||||
}
|
||||
else // 发给PB
|
||||
{
|
||||
InsertElem_i(*PB, k++, num + 1);
|
||||
CardPile[num]--;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else num = (num + 1) % N; // 若发完则检查下一位数字对应面值的牌
|
||||
}
|
||||
}
|
||||
delete[] CardPile; // 释放发牌堆占用的内存
|
||||
}
|
||||
|
||||
// 寻找栈内元素并返回其位置
|
||||
inline int FindElem(SqStack<int>& S, int e)
|
||||
{
|
||||
int i = S.top; // 从栈顶开始查找
|
||||
for (; i >= 0; i--)
|
||||
if (S.base[i] == e) return S.top - i; // 返回距离
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 判断谁的回合
|
||||
inline bool IsPlayerA(int i)
|
||||
{
|
||||
return i % 2 == 1;
|
||||
}
|
||||
|
||||
// 返回栈元素数量
|
||||
inline int StackSize(SqStack<int>& S)
|
||||
{
|
||||
return S.top + 1;
|
||||
}
|
||||
178
include/SqList.h
Normal file
178
include/SqList.h
Normal file
@@ -0,0 +1,178 @@
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
|
||||
template <class DT>
|
||||
struct SqList // 顺序表
|
||||
{
|
||||
DT* elem; // 表首址
|
||||
int length; // 表长
|
||||
int size; // 表容量
|
||||
};
|
||||
|
||||
//算法2.1
|
||||
template <class DT>
|
||||
bool PriorElem_e(SqList<DT> L, DT e, DT& pre_e) // 求值为e的元素前驱
|
||||
{
|
||||
int k;
|
||||
k = LocateElem_e(L, e);
|
||||
if (k > 1)
|
||||
{
|
||||
GetElem_i(L, k - 1, pre_e);
|
||||
return false;
|
||||
}
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
//【算法2.2】 // 初始化
|
||||
template <class DT>
|
||||
bool InitList(SqList<DT>& L, int m)
|
||||
{
|
||||
L.elem = new DT[m]; // 申请表空间
|
||||
if (L.elem == NULL)
|
||||
{
|
||||
cout << "未创建成功!"; // 申请不成功,退出
|
||||
exit(1);
|
||||
}
|
||||
L.length = 0; // 申请成功,属性赋值。空表,表长为0
|
||||
L.size = m; // 表容量为m
|
||||
return true;
|
||||
}
|
||||
|
||||
//【算法2.3】 // 创建表元素
|
||||
template <class DT>
|
||||
bool CreateList(SqList<DT>& L, int n)
|
||||
{
|
||||
int i;
|
||||
if (n > L.size) // 1.元素个数大于表容量,不能创建。
|
||||
{
|
||||
cout << "元素个数大于表长,不能创建!" << endl;
|
||||
return false;
|
||||
}
|
||||
cout << "请依次输入" << n << "个元素值:" << endl; // 2.依位序输入各元素值
|
||||
for (i = 1; i <= n; i++)
|
||||
cin >> L.elem[i - 1];
|
||||
L.length = n; // 3.表长为创建的元素个数
|
||||
return true;
|
||||
}
|
||||
|
||||
//【算法2.4】 // 销毁顺序表
|
||||
template <class DT>
|
||||
void DestroyList(SqList<DT>& L)
|
||||
{
|
||||
delete[] L.elem; // 1.释放表空间
|
||||
L.length = 0; // 2.属性赋值
|
||||
L.size = 0;
|
||||
}
|
||||
|
||||
//【算法2.5】 // 获取第i个元素值
|
||||
template<class DT>
|
||||
bool GetElem_i(SqList<DT> L, int i, DT& e)
|
||||
{
|
||||
if (i<1 || i>L.length) // 1.位序不合理,返回false
|
||||
{
|
||||
cout << "该元素不存在!" << endl;
|
||||
return false;
|
||||
}
|
||||
e = L.elem[i - 1]; // 2. 否则,获取第i个元素值
|
||||
return true; // 返回true
|
||||
}
|
||||
|
||||
//【算法2.6】 // 按值查找
|
||||
template<class DT>
|
||||
int LocateElem_e(SqList<DT> L, DT e)
|
||||
{
|
||||
for (int i = 1; i <= L.length; i++) // 顺序查找
|
||||
if (L.elem[i - 1] == e) // 1.找到
|
||||
return i; // 返回元素位序
|
||||
return 0; // 2.未找到,返回0
|
||||
}
|
||||
|
||||
//【算法2.7】
|
||||
template<class DT>
|
||||
bool InsertElem_i(SqList<DT>& L, int i, DT e)
|
||||
{
|
||||
if (L.length >= L.size) // 1.表满,不能插入
|
||||
return false;
|
||||
if (i<1 || i>L.length + 1) // 2.插入位置不合理,不能插入
|
||||
return false;
|
||||
for (int j = L.length; j >= i; j--) // 3. an~ai依次后移
|
||||
L.elem[j] = L.elem[j - 1];
|
||||
L.elem[i - 1] = e;
|
||||
L.length++;
|
||||
return true; // 插入成功,返回true
|
||||
}
|
||||
|
||||
//【算法2.8】 // 删除第i个元素
|
||||
template<class DT>
|
||||
bool DeleElem_i(SqList<DT>& L, int i)
|
||||
{
|
||||
if (L.length == 0) // 1.表空,不能删除
|
||||
return false;
|
||||
if (i<1 || i>L.length) // 2.删除位置不合理,不能插入
|
||||
return false;
|
||||
for (int j = i; j < L.length; j++) // 3. ai+1~an依次前移
|
||||
L.elem[j - 1] = L.elem[j];
|
||||
L.length--;
|
||||
return true; // 删除成功,返回true
|
||||
}
|
||||
|
||||
|
||||
//【算法2.9】
|
||||
template<class DT>
|
||||
bool PutElem(SqList<DT>& L, int i, DT e) // 修改第i个元素的值
|
||||
{
|
||||
if (i<1 || i>L.length) // 1.位序不合理,不能修改,
|
||||
return false; // 返回false
|
||||
L.elem[i - 1] = e; // 2.重置第i个元素值
|
||||
return true; // 3.修改成功,返回true
|
||||
}
|
||||
|
||||
// 清空顺序表
|
||||
template<class DT>
|
||||
void ClearList(SqList<DT>& L)
|
||||
{
|
||||
L.length = 0; // 空表,表长为0
|
||||
}
|
||||
|
||||
|
||||
// 测表长
|
||||
template<class DT>
|
||||
int ListLength(SqList<DT> L)
|
||||
{
|
||||
return L.length;
|
||||
}
|
||||
|
||||
|
||||
template<class DT>
|
||||
bool ListEmpty(SqList<DT> L) // 测表空
|
||||
{
|
||||
if (L.length == 0) // 空表,返回true
|
||||
return true;
|
||||
else
|
||||
return false; // 非空表,返回false
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
bool ListFull(SqList<DT> L)
|
||||
{
|
||||
if (L.length == L.size) // 表满,返回true
|
||||
return true;
|
||||
else
|
||||
return false; // 表不满,返回false
|
||||
}
|
||||
|
||||
//【算法2.10】 // 遍历输出
|
||||
template <class DT>
|
||||
void DispList(SqList<DT> L)
|
||||
{
|
||||
int i;
|
||||
for (i = 1; i <= L.length; i++) // 依位序输出元素值
|
||||
{
|
||||
cout << L.elem[i - 1] << " ";
|
||||
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
|
||||
102
include/SqQueue.h
Normal file
102
include/SqQueue.h
Normal file
@@ -0,0 +1,102 @@
|
||||
#pragma once
|
||||
|
||||
template<class DT>
|
||||
struct SqQueue
|
||||
{
|
||||
DT* base; // 存储空间基地址
|
||||
int front; // 队头指针,指向队首元素
|
||||
int rear; // 队尾指针,指向队尾元素的后面
|
||||
int queuesize; // 队列容量
|
||||
};
|
||||
|
||||
template<class DT> // 算法3.14 初始化队列
|
||||
bool InitQueue(SqQueue<DT>& Q, int m) // 创建容量为m的空队列
|
||||
{
|
||||
Q.base = new DT[m]; // 1. 申请一组连续的内存空间
|
||||
if (!Q.base) exit(1); // 申请失败,退出
|
||||
Q.front = Q.rear = 0; // 2. 申请成功,为队列属性赋值
|
||||
Q.queuesize = m;
|
||||
return true; // 3. 返回true
|
||||
}
|
||||
|
||||
template<class DT> // 算法3.15 销毁队列
|
||||
void DestroyQueue(SqQueue<DT>& Q) // 销毁循环队列
|
||||
{
|
||||
delete[] Q.base; // 1. 释放循环队列占用的内存空间
|
||||
Q.front = Q.rear = 0; // 2. 为队列属性赋值
|
||||
Q.queuesize = 0;
|
||||
}
|
||||
|
||||
template<class DT> // 算法3.16 入队
|
||||
bool EnQueue(SqQueue<DT>& Q, DT e) // 在队尾插入一个新元素
|
||||
{
|
||||
if ((Q.rear + 1) % Q.queuesize == Q.front) // 1. 队满的情况
|
||||
return false; // 无法入队,返回false
|
||||
Q.base[Q.rear] = e; // 2. 元素e放在队尾指针处
|
||||
Q.rear = (Q.rear + 1) % Q.queuesize; // 队尾指针增1
|
||||
return true; // 3. 返回true
|
||||
}
|
||||
|
||||
template<class DT> // 算法3.17 出队
|
||||
bool DeQueue(SqQueue<DT>& Q, DT& e) // 删除队头元素
|
||||
{
|
||||
if (Q.front == Q.rear) // 1. 队空的情况
|
||||
return false; // 无法出队,返回false
|
||||
e = Q.base[Q.front]; // 2. 取队头元素,赋值给e
|
||||
Q.front = (Q.front + 1) % Q.queuesize; // 队头指针加1
|
||||
return true; // 3. 返回true
|
||||
}
|
||||
|
||||
template<class DT> // 算法3.18 取队头元素
|
||||
bool GetHead(SqQueue<DT> Q, DT& e) // 取队头元素
|
||||
{
|
||||
if (Q.front == Q.rear) // 1. 队空的情况
|
||||
return false; // 无队头元素,返回false
|
||||
e = Q.base[Q.front]; // 2. 取队头元素,赋值给e
|
||||
return true; // 3. 返回true
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
bool GetTail(SqQueue<DT> Q, DT& e) // 取队尾元素
|
||||
{
|
||||
if (Q.front == Q.rear) // 1. 队空的情况
|
||||
return false; // 无队尾元素,返回false
|
||||
e = Q.base[(Q.rear - 1 + Q.queuesize) % Q.queuesize]; // 2. 取队尾元素,赋值给e
|
||||
return true; // 3. 返回true
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
int QueueLength(SqQueue<DT> Q) // 测表长
|
||||
{
|
||||
return (Q.rear - Q.front + Q.queuesize) % Q.queuesize;
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
bool QueueEmpty(SqQueue<DT> Q) // 判队空
|
||||
{
|
||||
return Q.front == Q.rear;
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
bool QueueFull(SqQueue<DT> Q) // 判队满
|
||||
{
|
||||
return (Q.rear + 1) % Q.queuesize == Q.front;
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
void ClearQueue(SqQueue<DT>& Q) // 清空队
|
||||
{
|
||||
Q.front = Q.rear = 0;
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
void DispQueue(SqQueue<DT> Q) // 显示队
|
||||
{
|
||||
int i = Q.front;
|
||||
while (i != Q.rear)
|
||||
{
|
||||
cout << Q.base[i] << " ";
|
||||
i = (i + 1) % Q.queuesize;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
109
include/SqStack.h
Normal file
109
include/SqStack.h
Normal file
@@ -0,0 +1,109 @@
|
||||
#pragma once
|
||||
|
||||
template <class DT>
|
||||
struct SqStack // 顺序栈
|
||||
{
|
||||
DT* base; // 栈首址
|
||||
int top; // 栈顶指针
|
||||
int stacksize; // 栈容量
|
||||
};
|
||||
|
||||
//基本操作的实现
|
||||
//【算法3.1】 // 初始化栈
|
||||
template <class DT>
|
||||
void InitStack(SqStack<DT>& S, int m)
|
||||
{
|
||||
S.base = new DT[m]; // 申请栈空间
|
||||
if (S.base == NULL) // 申请失败,退出
|
||||
{
|
||||
cout << "未创建成功!";
|
||||
exit(1);
|
||||
}
|
||||
S.top = -1; // 设置空栈属性
|
||||
S.stacksize = m;
|
||||
}
|
||||
|
||||
|
||||
//【算法3.2】 // 销毁栈
|
||||
template <class DT>
|
||||
void DestroyStack(SqStack<DT>& S)
|
||||
{
|
||||
delete[] S.base; // 释放栈空间
|
||||
S.top = -1;
|
||||
S.stacksize = 0; // 设置栈属性
|
||||
}
|
||||
|
||||
|
||||
//【算法3.3】 // 入栈
|
||||
template<class DT>
|
||||
bool Push(SqStack<DT>& S, DT e)
|
||||
{
|
||||
if (S.top == S.stacksize - 1) // 栈满,不能入栈
|
||||
return false; // 返回false
|
||||
S.top++;
|
||||
S.base[S.top] = e;
|
||||
return true; // 入栈成功,返回true
|
||||
}
|
||||
|
||||
//【算法3.4】 // 出栈
|
||||
template<class DT>
|
||||
bool Pop(SqStack<DT>& S, DT& e)
|
||||
{
|
||||
if (S.top == -1) // 栈空
|
||||
return false; // 返回false
|
||||
e = S.base[S.top]; // 取栈顶元素
|
||||
S.top--; // 栈顶指针下移
|
||||
return true; // 出栈成功,返回true
|
||||
}
|
||||
|
||||
|
||||
//【算法3.5】 // 获取栈顶元素
|
||||
template<class DT>
|
||||
bool GetTop(SqStack<DT> S, DT& e)
|
||||
{
|
||||
if (S.top == -1) // 栈空
|
||||
return false; // 返回false
|
||||
e = S.base[S.top]; // 栈非空,取栈顶元素
|
||||
return true; // 返回true
|
||||
}
|
||||
|
||||
//测栈空
|
||||
template<class DT>
|
||||
bool StackEmpty(SqStack<DT> S)
|
||||
{
|
||||
if (S.top == -1) // 空栈,返回true
|
||||
return true;
|
||||
else // 空栈,返回false
|
||||
return false;
|
||||
}
|
||||
|
||||
//显示栈内容
|
||||
template<class DT>
|
||||
void DispStack(SqStack<DT> S)
|
||||
{
|
||||
int i = S.top;
|
||||
while (i >= 0)
|
||||
{
|
||||
cout << S.base[i--] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
void ClearStack(SqStack<DT>& S)
|
||||
{
|
||||
S.top = -1;
|
||||
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
int StackFull(SqStack<DT> S)
|
||||
{
|
||||
if (S.top == S.stacksize - 1)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user