linear list linker implement

linear_slk.h

/*
*
* FILENAME:		linear_slk.h
* CREATED AT:		2012/May/15
* DECRITION:		delarantion of single link linear and data structure
* 
* MODULE:		any
* PLATFORM:		ubuntu-12.04/gcc-4.6.3
* AUTHOR:		incarnation-lee
* 
* ----------------------REVISION HISTROY------------------------
* No.	Date	Revised by	Function	Descrition
* 1	12/5/15	incar-lee			created
*
*/

#ifndef LINEAR_SLK_H
#define LINEAR_SLK_H

#include "udgtd.h"

typedef struct slk_lr{
	Sp_element elem;				/* storge address of data element */
	struct slk_lr *next;				/* pointer to the next element */
}S_slk_lr,*Sp_slk_lr;

STATUS linear_slk_demo(void) __attribute__((pure));

/* operation functions on data element */
static STATUS init_slnk(S_slk_lr**);				/* initilize the linear seq list */
static STATUS clear_slnk(S_slk_lr**);				/* clear all the element of linear list */
static STATUS destory_slnk(S_slk_lr**);				/* clear all the element of linear list */
static STATUS isEmpty_slnk(S_slk_lr*);				/* linear list is empty */
static size_t len_slnk(S_slk_lr*);				/* the number of linear list */
static STATUS add_slnk(S_slk_lr*,S_slk_lr*);			/* add a new element append the linear list */
static S_slk_lr* get_slnk(S_slk_lr*,const S_slk_lr*);		/* get the element of index in linear list */
static S_slk_lr* next_slnk(S_slk_lr*,const S_slk_lr*);		/* get the element of index in linear list */
static S_slk_lr* prior_slnk(S_slk_lr*,const S_slk_lr*);		/* get the element of index in linear list */
static STATUS insert_slnk(S_slk_lr*,S_slk_lr*,S_slk_lr*);	/* insert a data element */
static STATUS delete_slnk(S_slk_lr*,S_slk_lr*);			/* delete a data element */

#endif

linear_slk.c

/*
*
* FILENAME:		linear_slk.c/linear_slk.h 
* CREATED AT:		2012/May/15
* DECRITION:		implement the linear in the form of single linker
* 
* MODULE:		linead list
* PLATFORM:		ubuntu-12.04/gcc-4.6.3
* AUTHOR:		incarnation-lee
* 
* ----------------------REVISION HISTROY------------------------
* No.	Date	Revised by	Function			Descrition
* 1	12/5/15	incar-lee	demo,init,destory,isEmpty,	implement the feature
				clear,len and add
  2	12/5/16	incar-lee	get,next and prior		implement the feature
  3	12/5/19	incar-lee	insert, delete			implement the feature
*
*/

/* Standard C */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>

/* linux related */
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>

/* user defined */
#include "externals.h"
#include "udgtd.h"
#include "linear_slk.h"



STATUS linear_slk_demo(void)
{
	int result = 0;
	S_slk_lr* slk_head_demo = NULL;
	S_slk_lr* slk_sp;
	S_slk_lr slk_e_1,slk_e_2,slk_e_3,slk_e_4,slk_e_ins;


	/* initilize the single linker linear */
	result = init_slnk(&slk_head_demo);	
	if(__builtin_expect(!!(result),0))
		return INIT_FAIL;
	
	/* clear all the element of linear list */
	result = clear_slnk(&slk_head_demo);

	/* add a new element append the single linker linear list */
	slk_e_1.elem = (S_element*)gmalloc((void**)&slk_e_1.elem,sizeof(S_element));
	slk_e_1.next = NULL;
	slk_e_1.elem->value = 97;
	slk_e_1.elem->number = 1;
	slk_e_1.elem->name = 'a';
	result = add_slnk(slk_head_demo,&slk_e_1);

	
	slk_e_2.elem = (S_element*)gmalloc((void**)&slk_e_2.elem,sizeof(S_element));
	slk_e_2.next = NULL;
	slk_e_2.elem->value = 98;
	slk_e_2.elem->number = 2;
	slk_e_2.elem->name = 'b';
	result = add_slnk(slk_head_demo,&slk_e_2);
	
	slk_e_3.elem = (S_element*)gmalloc((void**)&slk_e_3.elem,sizeof(S_element));
	slk_e_3.next = NULL;
	slk_e_3.elem->value = 99;
	slk_e_3.elem->number = 3;
	slk_e_3.elem->name = 'c';
	result = add_slnk(slk_head_demo,&slk_e_3);

	/* get the element of specify element in linear list */
	slk_sp = get_slnk(slk_head_demo,&slk_e_2);	

	slk_e_4.elem = (S_element*)gmalloc((void**)&slk_e_4.elem,sizeof(S_element));
	slk_e_4.next = NULL;
	slk_e_4.elem->value = 100;
	slk_e_4.elem->number = 4;
	slk_e_4.elem->name = 'd';
	slk_sp = get_slnk(slk_head_demo,&slk_e_4);	
	assert(NULL==slk_sp);
	
	
	/* the next element */
	slk_sp = next_slnk(slk_head_demo,&slk_e_1);
	slk_sp = next_slnk(slk_head_demo,&slk_e_2);
	slk_sp = next_slnk(slk_head_demo,&slk_e_4);

	/* the prior element*/
	slk_sp = prior_slnk(slk_head_demo,&slk_e_1);
	slk_sp = prior_slnk(slk_head_demo,&slk_e_2);
	slk_sp = prior_slnk(slk_head_demo,&slk_e_4);

	/* insert a data element */
	slk_e_ins.elem = (S_element*)gmalloc((void**)&slk_e_ins.elem,sizeof(S_element));
	slk_e_ins.next = NULL;
	slk_e_ins.elem->value = 101;
	slk_e_ins.elem->number = 5;
	slk_e_ins.elem->name = 'e';
	result = insert_slnk(slk_head_demo,&slk_e_ins,&slk_e_1);

	/* delete a data element */
	result = delete_slnk(slk_head_demo,&slk_e_1);

	/* linear list is empty */
	result = isEmpty_slnk(slk_head_demo);

	/* the number of linear list */
	result = len_slnk(slk_head_demo);

	/* destory single linker linear */
	result = destory_slnk(&slk_head_demo);

	return result;
}


