04数据结构与算法—双向循环企业链表(小挂钩)

思路

这里的特点主要是链表的尾部指向不为NULL。如链表刚初始化时, list->head.next = &(list->head),指向的为头结点,之前的写法为:list->head.next =NULL。即正是这样才形成了链表双向循环的特点。

代码实现

头文件.h

#pragma once
#include <iostream>
using namespace std;
//小挂钩
typedef struct SNode {
	struct SNode* next;

}Snode;
typedef struct CircleList {
	Snode head;//起到挂钩作用 不分配内存
	int size;
}Clist;
typedef void(*PRINTCLIST)(Snode*);
typedef int(*MYCOMPARE)(Snode*,Snode*);
Clist* init_Clist();//初始化
void insert_Clist(Clist *list,int pos,Snode *data);//添加
void delete_Clist(Clist *list,Snode *data,MYCOMPARE myc);//按值删除
int findpos_Clist(Clist *list,Snode *data, MYCOMPARE myc);//按值找下标
Snode* findvalue_Clist(Clist *list,int pos);//按下标查找值
void print_Clist(Clist *list,PRINTCLIST print);//打印
void free_Clist(Clist *list);//摧毁内存
Snode* top_Clist(Clist *list);//返回头结点数据

.cpp文件

#include "CIrcleList.h"

Clist* init_Clist()
{
    Clist* list = (Clist*)malloc(sizeof(Clist));
    list->size = 0;
    list->head.next = &(list->head);
    return list;
}

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

void delete_Clist(Clist* list, Snode* data, MYCOMPARE myc)
{
    if (list == NULL) {
        cout << "链表数据为空!" << endl;
    }
    Snode* pre = &(list->head);
    int pos = findpos_Clist(list,data,myc);
    for (int i = 0;i<pos;i++) {
        pre = pre->next;
    }
    Snode* pcur = pre->next;
    pre->next = pcur->next;
    list->size--;
}

int findpos_Clist(Clist* list, Snode* data, MYCOMPARE myc)
{
    if (list == NULL) {
        cout << "链表数据为空!" << endl;
    }
    Snode* node = list->head.next;
    for (int i = 0;i<list->size;i++) {
        if (myc(data,node)) {
            return i;
        }
        node = node->next;
    }
    return 0;
}

Snode* findvalue_Clist(Clist* list, int pos)
{
    Snode* node = list->head.next;
    for (int i = 0;i<pos;i++) {
        node = node->next;
    }
    return node;
}

void print_Clist(Clist* list, PRINTCLIST print)
{
    if (list == NULL) {
        cout << "链表数据为空!" << endl;
    }
    Snode* node = list->head.next;
    for (int i = 0;i<list->size;i++) {
        print(node);
        node = node->next;
    }

}

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

Snode* top_Clist(Clist* list)
{
    return list->head.next;
}

主函数测试文件

#include "CIrcleList.h"
typedef struct Student
{
	Snode* node;
	string name;
	int score;
}stu;
void print(Snode *data) {
	Student* stu = (Student*)data;
	cout<<"name:"<<stu->name<<" score:"<<stu->score<< endl;

}
int myc(Snode *s1,Snode *s2) {
	Student* t1 = (Student *) s1;
	Student* t2= (Student*)s2;
	if (t1->name==t2->name&&t1->score==t2->score) {
		return 1;
	}
	return 0;

}
int main() {
	stu s1;
	s1.name = "zzz";
	s1.score = 90;
	stu s2;
	s2.name = "ggg";
	s2.score = 85;
	stu s3;
	s3.name = "yyy";
	s3.score = 80;
	Clist* list = init_Clist();
	insert_Clist(list,0,(Snode*)&s1);
	insert_Clist(list,0,(Snode*)&s2);
	insert_Clist(list,0,(Snode*)&s3);
	print_Clist(list,print);
	cout<<"值为:" << endl;
	print(findvalue_Clist(list,1));
	int pos=findpos_Clist(list,(Snode*)&s2,myc);
	cout<<"的下标为:"<<pos << endl;
	cout<<"删除下标为1的结点后为:" << endl;
	delete_Clist(list,(Snode*)&s2,myc);
	print_Clist(list, print);
	cout << "头结点的数据为:";
	print(top_Clist(list));
	free_Clist(list);
	return 0;
}

运行测试

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值