顺序表
#include <iostream>
using namespace std;
const int INITLENGTH = 10;
const int INCREMENT = 10;
const int INVALIDDATA = 100;
struct Node
{
int* data;
int length;
int size;
};
struct Equal
{
bool operator()(const int& left, const int& right)
{
return (left == right) ? true : false;
}
};
bool InitList(Node* &list);
void Destory(Node* list);
bool ListEmpty(Node* &list);
int ListLength(Node* &list);
bool GetElem(Node* list,int i,int& e);
int LocateElem(Node* list,int e,Equal equ);
bool ListInsert(Node* &list,int i,int e);
bool ListDelete(Node* list,int i,int& e);
void BiggerSpace(Node* &NewList);
void PrintList(Node* list);
void InvalidOrNot(Node* &L);
int main()
{
Node* MyList;
InitList(MyList);
// ListInsert(MyList,1,11);
ListInsert(MyList,1,10);
ListInsert(MyList,1,9);
ListInsert(MyList,1,8);
ListInsert(MyList,1,7);
ListInsert(MyList,1,6);
ListInsert(MyList,1,5);
ListInsert(MyList,1,4);
ListInsert(MyList,1,3);
ListInsert(MyList,1,2);
ListInsert(MyList,1,1);
// if (true == ListEmpty(MyList))
// {
// cout<<"Empty"<<endl;
// }
// else
// {
// cout<<"Not Empty"<<endl;
// }
//
// cout<<ListLength(MyList)<<endl;
// PrintList(MyList);
// cout<<LocateElem(MyList,3,Equal())<<endl;
Destory(MyList);
return 0;
}
bool InitList(Node* &list)
{
list = new Node;
list->data = new int[INITLENGTH];
for (int i = 0; i < INITLENGTH; ++i)
{
list->data[i] = INVALIDDATA;
}
list->length = 0;
list->size = INITLENGTH * sizeof(int);
return true;
}
void Destory(Node* list)
{
InvalidOrNot(list);
delete [] list->data;
delete [] list;
list = NULL;
}
bool ListEmpty(Node* &list)
{
InvalidOrNot(list);
return (list->length == 0) ? true : false;
}
int ListLength(Node* &list)
{
InvalidOrNot(list);
return list->length;
}
bool GetElem(Node* list,int i,int& e)
{
InvalidOrNot(list);
if (i > 0 && i <= ListLength(list))
{
e = list->data[i - 1];
return true;
}
return false;
}
int LocateElem(Node* list,int e,Equal equ)
{
int iLoc = -1;
for (int i = 0; i < ListLength(list); ++i)
{
if (equ(list->data[i],e))
{
iLoc = ++i;
return iLoc;
}
}
return iLoc;
}
bool ListInsert(Node* &list,int i,int e)
{
InvalidOrNot(list);
if (i < 1 || i > ListLength(list) + 1)
return false;
if (list->length * sizeof(int) == list->size)
{
BiggerSpace(list);
}
for (int j = ListLength(list) ; j >= i; j--)
{
list->data[j] = list->data[j - 1];
}
list->data[i - 1] = e;
++list->length;
return true;
}
bool ListDelete(Node* list,int i,int& e)
{
InvalidOrNot(list);
if (true == ListEmpty(list))
return false;
if (i < 1 || i > ListLength(list))
return false;
e = list->data[i - 1];
for (int j = i - 1; j < ListLength(list) - 1; j++)
list->data[j] = list->data[j + 1];
list->data[ListLength(list) - 1] = INVALIDDATA;
--list->length;
return true;
}
void BiggerSpace(Node* &NewList)
{
int* temp = new int[NewList->length];
for (int i = 0; i < NewList->length; ++i)
{
temp[i] = NewList->data[i];
}
delete [] NewList->data;
NewList->data = new int[NewList->length + INCREMENT];
NewList->size = NewList->size + INCREMENT * sizeof(int);
for (int i = 0; i < NewList->length; ++i)
{
NewList->data[i] = temp[i];
}
delete [] temp;
temp = NULL;
for (int i = NewList->length; i < NewList->length + INCREMENT; i++)
{
NewList->data[i] = INVALIDDATA;
}
}
void PrintList(Node* list)
{
InvalidOrNot(list);
if (0 == ListLength(list))
return ;
for (int i = 0; i < ListLength(list); ++i)
cout<<list->data[i]<<"\t";
cout<<endl;
}
void InvalidOrNot(Node* &L)
{
if (NULL == L)
{
cout<<"ERROR"<<endl;
exit(1);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现