数据结构--线性表的顺序表示及操作

来源:互联网 发布:成都网络车新规 编辑:程序博客网 时间:2024/05/17 00:56

线性表中数据元素的逻辑关系是:元素在计算机中物理位置相邻(逻辑上相邻的元素在物理位置上也是相邻的)

线性表的顺序存储结构是一种随机存取的储存结构,但是在进行删除和插入的是需要移动大量的元素。

#include <stdio.h>#include <stdlib.h>#include <iostream>#define LIST_INIT_SIZE 100#define LISTINSERCE 10using  namespace  std;typedef int Elemtype ;struct SqList{    int *element;    int len;    int listsize;};///位置合法性判断///长度的改变int  InitList(SqList &L);void Sscanf(SqList &L);int  ListInsert(SqList &L,int pos,Elemtype e);bool ListDelete(SqList &L,int pos);int LocateElem(SqList L,Elemtype e);void MergeList_Sq(SqList La,SqList Lb,SqList &Lc);void Print(SqList La);int main(){    SqList La;    InitList(La);    Sscanf(La);    Print(La);    ListInsert(La,4,99999);    Print(La);    cout<<"*******"<<endl;    ListDelete(La,5);    Print(La);    ///访问出界会报错    // ListDelete(La,10);    //  Print(La);    int pos1= LocateElem(La,99999);    int pos2= LocateElem(La,8888888);    if(pos1!=-1) cout<<"Find it ! "<<pos1<<endl;    else cout<<"Not Find !"<<endl;    if(pos2!=-1) cout<<"Find it ! "<<pos2<<endl;    else cout<<"Not Find !"<<endl;    SqList Lb;    InitList(Lb);    Sscanf(Lb);    SqList Lc;    MergeList_Sq(La,Lb,Lc);    Print(Lc);    return 0;}/// 在构造一个线性表的时候首先进行内存分配,如果内存分配失败则返回false;///内存分配完毕以后,对线性表的一些属性值进行初始化,返回true;int  InitList(SqList &L){    L.element=(Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype));    if(!L.element) return 0;//false;    L.len=0;    L.listsize=LIST_INIT_SIZE;    return 1;//true;}/// 对线性表里面element进行输入void Sscanf(SqList &L){    scanf("%d",&L.len);    int *p=L.element;    for(int i=0; i<L.len; i++)    {        int x;        scanf("%d",&x);        *p=x;        p++;    }}/// 在线性表中插入一个数///在插入一个数之前要先判断位置的合法性,如果不合法则返回false;/// 判断当前的长度是不是大于当前线性表的最大长度,如果大于线性表的最大长度,则对线性表进行长度的重新分配///移动插入位置之前的元素///在所要插入位置进行赋值///当前线性表长度++;int  ListInsert(SqList &L,int pos,Elemtype e){    int temp=pos-1;    if(!L.element) return false;    if(temp<0||temp>L.len) return false;    int *newbase;    if(L.len>=L.listsize)    {        ///注意重新分配时的表示方法        // newbase=(Elemtype *)realloc(L.element,(L.listsize+LISTINSERCE)*sizeof(Elemtype));        // if(!newbase) return false;        //  L.element=newbase;        L.element=(Elemtype *)realloc(L.element,(L.listsize+LISTINSERCE)*sizeof(Elemtype));        if(!L.element) return false;        L.listsize+=LISTINSERCE;    }    ///两种方式    /* for(int i=L.len-1; i>=pos-1; i--)         L.element[i+1]=L.element[i];     L.element[pos-1]=e;*/    Elemtype *p=&(L.element[pos-1]);    for(Elemtype *q=&(L.element[L.len-1]); q>=p; q--) *(q+1)=*(q);    *p=e;    ++L.len;    return true;}///删除一个位置///首先进行位置合法性判断,如果位置不合法,则返回false;///将所删除位置之后的元素向前移一位///线性表长度--;bool ListDelete(SqList &L,int pos){    int temp=pos-1;    if(temp<0||temp>=L.len) return false;    if(!L.element) return false;    Elemtype *p=&(L.element[pos-1]);    //  e=*p;    Elemtype *q=&(L.element[L.len-1]);    for(p; p<q; p++)    {        *p=*(p+1);    }    L.len--;    return true;}int LocateElem(SqList L,Elemtype e){    int pos=0;    int *p=L.element;    int *p_last=L.element+L.len;    for(p; p<p_last; p++)    {        if(*p==e)        {            return pos;        }        pos++;    }    return -1;}void MergeList_Sq(SqList La,SqList Lb,SqList &Lc){    Lc.len=La.len+Lb.len;    Lc.listsize=LIST_INIT_SIZE;    while(Lc.listsize<=Lc.len)    {        Lc.listsize+=LISTINSERCE;    }    int *c=Lc.element=(Elemtype *)malloc(Lc.listsize*sizeof(int));    ///    if(!Lc.element) return ;    ///    int *a=La.element;    int *b=Lb.element;    int *a_last=La.element+La.len-1;    int *b_last=Lb.element+Lb.len-1;    while(a<=a_last&&b_last)    {        if(*a<*b)        {            *c=*a++;            c++;        }        else *c++=*b++;    }    while(a<=a_last) *c++=*a++;    while(b<=b_last) *c++=*b++;    return ;}void Print(SqList La){    int *p=La.element;    int *p_last=La.element+La.len-1;    while(p<=p_last)    {        cout<<*p++<<" ";    }    cout<<endl;}