栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

目录
  • my_ArrayListTable.h
  • my_ArrayListTable.c

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

#ifndef LINUXC_MY_ARRAYLISTTABLE_H
#define LINUXC_MY_ARRAYLISTTABLE_H
#include 
#include 
#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
        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
        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;
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1026178.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号