线性表怎么写( 二 )


const int LIST_INIT_SIZE=100; const int LISTINCREMENT=10;
typedef struct { ElemType * elem; int length; int listsize; int incrementsize; }SqList;
int InitList_Sq (SqList &L, int maxsize= LIST_INIT_SIZE, int incremesize= LISTINCREMENT) { L.elem = new ElemType [maxsize]; if(&L==NULL) return 0; L.length = 0; L.listsize = maxsize; L.incrementsize = incremesize; return OK; }
void Creat_Sq(SqList &L) { int i; char choose; for(i=L.length;i<L.listsize;) { cout<<"Please input the number: "; cin>>L.elem[i]; cout<<"continue to input?[n/y]"; cin>>choose; L.length++; i=L.length; if(choose=='n'||choose=='N') break; } }
void Free_Sq(SqList &L) { delete L.elem; }
void Print_Sq(SqList &L) { int i; for(i=0;i<L.length;i++) { cout<<L.elem[i]<<' '; } cout<<endl; }
int main() { SqList l; InitList_Sq(l); Creat_Sq(l); Print_Sq(l); Free_Sq(l); return 0; }
运行结果如下:
不好意思前面误导了你一下,本来以为你是C语言我想C语言没有引用类型的 。线性表创建不是什么问题,难度在插入和删除要进行数据移动的,如果还有什么问题可以追问 。
7. 写出线性表操作的算法 #include<stdio.h>
void search(int a[];int b)
{
int i=0;
while(i<10&&a[i]!=b)
i++;
if(i<10){
printf("found!");
return;
}
else
{
printf("not found");
return;
}
}
main()
{
int a[]={12,26,39,30,52,43,80,92,101,89};
search(a,b);
}

线性表怎么写

文章插图