C language data structure - sequence table and its basic operations

foreword

This article introduces the definition and common operations of the sequence table and uses C language code to implement it.

1. Definition of sequence table

The sequential table is a linear table stored in the form of an array in the computer memory. The sequential storage of the linear table refers to storing each element in the linear table in sequence with a group of storage units with continuous addresses, so that the linear table is adjacent in logical structure. The data elements are stored in adjacent physical storage units, that is, the logical adjacent relationship between data elements is reflected through the adjacent relationship of the physical storage of the data elements. A linear table with a sequential storage structure is usually called a sequential table.

2. Storage structure of sequence table

Structure diagram:
insert image description here

Code:


//增强程序可维护性
typedef int SQDataType;
//动态的
typedef struct SeqList
{
    
    
	SQDataType* a;
	int size; //有效数据的个数
	int capacity;//容量
}SL;

3. Common operations of sequence tables

3.1 Initialization


//初始化顺序表
void SeqListInit(SL* ps)
{
    
    
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

3.2 Detection capacity

//检查容量是否足够
void SeqListCheckCapacity(SL* ps)
{
    
    
	if (ps->capacity == ps->size)
	{
    
    
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SQDataType *temp = (SQDataType* )realloc(ps->a, newcapacity * sizeof(SQDataType));

		if (temp == NULL)
		{
    
    
			printf("realloc fail\n");
			exit(-1);
		}
		else
		{
    
    
			ps->a = temp;
			ps->capacity = newcapacity;
		}
	}
	

}

3.3 Insert operation

//顺序表的插入
void SeqListInsert(SL* ps, int pos, SQDataType x)
{
    
    
	assert(pos <= ps->size);
	//检查容量
	SeqListCheckCapacity(ps);
	//把pos这个位置的以后的数据往后移
	int end = ps->size - 1;
	while (pos <= end)
	{
    
    
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	//把这个值插入顺序表中
	ps->a[pos] = x;
	ps->size++;
}

3.3.1 Plug

//头部插入元素
void SeqListPushFront(SL* ps, SQDataType x)
{
    
    
	SeqListInsert(ps, 0, x);
}

3.3.2 Rear plug

//尾部插入元素
void SeqListPushBack(SL* ps, SQDataType x)
{
    
    
	SeqListInsert(ps, ps->size, x);
}

3.4 Delete operation

//顺序表的删除操作
void SeqListErase(SL* ps, int pos)
{
    
    
	assert(pos < ps->size);
	int ret = SeqListFind(ps, ps->a[pos]);
	if (ret == -1)
	{
    
    
		printf("你要删除的元素不存在!\n");
		return;
	}
	int start = pos + 1;
	//把要删除的元素后面的元素前移
	while (start < ps->size)
	{
    
    
		ps->a[start-1] = ps->a[start];
		start++;
	}
	ps->size--;
}

3.4.1 Header deletion

//头部删除元素
void SeqListPopFront(SL* ps)
{
    
    
	assert(ps->size > 0);    
	SeqListErase(ps, 0);
}

3.4.2 Tail deletion

//尾部删除元素
void SeqListPopBack(SL* ps)
{
    
    
	SeqListErase(ps,ps->size);
}

3.5 Search

//查找顺序表中的元素
int SeqListFind(SL* ps, SQDataType x)
{
    
    
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
    
    
		if (ps->a[i] == x)
		{
    
    
			return i;
		}
	}
	//如果顺序表中没有该元素就返回-1
	return -1;
}

3.6 print

//打印顺序表中的元素
void SeqListPrint(SL* ps)
{
    
    
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
    
    
		printf("%d ", ps->a[i]);
	}
}

3.7 Destruction

//销毁顺序表
void SeqListDestroy(SL* ps)
{
    
    
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}

Guess you like

Origin blog.csdn.net/weixin_63181097/article/details/129740253