【数据结构】【顺序表】【纯C代码实现】

my_ArrayListTable.h

//
// Created by 刘杰 on 2022/7/26.
//

#ifndef LINUXC_MY_ARRAYLISTTABLE_H
#define LINUXC_MY_ARRAYLISTTABLE_H
#include <assert.h>
#include <stdbool.h>
#include "stdio.h"
#include "stdlib.h"

#define DataType int
#define INITSIZE 20
#define INCREMENT_SIZE 10

typedef struct Table{
    DataType* data;
    unsigned int length;
    unsigned int size;
}Table;

void InitTable(Table* table);
void DisplayTable(Table t);
void Insert(Table* table,DataType elem,int index);
void DeleteIndex(Table* table,const int index);
void Delete_DataByElem(Table* table,const DataType elem);
DataType Find_DataByIndex(const Table table,const int index);
int Find_indexByElem(const Table table,const DataType elem);
void pop_back(Table* table);
void pop_front(Table* table);
void push_back(Table* table ,const DataType elem);
void push_front(Table* table ,const DataType elem);
DataType get_front(const Table table);
DataType get_back(const Table table);
int size(const Table table);
bool isEmpty(const Table table);

#endif //LINUXC_MY_ARRAYLISTTABLE_H

my_ArrayListTable.c

//
// Created by 刘杰 on 2022/7/26.
//

#include "my_ArrayListTable.h"





void InitTable(Table* t){
    t->data = (DataType*) malloc(INITSIZE*sizeof (DataType));
    if(t->data == NULL){
        puts("申请空间失败!");
        exit(0);
    }
    t->length = 0;
    t->size = INITSIZE;

}
void DisplayTable(const Table t){
    if(isEmpty(t)){
        puts("数组元素为空,打印失败!");
        return ;
    }
    for(int i = 0;i<t.length;i++){
        if(i!=0&&i % 10 == 0)
            printf("\n");
        printf("%d ",t.data[i]);
    }
    printf("\n");

}
void Insert(Table* table,const DataType elem,const int index){
    if(table == NULL){
        puts("table为空!");
        exit(0);
    }
    if(index<1||index>table->length+1){
        puts("插入位置错误!");
        return ;
    }
    if((table->length==table->size)){
        table->data = (DataType*) realloc(table->data,(table->size+INCREMENT_SIZE)*sizeof (DataType));
        if(table->data == NULL){
            puts("开辟空间失败!");
            exit(0);
        }
        table->size+=INCREMENT_SIZE;
    }
    for(int i = table->length - 1 ;i>=index - 1;i--){
        table->data[i+1] = table->data[i];
    }
    table->data[index-1] = elem;
    table->length++;
}
void DeleteIndex(Table* table,const int index){
    if(table ==NULL||index < 0||index>table->length){
       puts("删除错误");
        exit(0);
    }
    for(int i = index;i<=table ->length - 1;i++){
        table->data[i] = table->data[i+1];
    }
    table->length--;
}
void Delete_DataByElem(Table* table,const DataType elem){
    int index =  Find_indexByElem(*table,elem);
    if(index == -1){
        puts("删除元素错误!");
        return ;
    }
    DeleteIndex(table,index);
    table->length--;
}
DataType Find_DataByIndex(const Table table,const int index){
    if(index < 1||index>table.length) {
        puts("查询错误!");
        return NULL;
    }
    return table.data[index - 1];

}
int Find_indexByElem(const Table table,const DataType elem){
    for(int i = 0;i<table.length;i++){
        if(elem == table.data[i])
            return i;
    }
    return -1;
}
void pop_back(Table* table){
    if(!isEmpty(*table)) {
        table->length--;
    }
    else {
        puts("删除错误!");
    }
}
void pop_front(Table* table){
    if(!isEmpty(*table)) {
        DeleteIndex(table, 1);
    }
    else {
        puts("删除错误!");
    }
}
void push_back(Table* table ,const DataType elem) {
    Insert(table, elem, table->length+1);
}
void push_front(Table* table ,const DataType elem){
    Insert(table,elem,1);
}
DataType get_front(const Table table){
    if(!isEmpty(table)){
        return table.data[0];
    }
    else {
        puts("数组为空,获取元素失败!");
    }
}
DataType get_back(const Table table){
    if(!isEmpty(table)){
        return table.data[table.length - 1];
    }
    else{
        puts("数组为空,获取元素失败!");
    }

}
int size(const Table table){
    return table.length;
}
bool isEmpty(const Table table){
    if(table.length == 0){
        return true;
    }
    return false;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jie3606

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值