1、创建数组
#include <stdio.h>
#define MAXSIZE 20
typedef int ELEMTYPE;
typedef struct sqlist
{
int array[MAXSIZE];
int length;
}Sqlist,*pSqlist;
2、数组的插入操作
int sqlist_insert(pSqlist p_sq,int i,ELEMTYPE elem_new)
{
int j;
if(p_sq->length==MAXSIZE)
{
return 0;
}
if(i<1||i>p_sq->length+1)
{
return 0;
}
if(i<=p_sq->length)
{
for(j=p_sq->length;j>=i;j--)
{
p_sq->array[j]=p_sq->array[j-1];
}
p_sq->array[i-1]=elem_new;
}
else
{
p_sq->array[p_sq->length]=elem_new;
}
p_sq->length++;
return 1;
}
3、数组的删除操作
int sqlist_del(pSqlist p_sq,int i,ELEMTYPE *e)
{
int j;
if(p_sq->length==0)
{
return 0;
}
if(i<1||i>p_sq->length)
{
return 0;
}
*e=p_sq->array[i-1];
for(j=i;j<p_sq->length;j++)
{
p_sq->array[j-1]=p_sq->array[j];
}
p_sq->length--;
return 1;
}
4、遍历数组
void traverse(pSqlist p_sq)
{
int i;
for(i=1;i<=p_sq->length;i++)
{
printf("第%d个元素为:%d\n",i,p_sq->array[i-1]);
}
}