注:
编译环境vs2019;面向初学者,各路大佬不必浪费自己的时间阅读;如有问题欢迎留言或私信讨论;
首先我们要知道数据链表的基本结构
在我的链表中写入了三个基本量:容量,当前容量,指针实现了如下功能:增删改查了解一个知识点:指针通过动态内存申请即可变为变量
结构化:
function.p中的代码段
enum TwoInformation { ERROR = -1, OK = 1 };
//定义枚举类型,方便错误查找
typedef struct myList
{
int Capacity;
int cur_Size;
int* p_MyArr;
}MYLIST, * p_MyList;
p_MyList createList(int);
int AddLData(p_MyList& p, int data);
int delData(p_MyList& p, int);
void show(p_MyList& p);
int Empty(p_MyList& p);
int getSize(p_MyList& p);
int lookup(p_MyList& p,int );
int modify(p_MyList& p, int, int);
function.cpp中代码(函数的具体实现):
#include#include #include"function.h" using namespace std; p_MyList createList(int max_Size) { p_MyList plist = (p_MyList)malloc(sizeof(MYLIST)); assert(plist); //关于assert函数大概就是用来看内存是否申请成功,具体克自己搜索 plist->cur_Size = 0; plist->Capacity = max_Size; plist->p_MyArr = (int*)malloc(sizeof(int) * max_Size); assert(plist->p_MyArr); return plist; } int AddLData(p_MyList& p, int data) { if (p->cur_Size >= p->Capacity) { cout << "List is full!" << endl; return ERROR; } p->p_MyArr[p->cur_Size++] = data; return OK; } int delData(p_MyList& p, int position) { if (p->cur_Size <= 0) { cout << "List is Empty!" << endl; return ERROR; } if (p->cur_Size == (position - 1)) p->cur_Size--; else { for (int i = (position - 1); i < p->cur_Size; i++) { p->p_MyArr[i] = p->p_MyArr[i + 1]; } } p->cur_Size--; return OK; } void show(p_MyList& p) { for (int i = 0; i < p->cur_Size; i++) cout << p->p_MyArr[i] << ' '; } int Empty(p_MyList& p) { if (p->Capacity == NULL) return ERROR; return p->Capacity == 0; } int getSize(p_MyList& p) { return p->cur_Size; } int lookup(p_MyList& p, int data) { if (p->cur_Size <= 0) { cout << "List is Empty!" << endl; return ERROR; } for (int i = 0; i < p->cur_Size; i++) { if (p->p_MyArr[i] == data) { cout << endl; cout << "Query to number " << data << endl; return OK; } } cout << endl; cout << "no seek to number" << endl; return OK; } int modify(p_MyList& p, int position, int data) { if (p->cur_Size <= 0) { cout << "List is Empty!"; return ERROR; } for (int i = 0; i < p->cur_Size; i++) { if (i == position - 1) { p->p_MyArr[i] = data; } } return OK; }
main.cpp中调用:
#include#include"class.hpp" #include"function.h" using namespace std; void test01() { p_MyList p = createList(10); for (int i = 0; i < 10; i++) { AddLData(p, i); } show(p); delData(p,10); cout << endl; show(p); lookup(p, 8); modify(p, 2, 9); show(p); } int main(int argv, char* argc) { test01(); char ch=getchar(); return 0; }
最后附上运行截图:
第一行插入元素;
第二行删除元素;
第三行查询是否有该元素;
第四行修改元素;



