线性表的顺序存储结构(Sequential Mapping)

#include <stdio.h>
#include <stdlib.h>

/*-------------Data Object Structure--------------*/
#define ElemType int
#define LIST_INIT_LENGTH 20/*Initial length of SqList*/
#define LIST_INCREMENT_LENGTH 5/*Step wight*/
typedef struct{//Sequential mapping
	ElemType *elem;//Point to data base address
	int length;//Length of list
	int size;//The number of elements in this list
}SqList;

#define true 1
#define false 0
#define null -65535

/*------------Operation Set--------------*/
int init_list_with_length(SqList *list,int length);//Create list with specific length
int init_list(SqList *list);//Create list with default length
int append(SqList *list,ElemType value);//Append an element after tail of the list
int insert(SqList *list,int position,ElemType value);//Insert an element at the specific postion,first position is 0
int remove_all(SqList *list);//Remove all elements in the list
int remove_at(SqList *list,int position);//Remove the element specified
int remove_elem_first(SqList *list,ElemType value);//Remove first element with specific value
int remove_elem_last(SqList *list,ElemType value);//Remove last element with specific value
int remove_elem_all(SqList *list,ElemType value);//Remove all elements equal value
void reset(SqList *list);//Reset list
int replace_elem_first(SqList *list,ElemType old_value,ElemType new_value);//Replace first element with specific value
int replace_elem_last(SqList *list,ElemType old_value,ElemType new_value);//Replace last element with specific value
int replace_elem_all(SqList *list,ElemType old_value,ElemType new_value);//Replace all elements equal to value
ElemType replace_elem_at(SqList *list,int position,ElemType value);//Replace element at specific position
SqList sub_list(SqList *list,int from,int to);//Get SubList bound by [from,to]
int index_of(const SqList *list,ElemType value);//Get index of specific element find at first time
int last_index_of(const SqList *list,ElemType value);//Get index of specific element find at last time
int value(const SqList *list,int position);//Get the value at position
int contains(const SqList *list,ElemType value);//Find element
void reverse(SqList *list);//Reverse list
int size(const SqList * list);//Items of list
int is_empty(const SqList *list);//Wether the list is empty
void print_list(const SqList * list);//Print list inline
void println_list(const SqList * list);//Print list and return
void trim_to_size(SqList *list);//Trim list

/*
* Function: Create list with specific length
* Parameters: list: Target list
*			  length: size of list
* Return: true/false
*/
int init_list_with_length(SqList *list,int length)
{
    list->elem=(ElemType*)malloc(length*sizeof(ElemType));
    if(!list->elem)
		return false;
    list->length=length;        
    list->size=0;       
	
    return true;
}

/*
* Function: Create list with default length
* Parameters: list: Target list
* Return: 
*/
int init_list(SqList *list)
{
    if(!init_list_with_length(list,LIST_INIT_LENGTH))
		return false;
	
    return true;      
}

/*
* Function: Append an element after tail of the list
* Parameters: list: Target list
*			  value: Appending value
* Return: true/false
*/
int append(SqList *list,ElemType value)
{
	if(list->size>=list->length)//Add memory
	{
		ElemType *e = (ElemType*)realloc(list->elem,(list->size+LIST_INCREMENT_LENGTH)*sizeof(ElemType));
		if(!e)
			return false;
		list->elem=e;
		list->length+=LIST_INCREMENT_LENGTH;
	}
	list->elem[list->size++]=value;
	return true;
}

/*
* Function: Insert an element at the specific postion,first position is 0
* Parameters: list: Target list
*			  position: Insert position
*			  value: Inserted value
* Return: true/false
*/
int insert(SqList *list,int position,ElemType value)
{
	int cur;//cursor for position in list

	if(position<0 || position>list->size)//Out of bound
		return false;
	
	if(list->size>=list->length)//Add memory
	{
		ElemType *e = (ElemType*)realloc(list->elem,(list->size+LIST_INCREMENT_LENGTH)*sizeof(ElemType));
		if(!e)
			return false;
		list->elem=e;
		list->length+=LIST_INCREMENT_LENGTH;
	}

	for(cur=list->size-1;cur>=position;--cur)//Move elements after position(including position)
	{
		list->elem[cur+1]=list->elem[cur];
	}
	list->elem[position]=value;
	list->size++;

	return true;
}