/**************************************
* operation functions on data element *
***************************************/


/*
* initilize the single linker linear
*/
static STATUS init_slnk(S_slk_lr** slk_lr_head)
{
	*slk_lr_head = (S_slk_lr*)gmalloc((void**)slk_lr_head,sizeof(S_slk_lr));
	(*slk_lr_head)->elem = NULL;
	(*slk_lr_head)->next = NULL;
	return 0;
}

/*
* destory single linker linear
*/
static STATUS destory_slnk(S_slk_lr** slk_lr_head)
{
	*slk_lr_head = NULL;
	return 0;		
}

/*
* clear all the element of linear list 
*/
static STATUS clear_slnk(S_slk_lr** slk_lr_head)
{
	S_slk_lr *sll_tmp,*slk_N;
	sll_tmp = (*slk_lr_head)->next;
	if(NULL==sll_tmp)
		return EMPTY;

	while(NULL!=sll_tmp)
	{
		free(sll_tmp->elem); 		/* free the memory */
		slk_N = sll_tmp->next;		/* store the next */
		sll_tmp->next = NULL;		/* put the current next to NULL */
		free(sll_tmp);
		sll_tmp = slk_N;		
	}
	(*slk_lr_head)->next = NULL;
	return 0;		
}

/* 
* linear list is empty 
*/
static STATUS isEmpty_slnk(S_slk_lr* slk_lr_head)
{
	if(NULL==slk_lr_head->next)
		return EMPTY;
	return UNEMPTY;
}

/*
* the number of linear list 
*/
static size_t len_slnk(S_slk_lr* slk_lr_head)
{
	size_t len = 0;
	assert(NULL!=slk_lr_head);

	if(NULL==slk_lr_head->next)
		return 0;
	slk_lr_head = slk_lr_head->next;
	while(NULL!=slk_lr_head)
	{
		len++;
		slk_lr_head = slk_lr_head->next;
	}
	return len;
}

/* 
* add a new element append the single linker linear list 
*/
static STATUS add_slnk(S_slk_lr* slk_lr_head,S_slk_lr* element)
{
	assert(NULL!=slk_lr_head);
	assert(NULL!=element);

	while(NULL!=slk_lr_head->next)			/* get the last element */
	{
		slk_lr_head = slk_lr_head->next;
	}		
	slk_lr_head->next = element;
	return 0;
	
}

/* 
* get the element of specify element in linear list 
*/
static S_slk_lr* get_slnk(S_slk_lr* slk_lr_head,const S_slk_lr* element)	
{
	assert(NULL!=slk_lr_head);	
	assert(NULL!=element);	
	
	slk_lr_head = slk_lr_head->next;		/* first element */
	while(NULL!=slk_lr_head)
	{
		if( slk_lr_head->elem->value == element->elem->value &&
			slk_lr_head->elem->number == element->elem->number &&
			slk_lr_head->elem->name == element->elem->name)
			return slk_lr_head;	
		slk_lr_head = slk_lr_head->next;
	}
	return NULL;
}

/* 
* the next element 
*/
static S_slk_lr* next_slnk(S_slk_lr* slk_lr_head,const S_slk_lr* element)
{
	S_slk_lr *slk_lr_tmp;
	assert(NULL!=slk_lr_head);	
	assert(NULL!=element);	

	slk_lr_tmp = get_slnk(slk_lr_head,element);

	if(NULL==slk_lr_tmp)
		return (S_slk_lr*)NA;
	return slk_lr_tmp->next;
}			

/* 
* the prior element
*/
static S_slk_lr* prior_slnk(S_slk_lr* slk_lr_head,const S_slk_lr* element)
{
	S_slk_lr *slk_prior;
	assert(NULL!=element);	
	assert(NULL!=slk_lr_head);	

	slk_prior = slk_lr_head;
	slk_lr_head = slk_lr_head->next;		/* first element */
	while(NULL!=slk_lr_head)
	{
		if( slk_lr_head->elem->value == element->elem->value &&
			slk_lr_head->elem->number == element->elem->number &&
			slk_lr_head->elem->name == element->elem->name)
			return slk_prior;	
		slk_prior = slk_lr_head;
		slk_lr_head = slk_lr_head->next;
	}
	return NULL;
}

/* 
* insert a data element 
*/
static STATUS insert_slnk(S_slk_lr* slk_lr_head,S_slk_lr* element,S_slk_lr* dest)
{
	S_slk_lr *slk_tmp,*slk_dest;
	assert(NULL!=element);
	assert(NULL!=slk_lr_head);

	slk_dest = get_slnk(slk_lr_head,dest);	/* location the destination, this why get return pointer */
	assert(NULL!=slk_dest);

	slk_tmp = slk_dest->next;
	slk_dest->next = element;
	element->next = slk_tmp;

	return 0;	
}

/* 
* delete a data element
*/
static STATUS delete_slnk(S_slk_lr* slk_lr_head,S_slk_lr* element)
{
	S_slk_lr *slk_pri;
	assert(NULL!=slk_lr_head);
	assert(NULL!=element);
	
	slk_pri = prior_slnk(slk_lr_head,element);
	slk_pri->next = element->next;
	return 0;
}

转载于:https://my.oschina.net/incarnation/blog/59156

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值