linear list sequence implement

本文详细介绍了一种基于数组实现的线性序列数据结构。包括初始化、销毁、清空、判断空闲、获取长度、添加元素、获取指定索引元素、获取前后元素、插入和删除元素等操作的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

udgtd.h for some global predefinition and data struct.

/*
*
* FILENAME:		udgtd.h
* CREATED AT:		2012/May/5
* DECRITION:		user defined global type defines, include enum, const and marco
* 
* MODULE:		any
* PLATFORM:		ubuntu-12.04/gcc-4.6.3
* AUTHOR:		incarnation-lee
* 
* ----------------------REVISION HISTROY------------------------
* No.	Date	Revised by	Function	Descrition
* 1	12/5/7	incar-lee	NA		created
*
*/

#ifndef UDGTD_H
#define UDGTD_H

typedef signed int STATUS;
#define UMALLCO	1
#define EMPTY 0
#define OVERFLOW 0xCC
#define INVALID_INDEX -1
#define UNEMPTY 0xFFFF
#define INIT_FAIL 20
#define DESTORY_FAIL 21
#define CLEAR_FAIL 22

#define ALGORITHM_FAIL 3
#define LINEAR_NOT_EXIT 4

typedef struct element{
	int value;
	short number;
	char name;
}S_element,*Sp_element;


#endif

linear_seq.h for function declaration and data structure operator.

/*
*
* FILENAME:		linear_seq.h
* CREATED AT:		2012/May/5
* DECRITION:		delarantion of linear sequnence 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/7	incar-lee			created
*
*/

#ifndef LINEAR_SEQ_H
#define LINEAR_SEQ_H

#include "udgtd.h"

#define LINEAR_SEQ_SIZE 100

typedef struct sqlinear{
	Sp_element elem;	/* storge address of data element */
	int length;		/* the length of linear seq list */
	int rest;	/* the current rest size for storge */	
}S_sqlinear,*Sp_sqlinear;

STATUS linear_seq_demo(void) __attribute__((pure));

/* operation functions on data element */
static STATUS init_lseq(S_sqlinear*);			/* initilize the linear seq list */
static STATUS destory_lseq(S_sqlinear*);		/* destroy the linear */
static STATUS clear_lseq(S_sqlinear*);			/* clear all the element of linear list */
static STATUS isEmpty_lseq(S_sqlinear*);		/* linear list is empty */
static size_t len_lseq(S_sqlinear*);			/* the number of linear list */
static STATUS add_lseq(S_sqlinear*,S_element*);		/* add a new element append the linear list */
static S_element* get_lseq(S_sqlinear*,int);		/* get the element of index in linear list */
static S_element* next_lseq(S_sqlinear*,int);		/* the next element */
static S_element* prior_lseq(S_sqlinear*,int);		/* the prior element*/
static STATUS insert_lseq(S_sqlinear*,S_element*,int);	/* insert a data element */
static STATUS delete_lseq(S_sqlinear*,int);		/* delete a data element */

#endif

linear_seq.c for the definition of linear list and a demonstration of usage.

