# 线性表c语言实现----初始化插入删除

#include <stdlib.h>

#include <stdio.h>
#define maxsize 100

typedef struct node{
int data[maxsize];
int length;
}SeqList;

void SeqListInit (SeqList *p,int n)    /* seqlist init  */
{
int i;
if(n>maxsize)
{
printf("error\n");
}
for(i=0;i<n;i++)
{
p->data[i]=i+1;
}
p->length=n;
}

void SeqListInsert(SeqList *p,int n,int x)
{
int *p1,*p2;
if(n<1||n>p->length)
{
printf("insert position error\n");
return ;
}
if(p->length>=maxsize)
{
printf("no enough space\n");
return ;
}
p1=&(p->data[n]);
p2=&(p->data[p->length]);
for(;p2>=p1;p2--)
{
*p2=*(p2-1);
}
*p1=x;
p->length++;
}

void SeqListDelete(SeqList *p,int n,int *x)
{
int *p1,*p2;
if(n<1||n>=p->length)
{
printf("delete position error \n");
return ;
}
p1=&(p->data[n]);
p2=&(p->data[p->length]);
*x=*p1;
for(;p1<p2;p1++)
{
*p1=*(p1+1);
}
p->length--;
}

int main()
{
SeqList sl;
int i;
int n=7;
SeqListInit(&sl,n);
printf("element(l~%d):\n",sl.length);
for(i=0;i<sl.length;i++)
{
printf("%d ",sl.data[i]);
}

printf("\n");
SeqListInsert(&sl,5,10);
printf("insert hou: \n");
for(i=0;i<sl.length;i++)
{
printf("%d ",sl.data[i]);
}

printf("\n");
/* int *p=10;
SeqListDelete(&sl,5,&p);*/

printf("\n");
return 0;
}