#include <iostream>
#include <string> //字符串函数头文件
using namespace std;
struct SqlList{
int *elem;
int length;
int listsize;
int incrementsize;
};
void initlist(SqlList &list,int n)// 创建函数重载,创建一个n个长度的线性表
{
list.elem=new int[100];
list.length=n;
list.listsize=100;
list.incrementsize=10;
}
void invert(SqlList &L,int s, int t)//转换操作
{
int *p,*q,w;
for(int k=s;k<=(s+t)/2;k++){
int l=t-k+s;
p=&L.elem[k];
q=&L.elem[l];
w=*p;
*p=*q;
*q=w;
}
}
void exchange2(SqlList A, int m, int n)//转换操作(2)
{
void invert(SqlList &L,int s, int t);
invert(A,0,m+n-1);
invert(A,0,n-1);
invert(A,n,m+n-1);
}
int main()
{
int n=5;
SqlList list;
initlist(list,n);
cout<<"please enter "<<n<<" numbers!"<<endl;
for(int a=0;a<5;++a)
cin>>list.elem[a];
cout<<"(1)改变顺序后:";
invert(list,0,4);
for(int i=0;i!=list.length;++i)
{
cout<<list.elem[i]<<" ";
}
cout<<endl;
cout<<"(2)改变顺序后:";
exchange2(list, 2,2 );
for(int i=0;i!=list.length;++i)
{
cout<<list.elem[i]<<" ";
}
cout<<endl;
return 0;
}
整理下,线性表一开始接触就是对于如何创立摸不着头脑,书上也都是一些伪代码,在百度完一些东西后,也渐渐的稍微明白了一些,多写写代码还是很有感觉的,如下啦:
#include <iostream>
#include <string> //字符串函数头文件
using namespace std;
typedef int ElemType;// 让ElemType为int型
struct SqlList{
ElemType *elem;
intlength;
intlistsize;
intincrementsize;
};
void initlist(SqlList &list)//创建一个空线性表
{
list.elem=new ElemType;
list.length=0;
list.listsize=0;
}
void initlist(SqlList &list,intn)// 创建函数重载,创建一个n个长度的线性表
{
list.elem=new ElemType[n];
list.length=n;
list.listsize=n*sizeof(ElemType)*2;
}
int locateelem_sq(SqlList L, ElemType e)//查找位置
{
inti=1;
int*p=L.elem;
while(i<=L.length && *p++!=e) ++i;
if(i<=L.length) return i;
elsereturn 0;
}
int ListInsert_sq(SqlList &L,int i,ElemType e)//插入元素
{
if((i<1)||(i>L.length))
return false;
if(L.length>=L.listsize) //当前存储空间已满,增加分配
{
int*a=new ElemType[L.listsize+L.incrementsize];
for(i=0;i<L.length;i++)a[i]=L.elem[i];
delete[]L.elem;
L.elem=a;
L.listsize+=L.incrementsize; //增加存储容量
}
ElemType *q=&(L.elem[i-1]);
for(ElemType*p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
returntrue;
}
void ListDelete_sq(SqlList &L,inti,ElemType &e)//删除L中第i个数据元素值并用e返回
{
int*p,*q;
if((i<1)||(i>L.length))
cout<<"i值不合法"<<endl;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)*(p-1)=*p;
L.length--;
}
bool destroylist(SqlList &list)//销毁线性表
{
delete[]list.elem;
list.length=0;
list.listsize=0;
returntrue;
}
bool listempty(SqlList &list)//判断线性表是否为空
{
returnlist.length==0?1:0;
}
int main()
{
intlocateelem_sq(SqlList L, ElemType e);
constint n=4;
SqlList list,list1;
initlist(list1);
initlist(list,n);
cout<<"请输入"<<n<<"个数字"<<endl;
for(int i=0;i!=n;++i)
{
cin>>list.elem[i];
}
cout<<"所输的"<<n<<"数字为"<<endl;
for(int i=0;i!=n;++i)
{
cout<<list.elem[i]<<endl;
}
cout<<endl;
cout<<"查找位置"<<endl;
int k;
int j=1;
k=locateelem_sq(list,j);
if(k)
cout<<"第"<<k<<"个元素的值为"<<j;
else
cout<<"没有值为"<<j<<"的元素"<<endl;
cout<<" "<<endl;
cout<<"删除元素"<<endl;
inte;
ints;
cout<<"要删除第n个元素:";
cin>>s;
ListDelete_sq(list,s,e);
for(int i=0;i!=list.length;++i)
{
cout<<list.elem[i]<<endl;
}
cout<<endl;
cout<<"插入元素"<<endl;
ListInsert_sq(list,1,2);
for(int i=0;i!=list.length;++i)
{
cout<<list.elem[i]<<endl;
}
cout<<endl;
if(destroylist(list))
{
cout<<"线性表删除成功!"<<endl;
}
else
{
cout<<"线性表删除失败!"<<endl;
}
if(listempty(list))
{
cout<<"线性表为空"<<endl;
}
else
{
cout<<"线性表不为空"<<endl;
}
return0;
}
整理下,线性表的创立 需要定义类型,然后就是一些算法的调用,&是表中元素有变化的时候,才会在参数前面加一个,大概就是实参传递吧...
还有一个...