/*
*
* FILENAME:		linear_seq.c/linear_seq.h 
* CREATED AT:		2012/May/5
* DECRITION:		implement the linear in the sequence way
* 
* 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/7	incar-lee	linear_seq_demo			implement the sequence linear list
* 2	12/5/8	incar-lee	init_lseq & destory_lseq	implement the feature of function
* 3	12/5/9	incar-lee	isEmpty_lseq,len_lseq,		implement the feature
*				add_lseq & get_lseq	
* 4	12/5/10	incar-lee	next_lseq, prior_lseq,
				insert and 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 "gen_fm.h"
#include "linear_seq.h"


S_element *sq_ele = NULL;


STATUS linear_seq_demo(void)
{
	S_sqlinear lseq_demo;
	STATUS result = 0;
	S_element element = {
		.value = 97,
		.number = 1,
		.name = 'a',
	},*ele_linear,*ele_next,*ele_prior;

	/* initiliazie the linear sequence list */
	result = init_lseq(&lseq_demo);
	if(__builtin_expect(!!(result),0))
		return INIT_FAIL;	

	/* add 3 ASCII element in linear list */
	result = add_lseq(&lseq_demo,&element);
	if(__builtin_expect(!!(OVERFLOW==result),0))
		return OVERFLOW;
	
	element.value = 98;
	element.number = 2;
	element.name = 'b';
	result = add_lseq(&lseq_demo,&element);
	if(__builtin_expect(!!(OVERFLOW==result),0))
		return OVERFLOW;

	element.value = 99;
	element.number = 3;
	element.name = 'c';
	result = add_lseq(&lseq_demo,&element);
	if(__builtin_expect(!!(OVERFLOW==result),0))
		return INVALID_INDEX;

	/* get the address in linear list element */
	ele_linear = get_lseq(&lseq_demo,1);
	if(__builtin_expect(!!(INVALID_INDEX==(int)ele_linear),0))
		return OVERFLOW;

	/* get the next element of specify index */
	ele_next = next_lseq(&lseq_demo,1);	
	if(__builtin_expect(!!(INVALID_INDEX==(int)ele_next),0))
		return OVERFLOW;

	/* get the next element of specify index */
	ele_prior = prior_lseq(&lseq_demo,1);	
	if(__builtin_expect(!!(INVALID_INDEX==(int)ele_prior),0))
		return OVERFLOW;

	/* insert a new element */
	element.value = 100;
	element.number = 4;
	element.name = 'd';
	result = insert_lseq(&lseq_demo,&element,1);
	if(__builtin_expect(!!(INVALID_INDEX==result),0))
		return INVALID_INDEX;

	/* delete the insert element */
	result = delete_lseq(&lseq_demo,1);
	if(__builtin_expect(!!(INVALID_INDEX==result),0))
		return INVALID_INDEX;

	/* is the linear list empty */
	result = isEmpty_lseq(&lseq_demo);
	if(LINEAR_NOT_EXIT==result)
		return LINEAR_NOT_EXIT;
/* here inseart your operation code on if empty */
	
	/* is the linear list empty */
	result = len_lseq(&lseq_demo);
	if(LINEAR_NOT_EXIT==result)
		return LINEAR_NOT_EXIT;
/* here inseart your operation code on the length exit */

	/* clear the linear list, and reset the rest field */		
	result = clear_lseq(&lseq_demo);
	if(__builtin_expect(!!(result),0))
		return CLEAR_FAIL;

	/* destory the whole linear list */
	result = destory_lseq(&lseq_demo);
	if(__builtin_expect(!!(result),0))
		return DESTORY_FAIL;

	return 0;
}



/* operation functions on data element */
/*
* initilize the linear seq list 
*/
static STATUS init_lseq(S_sqlinear* lseq_p)
{
	assert(lseq_p!=NULL);

	sq_ele = (S_element*)gmalloc((void**)&sq_ele,sizeof(S_element)*LINEAR_SEQ_SIZE);
	memset(sq_ele,0,sizeof(S_element)*LINEAR_SEQ_SIZE);
	if(__builtin_expect(!!(NULL==sq_ele),0))
		return UMALLCO;
	lseq_p->elem = sq_ele;			/* init the operate structure */
	lseq_p->length = LINEAR_SEQ_SIZE;	/* the length of linear list sequenece implement */
	lseq_p->rest = LINEAR_SEQ_SIZE;		/* init the rest size of linear sequence list */
	return 0;
}	


/* 
* destroy the linear
*/
static STATUS destory_lseq(S_sqlinear* lseq_p)
{
	assert(lseq_p!=NULL);

	if(NULL!=lseq_p->elem)			/* sequence linear list exit */	
	{
		free(sq_ele);
		lseq_p->elem = NULL;
		lseq_p->length = 0;
		lseq_p->rest = 0;
		return 0;
	}
	return LINEAR_NOT_EXIT;
}


/*
* clear all the element of linear list
*/
static STATUS clear_lseq(S_sqlinear* lseq_p)
{
	assert(lseq_p!=NULL);

	if(NULL!=lseq_p->elem)			/* sequence linear list exit */	
	{
		memset(lseq_p->elem,0,sizeof(S_element)*lseq_p->length);
		lseq_p->rest = lseq_p->length;
		return 0;
	}
	return LINEAR_NOT_EXIT;
}


/* 
* linear list is empty 
*/
static STATUS isEmpty_lseq(S_sqlinear* lseq_p)
{
	assert(lseq_p!=NULL);

	if(NULL!=lseq_p->elem)			/* sequence linear list exit */	
		if(lseq_p->length==lseq_p->rest)
			return EMPTY;
		return UNEMPTY;
	return LINEAR_NOT_EXIT;
}


