首页 > 代码库 > 1-线性表 顺序存储

1-线性表 顺序存储

仅供学习记录参考

如果有人觉得我的代码能帮到的话也很好

有问题可以随时提问!

2017/7/11

1、基本功能的实现

  1 #include<iostream>
  2 #include<cstdlib>
  3 using namespace std;
  4 #define ture 1
  5 #define false 0
  6 #define maxsize 50
  7 #define listincreament 10
  8 #define ok 1
  9 #define error -3
 10 #define overflow -2
 11 typedef int Status;
 12 typedef int ElemType;
 13 
 14 typedef struct{
 15 ElemType *data;
 16 int length;
 17 int listsize;
 18 }SqList;
 19 
 20 Status initList_Sq(SqList &L)
 21 {
 22     L.data=http://www.mamicode.com/(ElemType *)malloc(maxsize*sizeof(ElemType));
 23     L.length=0;
 24     L.listsize=maxsize;
 25     return ok;
 26 }
 27 Status ListInsert_Sq(SqList &L,int i,ElemType e)
 28 {
 29     ElemType *newbase,*p,*q;
 30     //顺序线性表L中第i个位置插入新的元素e
 31     if(i<1||i>L.length)return error;
 32     if(L.length>=L.listsize)
 33     {
 34         //存储空间满,增加分配。
 35         newbase=(ElemType *)realloc(L.data,(L.listsize+listincreament)*sizeof(ElemType));
 36         if(!newbase) exit(overflow);
 37         L.data=http://www.mamicode.com/newbase;
 38         L.listsize+=listincreament;
 39     }
 40 
 41     for(int j=L.length;j>=i;j--)
 42     {
 43         L.data[j]=L.data[j-1];
 44     }
 45     L.data[i-1]=e;
 46     L.length++;
 47     return ok;
 48 }
 49 void showList(SqList &L)
 50 {
 51     ElemType *p;
 52     int i=0;
 53     for(int i=0;i<L.length;i++)
 54     {
 55         cout<<L.data[i];
 56         if(i!=L.length-1) cout<< ;
 57         else cout<<endl;
 58     }
 59 }
 60 Status DeleteList_Elem(SqList &L,int i)
 61 {
 62     if(i<0||i>=L.length) return error;
 63     for(int j=i;j<L.length-1;j++)
 64     {
 65         L.data[j]=L.data[j+1];
 66     }
 67     L.length--;
 68     return ok;
 69 }
 70 int LocateElem(SqList &L,ElemType e)
 71 {
 72     int pos=-1;
 73     for(int j=0;j<L.length;j++)
 74     {
 75         if(L.data[j]==e)
 76             {pos=j;break;}
 77     }
 78     return pos+1;
 79 }
 80 //细节:返回值要+1
 81 int mergeList(SqList &a,SqList &b,SqList &c)
 82 {
 83     ElemType *x,*y,*z;
 84     int ia=0,ib=0,ic=0;
 85     while(ia<a.length-1&&ib<b.length-1)
 86     {
 87         if(a.data[ia]<b.data[ib])
 88         {
 89             c.data[ic++]=a.data[ia++];
 90         }
 91         else {c.data[ic++]=b.data[ib++];}
 92     }
 93     while(ia<a.length-1)
 94         c.data[ic++]=a.data[ia++];
 95     while(ib<b.length-1)
 96         c.data[ic++]=b.data[ib++];
 97     c.length=ic;
 98     return ok;
 99 }
100 
101 int main()
102 {
103     int w;
104     SqList L1,L2,L3;
105     w=initList_Sq(L1);
106     for(int i=0;i<10;i++)
107     {L1.data[i]=i+1;L1.length++;}
108     L1.length=10;
109 
110     initList_Sq(L2);
111     initList_Sq(L3);
112 
113     for(int i=0;i<7;i++)
114     {L2.data[i]=(i+1)*3;}
115     L2.length=7;
116 
117     //cout<<w;
118     showList(L1);
119     ListInsert_Sq(L1,4,33);
120     DeleteList_Elem(L1,7);
121     showList(L1);
122     int a=LocateElem(L1,33);
123     cout<<a<<endl;
124     mergeList(L1,L2,L3);
125     showList(L3);
126     return 0;
127 }

 

1-线性表 顺序存储