写了个小程序,貌似还有错,求指点
#include<iostream.h>#include<stdio.h>
#include<stdlib.h>
typedef int DataType;
struct SeqList
{
int MAXNUM;
int n;
DataType *element;
};
typedef struct SeqList *PSeqList;
PSeqList creatList_seq(int m)
{
PSeqList palist=(PSeqList)malloc(sizeof(struct SeqList));
if(palist!=NULL)
{
palist->element=(int *)malloc(sizeof(int)*m);
if(palist->element)
{
palist->element;
palist->MAXNUM=m;
palist->n=20;
for(int i=0;i<palist->n-1;i++)
{
palist->element[i]=i+1;
}
return palist;
}
else
free(palist);
}
printf("out of space!!\n");
return NULL;
}
int isNULLList_seq( PSeqList palist)
{
return(palist->n==0);
}
/*求x在palist所指顺序表中的下标*/
int locate_seq(PSeqList palist,DataType x)
{
int q;
for(q=0;q<palist->n;q++)
if(palist->element[q]==x)
return q;
return -1;
}
/*在palist所指顺序表表中下标为p的元素之前插入元素x*/
int insertPre_seq(PSeqList palist,int p,DataType x)
{
int q;
if(palist->n>=palist->MAXNUM)
{
printf("overflow!\n");
return 0;
}
if(p<0||p>palist->n)
{
printf("Not exist!\n");
return 0;
}
for(q=palist->n-1;q>=p;q--)
{
palist->element[q+1]=palist->element[q];
}
palist->element[p]=x;
palist->n=palist->n+1; //元素个数加1
return 1;
}
int main()
{
PSeqList palist;
palist=creatList_seq(40);
for(int q=0;q<palist->n-1;q++)
cout<<palist->element[q];
cout<<endl;
cout<<insertPre_seq( palist,4,1);
cout<<endl;
cout<<locate_seq(palist,3);
cout<<endl;
return 0;
}