企业通用链表实现

企业通用链表实现

通用链表:因为结构体的第一个成员变量的地址和结构体的地址是同一个地址,所以放一个结构体,内部只有一个成员变量指针,用来进行链表的操作,将具体的算法和数据类型相分离;实现了一种"我不包含万物,万物包含我的"哲学思想;
模型如下:
在这里插入图片描述
1.链表头文件,本例使用了动态库方法

#ifndef  _UNIVERSAL_LIST_H_
#define _UNIVERSAL_LIST_H_
#ifdef _cplusplus
extern "C" {
#endif

	typedef struct _linknode
	{
		struct _linknode * next;
	}linknode;//节点只包含next指针域
	__declspec(dllexport)
		void* linklist_creat();//创建
	__declspec(dllexport)
		int linklist_insert(void* list, linknode* node, int pos);//插入
	__declspec(dllexport)
		int linklist_length(void* list);
	__declspec(dllexport)
		linknode* linklist_get(void* list, int pos);//获取特定位置
	__declspec(dllexport)
		linknode* linklist_dele(void* list, int pos);

#ifdef _cplusplus
}
#endif // ! _UNIVERSAL_LIST_H
#endif


2.cpp文件

#include "iostream"
#include "universal_list.h"
using namespace std;

typedef struct _linklist
{
	linknode *node;
	int length;
}linklist;//链表

void* linklist_creat()//void* 返回任意类型的指针,可通过强制类型转换成具体类型指针
{
	linklist* list = NULL;
	list = new linklist;      //创建链表
	list->node = new linknode; //创建头节点
	list->length = 0;
	list->node->next = NULL;
	return list;               //返回动态库内部类型链表
}

int linklist_insert(void* list, linknode* node, int pos)
{
	int ret = 0;
	if (list == NULL || node == NULL || pos <0)
	{
		ret = -1;
		cout << "func linklist_insert() err:" << ret << endl;
		return ret;
	}
	linklist *tmp = (linklist*)list; //将外部类型链表强制转换成动态库内类型链表
	linknode * current = tmp->node;
	int i = 0;
	while (current->next != NULL && i <= pos)//pos = 0,1,2,3....
	{
		current = current->next;
		i++;
	}
	node->next = current->next;
	current->next = node;
	tmp->length++;
}
int linklist_length(void* list)
{
	linklist *tmp = (linklist*)list;
	return tmp->length;
}

linknode* linklist_get(void* list, int pos)
{
	linklist *tmp = (linklist*)list;
	linknode *current = tmp->node;
	if (current->next == NULL)
	{
		cout << "链表为空" << endl;
	}
	int i = 0;
	while (current->next != NULL && i <= pos)
	{
		current = current->next;
		i++;
	}
	return current;


}
linknode* linklist_dele(void* list, int pos)
{
	linklist* tmp = (linklist*)list;
	linknode *current = tmp->node;
	linknode *icurrent = NULL;
	if (tmp->length == 0)
	{
		cout << "链表为空" << endl;
		return NULL;
	}
	int i = 0;
	while (current->next != NULL && i <= pos - 1)
	{
		current = current->next;
		i++;
	}
	icurrent = current->next;
	current->next = icurrent->next;
	tmp->length--;
	return icurrent;
}

3.主程序

#include "universal_list.h"
#include "iostream"
using namespace std;

typedef struct
{
	linknode *node;//将节点放在首位,跟student地址重叠,就可以实现student和linknode 之间的 强制转换
	char name[10];
	int age;
	int scort;
}student;


void main()
{
	student s1,s2,s3,s4;
	s1.age = 13;
	s2.age = 14;
	s3.age = 21;
	s4.age = 22;
	void* list = NULL; //不知道动态库内链表类型,所以创建void*类型
	list = linklist_creat();
	linklist_insert(list, (linknode*)(&s1), 0);
	linklist_insert(list, (linknode*)(&s2), 1);
	linklist_insert(list, (linknode*)(&s3), 2);
	linklist_insert(list, (linknode*)(&s4), 3);
	linklist_dele(list, 3);
	for (int i = 0; i < linklist_length(list); i++)
	{
		student* s = (student*)linklist_get(list,i);
		cout << s->age << endl;

	}
	system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值