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;
}