栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

初识数据链表

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

初识数据链表

数据链表的简单实现

注:

编译环境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;
}

最后附上运行截图:

第一行插入元素;
第二行删除元素;
第三行查询是否有该元素;
第四行修改元素;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/780298.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号