Melon库运用——栈篇

头文件片段

// mln_stack.h

typedef struct mln_stack_node_s 
{
    void                    *data;
    struct mln_stack_node_s *prev;
    struct mln_stack_node_s *next;
} mln_stack_node_t;

typedef struct 
{
    mln_stack_node_t        *bottom;
    mln_stack_node_t        *top;
    mln_stack_node_t        *free_head;
    mln_stack_node_t        *free_tail;
    mln_uauto_t              nr_node; // 节点个数
    stack_free               free_handler;
    stack_copy               copy_handler;
} mln_stack_t;

struct mln_stack_attr 
{
    stack_copy               copy_handler; // 栈节点数据复制函数指针
    stack_free               free_handler; // 栈节点数据释放函数指针
};

代码段

#include
#include"mln_stack.h"

#include"mln_stack.c"

struct score
{
	int math;
	int physics;
};

struct additional_score
{
	int add_math;
	int add_physics;
};


// 根据需要编写一个复制函数myCopy()
// 函数需要赋给attr.copy_handler,最终在mln_stack_dup()函数中被使用
// 复制函数不一定只是实现复制功能,可以根据需要实现其它功能
// 例如,此处函数myCopy()实现原始分与附加分求和
// 注意!此处函数myCopy()会使得原始栈中的分数被修改为原始分与附加分之和
void * myCopy(void * p1, void * p2)
{
	struct score * p3 = (struct score *) p1;
	struct additional_score * p4 = (struct additional_score *) p2;
	
	int add_math = p4 -> add_math;
	int add_physics = p4 -> add_physics;
	p3 -> math += add_math;
	p3 -> physics += add_physics;
	
	return p3;
}

// 根据需要编写一个遍历函数myTraverse()
// 函数需要作为参数传递给mln_stack_iterate()函数
int myTraverse(void * p1, void * p2)
{
	struct score * p3 = (struct score *) p1;
	
	printf("数学:%d\n", p3 -> math);
	printf("物理:%d\n\n", p3 -> physics);
	
	return 0;
}

int main()
{
	struct score * p;
	struct score s1, s2;
	struct mln_stack_attr attr;
	mln_stack_t * st_ptr;
	
	
	attr.copy_handler = myCopy;
	attr.free_handler = NULL;
	
	st_ptr = mln_stack_init(&attr);
	// 按照初始化属性attr初始化栈结构
	
	
	s1.math = 60;
	s1.physics = 60;
	s2.math = 70;
	s2.physics = 70;

	mln_stack_push(st_ptr, &s1);
	mln_stack_push(st_ptr, &s2);
	// 向栈中压入两组数据 
	
	
	printf("++++++++++++++++++++++++++++++++++++++++栈的非空判断++++++++++++++++++++++++++++++++++++++++\n\n");
	
	if(mln_stack_empty(st_ptr))  printf("栈为空!\n");
	else                         printf("栈非空!\n");
	printf("数据组数为%d\n\n", st_ptr -> nr_node);
	
	printf("++++++++++++++++++++++++++++++++++++++++初始状态下++++++++++++++++++++++++++++++++++++++++\n\n");
	
	printf("初始状态下,栈顶数据如下:\n");
	p = (struct score *) mln_stack_top(st_ptr);
	printf("数学:%d\n", p->math);
	printf("物理:%d\n\n", p->physics);
	
    printf("初始状态下,栈的遍历详情如下:\n");
    mln_stack_iterate(st_ptr, myTraverse, NULL);
    
	printf("++++++++++++++++++++++++++++++++++++++++调用mln_stack_dup()函数后++++++++++++++++++++++++++++++++++++++++\n\n");


    mln_stack_t * new_st_ptr;
	struct additional_score add;
	
	add.add_math = 20;
	add.add_physics = 20;
	
	new_st_ptr = mln_stack_dup(st_ptr, &add);
	// 根据用户在函数myCopy()中定义的规则复制栈


	printf("调用mln_stack_dup()函数后,栈顶数据如下:\n");
	p = (struct score *) mln_stack_top(new_st_ptr);
	printf("数学:%d\n", p->math);
	printf("物理:%d\n\n", p->physics);
	
    printf("调用mln_stack_dup函数后,栈的遍历详情如下:\n");
    mln_stack_iterate(new_st_ptr, myTraverse, NULL);
    
    mln_stack_pop(new_st_ptr);
    // 弹出栈顶元素 
    
	printf("弹出一个栈顶元素后,栈顶数据如下:\n");
	p = (struct score *) mln_stack_top(new_st_ptr);
	printf("数学:%d\n", p->math);
	printf("物理:%d\n\n", p->physics);
	
	printf("++++++++++++++++++++++++++++++++++++++++测试结束++++++++++++++++++++++++++++++++++++++++\n\n");


	mln_stack_destroy(st_ptr);
	mln_stack_destroy(new_st_ptr);
	// 销毁栈结构


	return 0;
}

Melon库运用——栈篇_第1张图片

存疑

attr.free_handler的使用 

你可能感兴趣的:(C语言,c语言,melon)