# Sequence_table代码展示

> 来源：梦飞idc云平台 | 原文：https://www.mfisp.com/22563.html | 发布：2023-07-31 | 更新：2023-07-31 | 分类：建站教程

## Sequence_table完整代码展示

 

#pragma once  
#include<stdio.h>  
#include<stdlib.h>  
#include<assert.h>

 

// 动态顺序表  
typedef int SLDataType;

 

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

 

// 管理数据 -- 增删查改  
void SLInit(SL* ps);  
void SLDestroy(SL* ps);  
void SLPrint(SL* ps);  
void SLCheckCapacity(SL* ps);

 

// 头插头删 尾插尾删  
void SLPushBack(SL* ps, SLDataType x);  
void SLPopBack(SL* ps);  
void SLPushFront(SL* ps, SLDataType x);  
void SLPopFront(SL* ps);

 

// 在pos位置插入x  
void SLInsert(SL* ps, int pos, SLDataType x);  
// 删除pos位置的值  
void SLErase(SL* ps, int pos);

 

## Sequence_table.c

 

#include"Sequence_table.h"

 

//初始化顺序表  
void SLInit(SL* ps)  
{  
 ps->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);  
 if (ps->a == NULL)  
 {  
 perror("malloc failed");  
 exit(-1);  
 //return;  
 }

 

ps->size = 0;  
 ps->capacity = 4;  
}  
//销毁顺序表  
void SLDestroy(SL* ps)  
{  
 free(ps->a);  
 ps->a = NULL;  
 ps->capacity = ps->size = 0;  
}  
//打印顺序表  
void SLPrint(SL* ps)  
{  
 for (int i = 0; i < ps->size; i++)  
 {  
 printf("%d ", ps->a[i]);  
 }  
 printf("\n");  
}  
//扩容顺序表  
void SLCheckCapacity(SL* ps)  
{  
 if (ps->size == ps->capacity)  
 {  
 SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDataType)));  
 if (tmp == NULL)  
 {  
 perror("realloc failed");  
 exit(-1);  
 }

 

ps->a = tmp;  
 ps->capacity *= 2;  
 }  
}

 

//尾插  
void SLPushBack(SL* ps, SLDataType x)  
{  
 SLCheckCapacity(ps);  
 ps->a[ps->size] = x;  
 ps->size++;  
}  
//尾删  
void SLPopBack(SL* ps)  
{  
 assert(ps->size > 0);  
 ps->size--;  
}

 

//头插  
void SLPushFront(SL* ps, SLDataType x)  
{  
 SLCheckCapacity(ps);

 

// 挪动数据  
 int end = ps->size - 1;  
 while (end >= 0)  
 {  
 ps->a[end + 1] = ps->a[end];  
 --end;  
 }  
 ps->a[0] = x;  
 ps->size++;  
}  
//头删  
void SLPopFront(SL* ps)  
{  
 assert(ps->size > 0);  
 for (int i = 1; i <= ps->size - 1; i++) {  
 ps->a[i - 1] = ps->a[i];  
 }  
 ps->size--;  
}

 

// 在pos位置插入x  
void SLInsert(SL* ps, int pos, SLDataType x) {  
 SLCheckCapacity(ps);

 

int end = ps->size - 1;  
 for (int i = end; i >= pos-1; i--) {  
 ps->a[i + 1] = ps->a[i];  
 }  
 ps->a[pos-1] = x;  
 ps->size++;  
}

 

// 删除pos位置的值  
void SLErase(SL* ps, int pos) {  
 assert(ps->size > 0);  
 for (int i = pos-1; i < ps->size - 1; i++) {  
 ps->a[i] = ps->a[i+1];  
 }  
 ps->size--;  
}

 

## Sequence_table_test.c

 

#include"Sequence_table.h"

 

void TestSeqList1()  
{  
 SL sl;  
 SLInit(&sl);  
 SLPushBack(&sl, 1);  
 SLPushBack(&sl, 2);  
 SLPopBack(&sl);  
 SLPrint(&sl);  
 SLInsert(&sl, 5, 0);  
 SLPrint(&sl);  
 SLErase(&sl, 3);  
 SLPrint(&sl);  
 SLDestroy(&sl);  
}

 

void TestSeqList2()  
{  
 SL sl;  
 SLInit(&sl);  
 SLPushFront(&sl, 10);  
 SLPushFront(&sl, 20);  
 SLPopFront(&sl);  
 SLPopFront(&sl);  
 SLInsert(&sl, 3, 0);  
 SLPrint(&sl);  
 SLInsert(&sl, 5, 0);  
 SLPrint(&sl);  
 SLErase(&sl, 3);  
 SLPrint(&sl);  
 SLDestroy(&sl);  
}

 

int main()  
{  
 TestSeqList1();  
 TestSeqList2();  
 return 0;  
}

---
本页由 梦飞idc云平台（https://www.mfisp.com）提供。站点导航见 https://www.mfisp.com/llms.txt
