顺序表的创建与功能实现

  1 #include<iostream>
  2 #include<stdio.h>
  3 
  4 #define MAXSIZE 100
  5 #define OVERFLOW -2
  6 #define ERROR 0
  7 #define OK 1
  8 
  9 using namespace std;
 10 
 11 typedef struct squList
 12 {
 13     int *elem;
 14     int length;
 15 }SqList;
 16 
 17 //初始化
 18 int Init(SqList *L)
 19 {
 20     L->elem=new int[MAXSIZE];
 21     if(! L->elem) 
 22     {  
 23         exit(OVERFLOW);  //存储分配失败     exit(0)---结束程序 
 24     }  
 25     int n,i=0;
 26     cout<<"请输入顺序表元素个数"<<endl;
 27     cin>>n;
 28     while(i<n)
 29     {
 30         cin>>L->elem[i++];
 31     }
 32     return (L->length=i+1);
 33     cout<<"输出顺序表为:"<<endl;
 34 }
 35 
 36 //判断是否为空
 37 void IsEmpty(SqList *L)
 38 {
 39     if(L->length)
 40         cout<<"序列不为空"<<endl; 
 41     else 
 42         cout<<"序列为空"<<endl; 
 43 } 
 44 
 45 //输出序列 
 46 void print(SqList &L) 
 47 {
 48     int i;
 49     for(i=0;i<L.length-1;i++)
 50         cout<<L.elem[i]<<"  ";
 51     cout<<endl;
 52 }
 53 
 54 //插入元素 
 55 int  InsList(SqList *L)
 56 {
 57     int i,k;
 58     cout<<"请输入要插入元素的位置i和元素值k"<<endl;
 59     cin>>i>>k; 
 60     if((i<1) || (i>L->length+1)) 
 61     {
 62         printf("插入位置i值不合法");
 63         return ERROR;
 64     }
 65     if(L->length>= MAXSIZE-1)
 66     {
 67         printf("表已满无法插入");
 68         return ERROR;
 69     }
 70     for(int j=L->length;j>=i-1;j--)   
 71         L->elem[j+1]=L->elem[j];
 72     L->elem[i-1]=k;   
 73     L->length++;
 74     print(*L);
 75     return OK;
 76 }
 77 
 78 //删除元素 
 79 int  DelList(SqList *L)
 80 {
 81     int i,k;
 82     cout<<"请输入要删除元素的位置i"<<endl;
 83     cin>>i; 
 84     if((i<1)||(i>L->length+1))
 85     {
 86         cout<<"删除位置不合法!删除失败"<<endl;
 87         return ERROR;
 88     }
 89     for(k=i; k<=L->length; k++)
 90         L->elem[k-1] = L->elem[k]; 
 91     
 92     L->length--;
 93     cout<<"删除后的顺序表为"<<endl;
 94     print(*L);
 95     return OK;
 96 }
 97 
 98 //取表中第m个元素
 99 void GetElem( SqList *L)
100 {
101     int m;
102     cout<<"请输入所取元素的序号"<<endl;
103     cin>>m; 
104     if (m<1 || m>L->length+1) 
105         cout<<"你所输入的元素序号不合理"<<endl; 
106     else
107         cout<<""<<m<<"位元素的值为:"<<L->elem[m - 1]<<endl; 
108 }
109 //查找元素的序号
110 int  Locate(SqList *L)
111 {
112     int a;
113     int i=0;
114     cout<<"请输入要查找的元素值a"<<endl;
115     cin>>a;      
116     while ((i<=L->length)&&(L->elem[i]!=a))
117         i++;
118     if  (i<=L->length)
119         cout<<"元素"<<a<<"的序号为"<<i+1<<endl;  
120     else 
121         cout<<"未找到元素"<<a<<endl;     
122 }
123 
124 //改变元素值 
125 int Change(SqList *L)
126 {
127     int i,k;
128     cout<<"请输入要修改的元素的位置i和要替换的值k"<<endl;
129     cin>>i>>k;
130     if((i<1)||(i>L->length))
131         {
132             cout<<"修改位置不合法!修改失败"<<endl;
133             return ERROR;
134         }
135     L->elem[i-1]=k;   
136     cout<<"修改后的顺序表为"<<endl;
137     print(*L);
138     return OK;     
139 } 
140 
141 void merge(SqList *LA,  SqList *LB,  SqList *LC)//合并
142 {
143     int i,j,k;
144     i=0;j=0;k=0;
145     while(i<=LA->length&&j<=LB->length)
146         if(LA->elem[i]<=LB->elem[j])
147         {
148             LC->elem[k]= LA->elem[i];
149             i++; 
150             k++;
151         }
152         else
153         {
154             LC->elem[k]=LB->elem[j];
155             j++; 
156             k++;
157         }
158         while(i<=LA->length)
159         {
160             LC->elem[k]= LA->elem[i];
161             i++; 
162             k++;
163         }
164         while(j<=LB->length)  
165         {
166             LC->elem[k]= LB->elem[j];
167             j++; 
168             k++;
169         }
170         LC->length=LA->length+LB->length+1;
171 }
172 
173 int main()
174 {
175     SqList L,L1,L2;
176     int e;
177     
178     cout<<"初始化顺序表:"<<endl;
179     //初始化
180     Init(&L);
181     print(L);
182     
183     //在位置i处插入元素b 
184     InsList(&L);
185     
186     //删除
187     DelList(&L);
188     
189     //修改
190     Change(&L); 
191     
192     //查找
193     Locate(&L);
194     
195     //取对应位置元素
196     GetElem(&L);
197     
198     //判空
199     IsEmpty(&L); 
200     
201     cout<<"对L1、L2进行初始化操作:"<<endl;
202     Init(&L1);
203     Init(&L2);
204     cout<<"L1,L2,L合并后为:"<<endl;
205     merge(&L1,&L2,&L);
206     print(L);
207     return 0;
208 }

 

转载于:https://www.cnblogs.com/srx-0816/p/11528834.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值