顺序表完整得

#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 SeqListPrint(SL* ps);
void SeqListInit(SL* ps);
void SeqListDestory(SL* ps);
void SeqListCheckCapatcity(SL* ps);
void SeqListPushBack(SL* ps, SLDataType x);//尾插
void SeqListPopBack(SL*ps);
void SeqListPushFront(SL* ps, SLDataType x);
void SeqListPopFront(SL* ps);
//找到了返回1位置下标,没有找打返回-1;
int SeqListFind(SL* ps, SLDataType x);
//增加pos位置的数据
void SeqListInsert(SL*ps,int pos , SLDataType x);
//删除pos位置的数据
void SeqListErase(SL*ps,int pos );

#include"SeqList.h"
#include<assert.h>
void SeqListPrint(SL* ps) {
    for (int i = 0; i < ps->size; i++) {
        printf("%d ", ps->a[i]);
    }
    printf("\n");
}
void SeqListInit(SL* ps) 
{
    ps->a = NULL;
    ps->size = ps->capacity = 0;
}
void SeqListDestory(SL* ps) {
    free(ps->a);
    ps->a = NULL;
    ps->size = ps->capacity = 0;
}
void SeqListCheckCapatcity(SL* ps) {
    if (ps->size == ps->capacity) {
        int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
        SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
        if (NULL == tmp) {
            printf("failure\n");
            exit(-1);
        }
        ps->a = tmp;
        ps->capacity = newcapacity;
    }
}
void SeqListPushBack(SL* ps, SLDataType x) {
    //SeqListCheckCapatcity(ps);
    //ps->a[ps->size] = x;//有这两行代码才可以才会打印12345;
    //ps->size++;

    SeqListInsert(ps,ps->size,x);
}
void SeqListPopBack(SL* ps) {
    //assert(ps->size > 0);
    //ps->size--;
    SeqListErase(ps,ps->size-1);
}
void SeqListPushFront(SL* ps, SLDataType x) {
    
    //SeqListCheckCapatcity(ps);
    移动数据
    //int end = ps->size - 1;
    //while (end >= 0) {//移动的下标要大于0;不然会报错
    //    ps->a[end + 1] = ps->a[end]; 
    //    end--;
    //}
    //ps->a[0] = x;
    //ps->size++;
    SeqListInsert(ps, 0, x);
}
void SeqListPopFront(SL* ps) {
    //assert(ps->size >0);
    这个是顺序表的头删
        头删的话  需要把后序元素整体往前搬移  把0好位置的元素覆盖掉
    //int begin = 1;//这个begin意思就是下标是1的意思,可以覆盖第一个元素
    //while (begin < ps->size) {
    //    ps->a[begin - 1] = ps->a[begin]; //表示把后一个位置上的元素往前一个位置上搬移
    //    begin++;
    //}
    //ps->size--;
    SeqListErase(ps, 0);
}
int SeqListFind(SL* ps, SLDataType x) {
    for (int i = 0; i < ps->size; i++) {
        if (ps->a[i] == x) {
            return i;
        }
    }
    return -1;
}//插入指定下标pos,和pos->size,
void SeqListInsert(SL* ps, int pos, SLDataType x) {
    assert(pos>= 0 && pos <= ps->size);
    SeqListCheckCapatcity(ps);
    int end = ps->size - 1;
    //移动数据
    //while (end >= pos) {//pos插入的数字在前面,end 在后面,所以要大于等于
    //    ps->a[end + 1] = ps->a[end];
    //    end--;
    //}
    while (pos<=end) {//pos插入的数字在前面,end 在后面,所以要大于等于
        ps->a[end + 1] = ps->a[end];
        end--;
    }
    ps->a[pos] = x;
    ps->size++;
}//删除pos位置的数据
void SeqListErase(SL* ps, int pos) {
    assert(pos>= 0 && pos < ps->size);
    int begin = pos + 1;
    while (begin < ps->size) {
        ps->a[begin - 1] = ps->a[begin];
        begin++;
    }
    ps->size--;
}

#include"SeqList.h"
void TestSeqList1() {
    SL sl;
    SeqListInit(&sl);

    SeqListPushBack(&sl, 1);
    SeqListPushBack(&sl, 2);
    SeqListPushBack(&sl, 3);
    SeqListPushBack(&sl, 4);
    SeqListPushBack(&sl, 5);
    SeqListPrint(&sl);

    SeqListPushFront(&sl, 10);
    SeqListPushFront(&sl, 20);
    SeqListPushFront(&sl, 30);
    SeqListPushFront(&sl, 40);
    SeqListPrint(&sl);

    SeqListPopFront(&sl);
    SeqListPopFront(&sl);
    SeqListPopFront(&sl);
    
    SeqListPrint(&sl);

    SeqListDestory(&sl);
}
TestSeqList() {
    SL sl;
    SeqListInit(&sl);

    SeqListPushBack(&sl, 1);
    SeqListPushBack(&sl, 2);
    SeqListPushBack(&sl, 3);
    SeqListPushBack(&sl, 4);
    SeqListPushBack(&sl, 5);
    SeqListPrint(&sl);

    SeqListInsert(&sl, 2, 30);
    SeqListPrint(&sl);

    
    SeqListDestory(&sl);
}
TestSeqList2() {
    SL sl;
    SeqListInit(&sl);

    SeqListPushBack(&sl, 1);
    SeqListPushBack(&sl, 2);
    SeqListPushBack(&sl, 3);
    SeqListPushBack(&sl, 4);
    SeqListPushBack(&sl, 5);
    SeqListPrint(&sl);

    SeqListPushFront(&sl, 1);
    SeqListPushFront(&sl, 2);
    SeqListPushFront(&sl, 3);
    SeqListPushFront(&sl, 4);
    
    SeqListPrint(&sl);

    SeqListErase(&sl,2);
    SeqListPrint(&sl);

    SeqListPopBack(&sl);
    SeqListPopBack(&sl);


    SeqListPopFront(&sl);
    SeqListPopFront(&sl);

    SeqListPrint(&sl);
}
int main() {
    //TestSeqList1();
    //TestSeqList();
    TestSeqList2();
    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值