my_ArrayListTable.h
#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
my_ArrayListTable.c
#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;
}