03数据结构——单向企业链表(小挂钩)

1.思想

与之前单向顺序链表的区别就是,结构体结点没有直接开辟内存,仅是开辟小挂钩连接,在自定义数据中开辟结点内存空间。

代码实现:

头文件.h

#include <iostream>
using namespace std;
//创建结构体
//小挂钩
typedef struct SNode {
	struct SNode* next;

}Snode;

typedef struct LinkList {
	Snode head;///不用分配内存,通过小挂钩实现
	int size;
}List;
typedef void(*PRINTLIST)(Snode*);
typedef int(*MYCOMPARE)(Snode *,Snode *);
List* init_List();//初始化
void insert_List(List *list,int pos,Snode *data);//增加
void delete_List(List* list,int pos);   //删除
Snode *findvalue_List(List* list,int pos);//查询
int findpos_List(List *list,Snode *data,MYCOMPARE myc);
void print_List(List* list,PRINTLIST print);//打印
void free_List(List* list);//摧毁内存

.cpp文件

#include "Linklist.h"

List* init_List()
{
	List* list = (List*)malloc(sizeof(List));
	list->head.next = NULL;
	list->size = 0;
	return list;
}

void insert_List(List* list,int pos, Snode* data)
{
	if (list == NULL) {
		cout<<"链表数据为空或者不存在!" << endl;
	}
	if (pos<0 || pos>list->size) {
		cout<<"你的输入有误!" << endl;
	}
	SNode* pPre = &(list->head);
	for (int i = 0; i < pos; i++) {
		pPre = pPre->next;
	}
	//插入
	SNode* pCur = pPre->next;
	data->next = pCur;
	pPre->next = data;

	list->size++;
	

}

void delete_List(List* list, int pos)
{
	if (list == NULL) {
		cout<<"链表为空!" << endl;
	}
	Snode* pre = &(list->head);
	for (int i = 0;i<pos;i++) {
		pre = pre->next;
	}
	pre->next = pre->next->next;
	list->size--;
}

Snode* findvalue_List(List* list, int pos)
{
	if (list == NULL) {
		cout<<"链表数据为空!" << endl;
	}
	Snode* node = &(list->head);
	for (int i = 0;i<pos+1;i++) {
		node = node->next;
	}
	return node;
}

int findpos_List(List* list, Snode* data,MYCOMPARE myc)
{
	int count = 0;
	Snode *node = list->head.next;
	while (node!=NULL) {
		if (myc(data, node) == 1) {
			return count;
		}
		count++;
		node = node->next;
	}
	return 0;
}

void print_List(List* list, PRINTLIST print)
{
	if (list == NULL) {
		cout << "链表数据为空或者不存在!" << endl;
	}
	Snode* node =list->head.next;
	while (node!=NULL) {
		print(node);
		node = node->next;
	}
}

void free_List(List* list)
{
	if (list == NULL) {
		cout<<"链表数据已为空!" << endl;
	}
	free(list);
	cout<<"链表数据已被摧毁!" << endl;
}

主函数测试文件

#include "Linklist.h"
typedef struct student {
	Snode* node;//定义挂钩地址 这里相当于在之前结点定义的data,之前用head内存地址存放,现在用用这个,所以此时的head就叫小挂钩
	char name[30];
	int score;
}stu;
void print(Snode * data) {//打印
	
	student* stu =(student *)data;
	cout<<"姓名:" << stu->name << " 分数:" << stu->score << endl;

}
int myc(Snode *s1,Snode *s2) {//对比
	student* t1 = (student*)s1;
	student* t2 = (student*)s2;
	if (strcmp(t1->name, t2->name) == 0 && t1->score == t2->score) {
		return 1;
	}
	else return 0;
}
int main() {
	stu s1;
	stu s2;
	stu s3;
	strcpy(s1.name,"zz");//数组字符串赋值方式
	strcpy(s2.name,"gg");
	strcpy(s3.name,"yy");
	s1.score = 88;
	s2.score = 90;
	s3.score = 80;
	List* list = init_List();
	//存放挂钩相应的地址
	insert_List(list,0,(Snode*)&s1);
	insert_List(list,0,(Snode*)&s2);
	insert_List(list,0,(Snode*)&s3);
	print_List(list,print);
	cout << "------------------" << endl;
	Snode *node=findvalue_List(list,1);
	print(node);
	cout << "------------------" << endl;
	int pos=findpos_List(list,(Snode*)&s2,myc);
	cout<<"pos:"<<pos << endl;
	delete_List(list,1);
	cout<<"------------------" << endl;
	print_List(list, print);
	free(list);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值