/*
* the length of element exit at linear list
*/
static size_t len_lseq(S_sqlinear* lseq_p)
{
	assert(NULL!=lseq_p);

	if(NULL!=lseq_p->elem)  		/* sequence linear list exit */	
		return lseq_p->length - lseq_p->rest;
	return LINEAR_NOT_EXIT;
}


/*
* append a new element at the end of linear list
*/	
static STATUS add_lseq(S_sqlinear* lseq_p,S_element* element)
{
	assert(NULL!=element);
	assert(NULL!=lseq_p);
		
	if(lseq_p->rest<=0)
		return OVERFLOW;
	memcpy(
		lseq_p->elem+len_lseq(lseq_p),
		element,
		sizeof(S_element)
	);
	lseq_p->rest--;
	return 0;
}

/*
* get the element of index in linear list
*/	
static S_element* get_lseq(S_sqlinear* lseq_p,int index)	
{
	assert(NULL!=lseq_p);

	if(NULL!=lseq_p->elem)			/* sequence linear list exit */	
		if(index>=0 && index<len_lseq(lseq_p))
			return lseq_p->elem+index;
		return (S_element*)INVALID_INDEX;
	return (S_element*)LINEAR_NOT_EXIT;
}

/*
* the next element 
*/
static S_element* next_lseq(S_sqlinear* lseq_p,int index)
{
	assert(NULL!=lseq_p);
	
	if(NULL!=lseq_p->elem)			/* sequence linear list exit */	
		if(index>=0 && index <len_lseq(lseq_p)-1)
			return lseq_p->elem+(++index);	/* here you can write like this lseq_p->elem+++index, but do not 
make any confused risk */
		return (S_element*)INVALID_INDEX;
	return (S_element*)LINEAR_NOT_EXIT;
}

/*
* the prior element 
*/
static S_element* prior_lseq(S_sqlinear* lseq_p,int index)
{
	assert(NULL!=lseq_p);
	
	if(NULL!=lseq_p->elem)			/* sequence linear list exit */	
		if(index>0 && index <len_lseq(lseq_p))
			return lseq_p->elem+--index;	
		return (S_element*)INVALID_INDEX;
	return (S_element*)LINEAR_NOT_EXIT;
}

/* 
* insert a data element 
*/
static STATUS insert_lseq(S_sqlinear* lseq_p,S_element* element,int index)
{
	S_element *elem_tmp = NULL;
	size_t len_tmp = 0;
	assert(NULL!=lseq_p);
	assert(NULL!=element);

	len_tmp = (len_lseq(lseq_p) - index)*sizeof(S_element);
	if(NULL!=lseq_p->elem)			/* linear list exit */
		if(index>=0 && index<len_lseq(lseq_p))
		{
			elem_tmp = (S_element*)gmalloc((void**)&elem_tmp,len_tmp);
			memcpy(elem_tmp,lseq_p->elem+index,len_tmp);		/* copy the element to temp buffer */
			memcpy(lseq_p->elem+index,element,sizeof(S_element));	/* insert the new element */
			memcpy(lseq_p->elem+(++index),elem_tmp,len_tmp);	/* copy the pre store element back to linear in new index*/
			lseq_p->rest--;
			free(elem_tmp);
			return 0;
		}		
		return INVALID_INDEX;
	return LINEAR_NOT_EXIT;	
}


/* 
* delete a data element 
*/
static STATUS delete_lseq(S_sqlinear* lseq_p,int index)
{
	S_element *elem_tmp = NULL;
	size_t len_tmp = 0;
	assert(NULL!=lseq_p);

	len_tmp = (len_lseq(lseq_p) - index - 1)*sizeof(S_element);
	if(NULL!=lseq_p->elem)			/* linear list exit */
		if(index>=0 && index<len_lseq(lseq_p))
		{
			elem_tmp = (S_element*)gmalloc((void**)&elem_tmp,len_tmp);
			memcpy(elem_tmp,lseq_p->elem+index+1,len_tmp);		/* copy the element after index to temp */
			memcpy(lseq_p->elem+index,elem_tmp,len_tmp);		/* shift left the element after index */
			lseq_p->rest++;						/* update the rest */
			memset(lseq_p->elem+len_lseq(lseq_p),0,sizeof(S_element));	/* reset the last null elemnt */
			free(elem_tmp);
			return 0;
		}		
		return INVALID_INDEX;
	return LINEAR_NOT_EXIT;	
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值