数据链表的简单实现 【初识数据链表】注:
- 编译环境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;} 最后附上运行截图:第一行插入元素;
第二行删除元素;
第三行查询是否有该元素;
第四行修改元素;
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
