顺序表的表示与实现

  1 #include<iostream.h>
  2 #include<stdlib.h>
  3 #define LIST_INIT_SIZE   100
  4 #define OK     1
  5 #define OVERFLOW    -2
  6 #define ERROR 0
  7 typedef int Status;
  8 
  9 typedef int ElemType;
 10 typedef struct
 11 {
 12     ElemType *elem;
 13     int   length;
 14     int listsize;
 15 }SqList;
 16 
 17 Status InitList_Sq(SqList &L)
 18 {
 19     //构造一个空的线性表
 20     L.elem=new ElemType[LIST_INIT_SIZE];
 21     if(!L.elem)
 22         exit(OVERFLOW);
 23     L.length=0;
 24     L.listsize=LIST_INIT_SIZE;
 25     return OK;
 26 }//InitList_Sq
 27 
 28 Status ListCreat_Sq(SqList &L,int n)
 29 {
 30     //创建顺序表
 31     //i的合法范围为 1<=i<=L.length+1
 32     ElemType x;
 33     cout<<"input x(n)="<<endl;
 34     for(int i=1;i<=n;++i)
 35     {
 36         cin>>x;
 37         L.elem[i-1]=x;
 38         ++L.length;
 39     }
 40     return OK;
 41 }//ListCreat_Sq
 42 
 43 int LocateElem_Sq(SqList L,ElemType e)
 44 {
 45     //在顺序表中查询第一个满足判定条件的数据元素
 46     //若存在,则返回它的位序,否则返回0
 47     ElemType *p;
 48     int i;
 49     i=1;        //i的初值为第1元素的位序
 50     p=L.elem;       //p的初值为第1元素的存储位置
 51     while(i<=L.length && (*p++!=e))
 52         ++i;
 53     if(i<=L.length)
 54         return i;
 55     else 
 56         return 0;
 57 }//LocateElem_Sq
 58 
 59 Status ListInsert_Sq(SqList &L,int i,ElemType e)
 60 {
 61     //在顺序表L的第i个元素之前插入新的元素e
 62     //i的合法范围为1<=i<=L.length+1
 63     ElemType *q,*p;
 64     q=&(L.elem[i-1]);        //q指示插入位置
 65     if(i<1||i>=L.length+1)
 66         return ERROR;
 67     if(L.length>=L.listsize)
 68         return OVERFLOW;      //当前存储空间已满,不能插入
 69     for(p=&(L.elem[L.length-1]);p>=q;--p)
 70         *(p+1)=*p;      //插入位置及之后的元素右移
 71     *q=e;       //插入e
 72     ++L.length;      //表长增1
 73     return OK;
 74 }//LisInsert_Sq
 75 
 76 Status ListDelete_Sq(SqList &L,int i,ElemType &e)
 77 {
 78     ElemType *q,*p;
 79     if((i<1)||(i>L.length))
 80         return ERROR;        //删除位置不合法
 81     if(L.length==0)
 82         return ERROR;      //空表不能删除
 83     p=&(L.elem[i-1]);      //p为被删除元素的位置
 84     e=*p;        //被删除元素的值赋给e
 85     q=L.elem+L.length-1;        //表尾元素的位置
 86     for(++p;p<=q;++p)
 87         *(p-1)=*p;       //被删除元素之后的元素左移
 88     --L.length;       //表长减1
 89     return OK;
 90 }//ListDelete_Sq
 91 
 92 void visit_Sq(SqList L)
 93 {
 94     //访问顺序表的各个元素
 95     for(int i=1;i<=L.length;++i)
 96     {
 97         cout<<L.elem[i-1]<<"  ";
 98     }
 99     cout<<endl;
100 }//visit_Sq
101 
102 
103 void main()
104 {
105     SqList L;
106     ElemType x;
107     int i,n;
108     InitList_Sq(L);
109     cout<<"input sqList n=";
110     cin>>n;
111     ListCreat_Sq(L,n);
112         cout<<endl;
113     cout<<L.length<<endl;
114     visit_Sq(L);
115     cout<<"input insert i,x=";
116     cin>>i>>x;
117     if(ListInsert_Sq(L,i,x))
118     {
119         visit_Sq(L);
120         cout<<L.length<<endl;
121     }
122     else 
123         cout<<"Insert i error!\n";
124     cout<<L.length<<endl;
125 
126     cout<<"input delete i=";
127     cin>>i;
128     if(ListDelete_Sq(L,i,x))
129     {
130         visit_Sq(L);
131         cout<<L.length<<endl;
132     }
133     else
134         cout<<"delete i error!\n";
135 
136     cout<<"input find x=";
137     cin>>x;
138     i=LocateElem_Sq(L,x);
139     if(i)
140         cout<<"L.elem["<<i-1<<"]="<<L.elem[i-1]<<endl;
141     else
142         cout<<"no find!";
143 }

 

posted on 2013-04-23 21:43  绕指柔yy  阅读(272)  评论(0编辑  收藏  举报

导航