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;
}
?
?