线性表的数据结构是:数据的类型一样...
所以线性表就类似于数组,对线性表的操作,更像与数组的操作...哦
如:
typedef struct list
{
int *elem; //里面的核心,实质上他就是一个整形的数组,int *temp=*.elem *表示的是一个这个
//结构类型的变量,看初始化就说明了,他的操作就是跟数组一样。
int length;//用来补充对该数据类型的操作
int size;//用来补充对该数据类型的操作
}List;
我的理解就是线性表的操作无非就是对数组的操作,只不过他有比较完善的函数为其方便而存在,而那些函数也仅仅是一些对数组的基本操作而已,一般用到顺序结构的线性表时,用数组完全可以代替,只不过,如何多人合作的话,顺序结构的线性表的优势就展现出来了哦...
这是我写的一个关于顺序结构的线性表的程序(在下面还有一个相当比较标准的它的一个具体表示法):
# include <iostream>
using namespace std;
typedef struct list
{
int *elem;
int length;
int size;
}List;
void Initlist(List &L)
{
L.elem = (int*)malloc(100*sizeof(int));
L.length=0;
L.size=100;
}
void input(list &L , int len)
{
int *temp=L.elem;
int e;
for(int i=0 ; i<len ; ++i,++temp)
{
cout<<"输入你初始化的整数"<<"第"<<i+1<<"个:"<<endl;
cin>>e;
*temp=e;
}
L.length=len;
}
void AddLength(list &L)
{
L.elem=(int*)realloc(L.elem,100*sizeof(int));
L.size=L.size+100;
}
void Insert(List &L , int i ,int e)
{
if(L.length>=100) AddLength(L);
if(i > L.length+1)
{
cout<<"error"<<endl;
exit(0);
}
int *temp ;
if(i<L.length+1)
{
temp=L.elem + L.length;
for( ; temp > L.elem+i-1 ; --temp)
{
*temp=*(temp-1);
}
}
if(i==L.length+1)
{
temp=L.elem + i-1;
}
*temp=e;
L.length+=1;
}
void Del(List &L , int i )
{
if(i>L.length)
{
cout<<"error"<<endl;
exit(0);
}
int *temp=L.elem +i-1;
cout<<"你删除的数字是:"<<*temp<<endl;
for( ; temp<L.elem+L.length-1 ; ++temp)
{
*temp=*(temp+1);
}
L.length-=1;
}
void Index(List L , int e)
{
int *temp;
int n=0 ,indicate=0;
for(temp=L.elem; temp<L.elem+L.length ;++temp)
{
n++;
if(e==*temp)
{
indicate=1; //标志的是看看是否存在 存在indicate=1 不存在indicate=0
cout<<"存在该数:"<<e<<" 在顺序表中的位置是:"<<n<<endl;
break;
}
}
if(!indicate) //不存在indicate=0 输出
cout<<"不存在该数"<<n<<endl;
}
void GetElem(list L , int i)
{
if(i>L.length)
{
cout<<"error"<<endl;
return ;
}
cout<<"你要得到的数是:"<<L.elem[i-1]<<endl;
}
void out(List L)
{
int *temp=L.elem;
if(L.length>0)
{
for(;temp<L.elem+L.length ; ++temp)
{
cout<<*temp<<" ";
}
}
else cout<<"空表";
cout<<endl;
}
int main()
{
List n;
int e , i;
int len;
Initlist(n);
cout<<"输入你初始化时顺序表的长度"<<endl;
cin>>len;
input(n,len);
cout<<"输出全部数:";
out(n);
cout<<"你要插入的数及插入的位置"<<endl;
cin>>e>>i;
Insert(n,i,e);
cout<<"输出全部数:";
out(n);
cout<<"输入你要删除的数的位置"<<endl;
cin>>i;
Del(n,i);
cout<<"输出全部数:";
out(n);
cout<<"输入你要得到的数的顺序号"<<endl;
cin>>i;
GetElem(n , i);
cout<<"输出全部数:";
out(n);
cout<<"输入你要查找的数"<<endl;
cin>>e;
Index(n,e);
cout<<"输出全部数:";
out(n);
return 0;
}
它的一个具体表示法:
# include <iostream>
using namespace std;
const LIST_INIT_SIZE=100;
const LISTINCREMENT=10;
typedef struct{
int *elem;
int length;
int listsize;
}SqList;
void InitList_sq(SqList&L){
//构造一个空的线性表L
L.elem=new int[LIST_INIT_SIZE];
if(!L.elem)
exit(0); //存储分配失败
L.length=0; //空表长度为0
L.listsize=LIST_INIT_SIZE; //初始存储容量
cout<<"OK!"<<endl;
}
void ListInsert_Sq(SqList&L,int i,int e){
//在顺序线性表L中第i个位置之前插入新的元素e,
//i的合法值为1<=i<=ListInsert_Sq(L)+1
if(i<1||i>L.length+1) //i值不合法
cout<<"ERROR!"<<endl;
if(L.length>=L.listsize){ //当前存储空间已满,增加分配
int *newbase=(int*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(int));
if(!newbase)
exit(0); //存储分配失败
L.elem=newbase; //新基址
L.listsize+=LISTINCREMENT; //增加存储容量
}
int *q=&(L.elem[i-1]);
for(int*p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
cout<<"OK!"<<endl;
}//ListInsert_Sq
void ListDelete_Sq(SqList&L,int i,int&e){
//在顺序线性表L中删除第i个元素,并用e返回其值
//i的合法值为1<=i<=ListInsert_Sq(L)
if((i<1)||(i>L.length))
cout<<"ERROR!"<<endl; //i值不合法
int *p=&(L.elem[i-1]); //p为被删除元素的位置
e=*p; //被删除元素的值赋给e
int *q=L.elem+L.length-1; //表尾元素的位置
for(++p;p<=q;++p)
*(p-1)=*p; //被删除元素之后的元素左移
--L.length; //表长减1
cout<<"OK!"<<endl;
}//ListDelete_Sq
bool compare(int m,int n){
if(m==n)
return true;
else
return false;
}
int LocateElem_Sq(SqList L,int e){
//在顺序线性表L中查找第1个值与e满足compare()的元素的位序
//若找到,则返回其在L中的位序,否则返回0
int i=1; //i的初值为第1个元素的位序
int *p=L.elem; //p的初值为第1个元素的存储位置
while(i<=L.length&&!compare(*p++,e))
++i;
if(i<=L.length)
return i;
else
return 0;
}//LocateElem_Sq
void disp(SqList&L){
int *p=L.elem;
for(int i=0;i<L.listsize;i++){
cout<<*p<<"/t";
p++;
}
}
void main(){
SqList List;
InitList_sq(List);
int *p=List.elem;
for(int i=0;i<List.listsize;i++){
*p=i+1;
p++;
}
cout<<endl;
disp(List);
cout<<endl;
}
10-24
452
04-09