/*
* Function: 
* Parameters: list: Target list
*			  value: 
* Return: 
*/
int remove_all(SqList *list)
{
	if(list->size>0)
	{
		//Just set the size=0,original element can also be visited
		list->size=0;
	}
	return true;
}

/*
* Function: 
* Parameters: list: Target list
*			  value: 
* Return: 
*/
int remove_at(SqList *list,int position)
{
	int cur;//cursor of position in list

	if(position<0 || position>=list->size)//Out of bound
		return false;

	for(cur=position;cur<list->size-1;cur++)
	{
		list->elem[cur]=list->elem[cur+1];
	}
	list->size--;

	return true;
}

/*
* Function: Remove first element with specific value
* Parameters: list: Target list
*			  value: Element value will be deleted
* Return: Index of element deleted,not found return -1
*/
int remove_elem_first(SqList *list,ElemType value)
{
	int cur=0;
	int index=-1;

	for(;cur<list->size;)
	{
		if(list->elem[cur]==value)//Matched
		{
			index = cur;
			for(;cur<list->size-1;)//Delete
			{				
				list->elem[cur]=list->elem[cur+1];
				cur++;
			}
			list->size--;
		}
		cur++;
	}
	return index;
}

/*
* Function: Remove last element with specific value
* Parameters: list: Target list
*			  value: Element value will be deleted
* Return: Index of element deleted,not found return -1
*/
int remove_elem_last(SqList *list,ElemType value)
{
	int cur=list->size-1;
	int index=-1;

	for(;cur>0;)
	{
		if(list->elem[cur]==value)//Matched
		{
			index = cur;
			for(;cur<list->size-1;)//Delete
			{				
				list->elem[cur]=list->elem[cur+1];
				cur++;
			}
			list->size--;
			return index;
		}
		cur--;
	}
	return index;
}

/*
* Function: Remove all elements with specific value
* Parameters: list: Target list
*			  value: Element value will be deleted
* Return: Counts of elements deleted
*/
int remove_elem_all(SqList *list,ElemType value)
{
	int cur=0;
	int count=0;

	for(;cur<list->size;)
	{
		if(list->elem[cur]==value)//Matched
		{
			int i=cur;
			count++;
			for(;i<list->size-1;)//Delete
			{				
				list->elem[i]=list->elem[i+1];
				i++;
			}
			list->size--;
			continue;
		}
		cur++;
	}
	return count;
}

/*
* Function: Replace first element with specific value
* Parameters: list: Target list
*			  old_value: Element value will be replaced
*			  new_value: Element value will replace old_value
* Return: Index of element replaced,not found return -1
*/
int replace_elem_first(SqList *list,ElemType old_value,ElemType new_value)
{
	int cur=0;

	for(;cur<list->size;)
	{
		if(list->elem[cur]==old_value)//Matched
		{
			list->elem[cur]=new_value;
			return cur;
		}
		cur++;
	}
	return -1;
}

/*
* Function: Replace last element with specific value
* Parameters: list: Target list
*			  old_value: Element value will be replaced
*			  new_value: Element value will replace old_value
* Return: Index of element replaced,not found return -1
*/
int replace_elem_last(SqList *list,ElemType old_value,ElemType new_value)
{
	int cur=list->size;

	for(;cur>0;)
	{
		if(list->elem[cur]==old_value)//Matched
		{
			list->elem[cur]=new_value;
			return cur;
		}
		cur--;
	}
	return -1;
}

/*
* Function: Replace all elements with specific value
* Parameters: list: Target list
*			  value: Element value will be replaced
*			  new_value: Element value will replace old_value
* Return: Counts of elements replaced
*/
int replace_elem_all(SqList *list,ElemType old_value,ElemType new_value)
{
	int cur=0;
	int count=0;

	for(;cur<list->size;)
	{
		if(list->elem[cur]==old_value)//Matched
		{
			list->elem[cur]=new_value;
			count++;
		}
		cur++;
	}
	return count;
}

