#include <iostream>
#include <stdlib.h>
typedef int ElemType;
typedef struct
{
ElemType data;
int length;
}Sqlist;
Sqlist *Creat()
{
Sqlist *S;
S=(Sqlist *)malloc(sizeof(Sqlist));
if(!S)
{
std::cout<<"开辟空间失败!"<<std::endl;
return NULL;
}
else
{
S->length=-1;
return S;
}
}
int Init_list(Sqlist *S,int i,ElemType e)
{
int j;
for(j=S->length-1;j>=i-1;j--)
{
S->data[j+1]=S->data[j]; /*报错在这儿,大概意思是下标需要数组或指针类型.如果把"[j+1]"和"[j]"去掉,这里编译器就通过了,但达不到目的.你们看看就明白了. */
}
S->data=e;
S->length++;
return 0;
}
void display(Sqlist *S)
{
std::cout<<"最后顺序表中的元素为:"<<std::endl;
for(int i=0;i<S->length;i++)
std::cout<<S->data[i]<<" "; //还有这儿,同上!我不知道该怎样表示了??
}
int main()
{
Sqlist *S;
int i,j;
ElemType e;
S=Creat();
std::cout<<"请输入数组的长度:"<<std::endl;
std::cin>>j;
S->length=j;
std::cout<<"请输入数组:"<<std::endl;
for(i=0;i<j;i++)
std::cin>>S->data;
std::cout<<"原数组的内容为:"<<std::endl;
for(i=0;i<j;i++)
std::cout<<S->data<<" ";
std::cout<<std::endl<<"请输入插入数组的位置"<<std::endl;
std::cin>>i;
std::cout<<"请输入插入的元素:"<<std::endl;
std::cin>>e;
Init_list(S,i,e);
display(S);
return 0;
}
先谢谢啦!!