欧美一区2区三区4区公司二百,国产精品婷婷午夜在线观看,自拍偷拍亚洲精品,国产美女诱惑一区二区

Sequence_table代碼展示

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

?

?

文章鏈接: http://www.qzkangyuan.com/22563.html

文章標題:Sequence_table代碼展示

文章版權:夢飛科技所發布的內容,部分為原創文章,轉載請注明來源,網絡轉載文章如有侵權請聯系我們!

聲明:本站所有文章,如無特殊說明或標注,均為本站原創發布。任何個人或組織,在未征得本站同意時,禁止復制、盜用、采集、發布本站內容到任何網站、書籍等各類媒體平臺。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
建站教程

什么是Sequence_table.c功能函數

2023-7-28 14:49:24

建站教程

Hash表

2023-7-31 17:09:20

0 條回復 A文章作者 M管理員
    暫無討論,說說你的看法吧
?
個人中心
購物車
優惠劵
今日簽到
有新私信 私信列表
搜索
主站蜘蛛池模板: 望谟县| 商洛市| 宁陕县| 北京市| 沅陵县| 华安县| 肥西县| 云梦县| 彭阳县| 六枝特区| 永善县| 根河市| 迭部县| 卓尼县| 天水市| 淳化县| 肥城市| 正蓝旗| 江安县| 清流县| 蓬安县| 玉山县| 昌吉市| 阿鲁科尔沁旗| 云林县| 乐安县| 宜宾县| 饶平县| 体育| 介休市| 松溪县| 商都县| 南皮县| 文安县| 大荔县| 南京市| 大石桥市| 库伦旗| 平武县| 汝州市| 泽州县|