|
发表于 2015-4-4 17:27:21
|
显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 10
typedef int ElemType;
typedef struct SqList
{
ElemType *data;
int size;
}SqList;
void Init_SqList(SqList *sqlist)
{
sqlist->data=(ElemType *)malloc(sizeof(ElemType)*MAXSIZE);
sqlist->size = 0;
}
void En_SqList(SqList *sqlist,int index,ElemType i)
{
int j;
if(index<1 || index>sqlist->size + 1)
{
printf("the index is not in the array\n");
return;
}
if(sqlist->size == MAXSIZE)
{
printf("the list is full\n");
return;
}
for(j=sqlist->size-1;j>=index-1;j--)
{
sqlist->data[j+1]=sqlist->data[j];
}
sqlist->data[index-1] = i;
sqlist->size++;
}
void Del_SqList(SqList *sqlist,int index,ElemType *i)
{
int j;
if(index<1 || index>sqlist->size + 1)
{
printf("the index is not in the array\n");
return;
}
*i=sqlist->data[index-1];
for(j=index-1; j<sqlist->size; j++)
{
sqlist->data[j] = sqlist->data[j+1];
}
sqlist->size--;
}
void main()
{
SqList sqlist;
int j,m;
Init_SqList(&sqlist);
for(j=1; j<6; j++)
{
En_SqList(&sqlist,j,j);
}
if(sqlist.size == 5)
{
for(j=0;j<sqlist.size;j++)
{
printf("%d ",sqlist.data[j]);
}
printf("\nthe remain space is %d\n",MAXSIZE-sqlist.size);
}
En_SqList(&sqlist,3,0);
for(j=0;j<sqlist.size;j++)
{
printf("%d ",sqlist.data[j]);
}
printf("\nthe remain space is %d\n",MAXSIZE-sqlist.size);
printf("please input the inseart index:");
scanf("%d",&j);
En_SqList(&sqlist,j,0);
printf("please input the delete index:");
getchar();
scanf("%d",&j);
Del_SqList(&sqlist,j,&m);
for(j=0;j<sqlist.size;j++)
{
printf("%d ",sqlist.data[j]);
}
printf("\nthe remain space is %d\n",MAXSIZE-sqlist.size);
} |
|