Basic operation code realization of sequence table

Sequence table: The sequence table is a linear table stored in the form of an array in the computer memory. It refers to a linear structure in which a set of memory cells with consecutive addresses store data elements in sequence. That is, the linear table is stored in a sequential storage mode and it is called a sequential table.

Directly on the code below:


 //SeqList.h

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

typedef int SLdatatype;

typedef struct SeqList
{
    
    
	SLdatatype *a;//存储数据空间的指针
	int capacity;//容量空间大小
	int size;//有效数据个数
}SeqList;

void SeqListInit(SeqList *psl);//初始化
void SeqListDestroy(SeqList *psl);//删除
void SeqListPrint(SeqList *psl);//输出
void SeqListPushBack(SeqList *psl, SLdatatype x);//尾插
void SeqListPushFront(SeqList *psl, SLdatatype x);//头插
void SeqListPopBack(SeqList *psl);//尾删
void SeqListPopFront(SeqList *psl);//头删
void SeqListInsert(SeqList *psl, int pos, SLdatatype x);//按位置插入元素
void SeListErase(SeqList *psl, int pos);//按位置删除元素

 //SeqList.c

#include"SeqList.h"

void SeqListInit(SeqList *psl)
{
    
    
	psl->a = (SLdatatype*)malloc(sizeof(SLdatatype)*4);
	if (psl->a == 0){
    
    
		printf("malloc fail\n");
		exit(-1);
	}
	memset(psl->a, 0, sizeof(SLdatatype) * 4);
	psl->size = 0;
	psl->capacity = 4;
}

void SeqListDestroy(SeqList *psl)
{
    
    
	free(psl->a);
	psl->a = NULL;
	psl->capacity = psl->size = 0;
}

void SeqListPrint(SeqList *psl)
{
    
    
	for (int i = 0; i < psl->size; i++){
    
    
		printf("%d ", psl->a[i]);
	}
	printf("\n");
}

void CheckCapacity(SeqList* psl)
{
    
    
	if (psl->size == psl->capacity){
    
    
		SLdatatype *b = (SLdatatype*)realloc(psl->a, sizeof(SLdatatype)*psl->capacity * 2);
		if(b == NULL){
    
    
			printf("malloc fail\n");
			exit(-1);
		}
		psl->a = b;
		psl->capacity = psl->capacity * 2;
	}
}

void SeqListPushBack(SeqList *psl, SLdatatype x)
{
    
    
	    assert(psl);
		CheckCapacity(psl);
        psl->a[psl->size] = x;
	    psl->size++;

}

void SeqListPushFront(SeqList *psl, SLdatatype x)
{
    
    
	assert(psl);
	CheckCapacity(psl);
	for (int i = psl->size; i >0; i--){
    
    
		psl->a[i] = psl->a[i - 1];
	}
	psl->a[0] = x;
	psl->size++;
}

void SeqListPopBack(SeqList *psl)
{
    
    
	assert(psl);
	assert(psl->size > 0);
	psl->size--;
}

void SeqListPopFront(SeqList *psl)
{
    
    
	assert(psl);
	assert(psl->size > 0);
	for (int i = 0; i < psl->size; i++){
    
    
		psl->a[i] = psl->a[i + 1];
	}
	psl->size--;
}

void SeqListInsert(SeqList *psl, int pos, SLdatatype x)
{
    
    
	assert(psl);
	assert(pos >= 0 && pos <= psl->size);
	CheckCapacity(psl);
	int i;
	for (i = psl->size; i>=pos; i--){
    
    
		psl->a[i] = psl->a[i - 1];
	}
	psl->a[pos] = x;
	psl->size++;
}

void SeListErase(SeqList *psl, int pos)
{
    
    
	assert(psl);
	assert(pos >= 0 && pos <= psl->size);
	for (int i = pos+1; i < psl->size; i++){
    
    
		psl->a[i - 1] = psl->a[i];
	}
	psl->size--;
}

 //test.c

#include"SeqList.h"

int main()
{
    
    
	SeqList s1;
	SeqListInit(&s1);
	SeqListPushBack(&s1, 1);
	SeqListPushBack(&s1, 2);
	SeqListPushBack(&s1, 3);
	SeqListPushBack(&s1, 4);
	SeqListPushBack(&s1, 5);
	SeqListPrint(&s1);
	SeqListPushFront(&s1, 0);
	SeqListPrint(&s1);
	SeqListPopBack(&s1);
	SeqListPrint(&s1);
	SeqListPopFront(&s1);
	SeqListPrint(&s1);
	SeqListInsert(&s1,2,10);
	SeqListPrint(&s1);
	SeListErase(&s1,2);
	SeqListPrint(&s1);
}

Guess you like

Origin blog.csdn.net/m0_52771278/article/details/113702796