函数体代码
#include <stdio.h>
#include <stdlib.h>
#include "./_data.h"
/*
* function: 创建顺序表
* @param [ in]
* @param [out]
* @return
*/
sqltype* create_sqltable()
{
sqltype* sq=(sqltype*)malloc(sizeof(sqltype));
if(sq==NULL)
{
printf("创建顺序表失败\n");
return NULL;
}
sq->pos = 0;
return sq;
}
/*
* function: 顺序插入
* @param [ in]
* @param [out]
* @return
*/
void inster_sqltable(sqltype * sq,use_type inster_data)
{
if(sq->pos >= N)
{
printf("顺序表已满,无法插入新的数据\n");
return;
}
sq->sql[sq->pos]=inster_data;
(sq->pos)++;
printf("插入数据成功\n");
}
/*
* function: 索引插入
* @param [ in]
* @param [out]
* @return
*/
void inster_index_sqltable(sqltype * sq,use_type value,int index)
{
if(sq->pos >= N)
{
printf("顺序表已满,无法插入新的数据\n");
return;
}
if(index < 0 || index>sq->pos)
{
printf("插入位置错误!\n");
return;
}
int i=sq->pos;
for(i;i>index;i--)
{
sq->sql[i]=sq->sql[i-1];
}
sq->sql[i]=value;
sq->pos++;
printf("插入数据成功\n");
}
/*
* function: 顺序删除
* @param [ in]
* @param [out]
* @return
*/
void delete_sqltable(sqltype* sq)
{
if(sq->pos==0)
{
printf("顺序表空空如也\n");
return;
}
(sq->pos)--;
}
/*
* function: 索引删除
* @param [ in]
* @param [out]
* @return
*/
void delete_index_sqltable(sqltype* sq,int index)
{
if(sq->pos==0)
{
printf("顺序表空空如也\n");
return;
}
if(index < 0 || index > sq->pos)
{
printf("索引位置不合法\n");
return;
}
int i=index;
for(i;i<sq->pos-1;i++)
{
sq->sql[i]=sq->sql[i+1];
}
(sq->pos)--;
}
/*
* function: 输出顺序表
* @param [ in]
* @param [out]
* @return
*/
void put_sqltable(sqltype* sq)
{
for(int i=0;i<sq->pos;i++)
{
printf("%-4d",sq->sql[i]);
}
printf("\n");
}
/*
* function: 释放申请的空间
* @param [ in]
* @param [out]
* @return
*/
void free_sq(sqltype** sq)
{
free(*sq);
*sq=NULL;
}
头文件代码
#ifndef __DATA__
#define __DATA__
#define N 7
typedef int use_type;
typedef struct
{
use_type sql[N];
int pos;
}sqltype;
void inster_sqltable(sqltype * sq,use_type inster_data);//顺序插入
void put_sqltable(sqltype* sq);//遍历输出顺序表
sqltype* create_sqltable();//创建顺序表
void free_sq(sqltype** sq);//释放空间
void delete_sqltable(sqltype* sq);//顺序删除
void inster_index_sqltable(sqltype * sq,use_type value,int index);//根据索引插入元素
void delete_index_sqltable(sqltype* sq,int index);
#endif
主函数代码
#include <stdio.h>
#include "./_data.h"
int main(int argc, const char *argv[])
{
sqltype* sq=create_sqltable();
inster_sqltable(sq,10);
inster_sqltable(sq,20);
inster_sqltable(sq,30);
printf("\n\n------------我是分割线-------------\n\n");
printf("原始表:\n");
put_sqltable(sq);
printf("索引插入首元素:\n");
inster_index_sqltable(sq,50,0);
put_sqltable(sq);
printf("索引插入尾元素:\n");
inster_index_sqltable(sq,50,4);
put_sqltable(sq);
printf("插入非法索引-1:\n");
inster_index_sqltable(sq,50,-1);
printf("插入非法索引6:\n");
inster_index_sqltable(sq,50,-1);
printf("\n\n------------我是分割线-------------\n\n");
printf("原始表:\n");
put_sqltable(sq);
printf("索引删除首元素:\n");
delete_index_sqltable(sq, 0);
put_sqltable(sq);
printf("索引删除尾元素:\n");
delete_index_sqltable(sq,3);
put_sqltable(sq);
printf("删除非法索引-1:\n");
delete_index_sqltable(sq,-1);
printf("删除非法索引6:\n");
delete_index_sqltable(sq,6);
free_sq(&sq);
return 0;
}
makefile代码
-include ./makefile.cfg
$(outfile):$(obj)
$(bianyiqi) $^ -o $@
%.o: %.c
$(bianyiqi) $^ $(canshu) $@
.PHONY:clean
clean:
rm $(obj)
makefile.cfg代码
obj=_main.o _data.o
outfile=a.out
bianyiqi=gcc
canshu=-c -o
运行结果