/*
* Function: Replace first element with specific position
* Parameters: list: Target list
*			  value: Element value will be setted
* Return: Original value,not found return null(-65535)
*/
ElemType replace_elem_at(SqList *list,int position,ElemType value)
{
	int original_value;

	if(position<0 || position>=list->size)//Out of bound
		return null;

	original_value=list->elem[position];
	list->elem[position]=value;

	return original_value;
}

/*
* Function: Get SubList bound by [from,to]
* Parameters: list: Target list
*			  from: Start index
*			  to: End index 
* Return: Sublist
*/
SqList sub_list(SqList *list,int from,int to)
{
	SqList sub_list;

	if(from<0 || from>=list->size || to<0 || to>=list->size || from>to)//Out of bound
		return sub_list;//Return null object,replace this with certain type value when using this function

	init_list(&sub_list);

	for(;from<=to;)
	{
		append(&sub_list,list->elem[from++]);
	}

	return sub_list;
}

/*
* Function: Get index of specific element first matched
* Parameters: list: Target list
*			  value: Element value
* Return: index
*/
int index_of(const SqList *list,ElemType value)
{
	int cur=0;

	for(;cur<list->size;)
	{
		if(list->elem[cur]==value)//Matched
		{
			return cur;
		}
		cur++;
	}
	return -1;
}

/*
* Function: Get index of specific element last matched
* Parameters: list: Target list
*			  value: Element value used to search
* Return: index
*/
int last_index_of(const SqList *list,ElemType value)
{
	int cur=list->size;

	for(;cur>0;)
	{
		if(list->elem[cur]==value)//Matched
		{
			return cur;
		}
		cur--;
	}
	return -1;
}

/*
* Function: Get value at specific position of list
* Parameters: list: Target list
*			  position: position in list
* Return: Element value
*/
int value(const SqList *list,int position)
{
	if(position<0 || position>=list->size)//Out of bound
		return null;

	return list->elem[position];
}

/*
* Function: Judge wether the list has element(s) with value
* Parameters: list: Target list
*			  value: Element value used to match
* Return: Count of matched element
*/
int contains(const SqList *list,ElemType value)
{
	int cur=0;
	int count=0;

	for(;cur<list->size;)
	{
		if(list->elem[cur]==value)//Find
		{
			count++;
		}
		cur++;
	}
	return count;
}

/*
* Function: Reverse the list
* Parameters: list: Target list
* Return: void
*/
void reverse(SqList *list)
{
	int start=0;
	int end=list->size-1;

	for(;start<end;)
	{
		list->elem[start]^=list->elem[end];
		list->elem[end]^=list->elem[start];
		list->elem[start]^=list->elem[end];
		start++;
		end--;
	}
}

/*
* Function: Reset the list
* Parameters: list: Target list
* Return: void
*/
void reset(SqList *list)
{
	init_list(list);
}

/*
* Function: Get size of the list
* Parameters: list: Target list
* Return: counts of items in this list
*/
int size(const SqList * list)
{
    return list->size;
}

/*
* Function: Judge wether the list is empty
* Parameters: list: Target list
* Return: true/false
*/
int is_empty(const SqList *list)
{
	return list->size==0?true:false;
}

/*
* Function: Print the list elements in-line
* Parameters: list: Target list
* Return: void
*/
void print_list(const SqList *list)
{
	if(0==list->size)
		printf("null");
    else
    {
		int i;
		for(i=0;i<list->size;i++)
		{
			if(0==i)
				printf("%d",list->elem[i]);
			else
				printf(" %d",list->elem[i]);
		}
    }
}

/*
* Function: Print the list elements and return a new line
* Parameters: list: Target list 
* Return: void
*/
void println_list(const SqList *list)
{
	if(0==list->size)
		printf("null\n");
    else
    {
		int i;
		for(i=0;i<list->size;i++)
		{
			if(0==i)
				printf("%d",list->elem[i]);
			else
				printf(" %d",list->elem[i]);
		}
		printf("\n");
    }
}

/*
* Function: Resize the list to its actual size
* Parameters: list: Target list
* Return: void
*/
void trim_to_size(SqList *list)
{
	list->length=list->size;
	list->elem=(ElemType*)realloc(list->elem,list->size*sizeof(ElemType));
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值