表的操作——插入
有一线性表List,假设表中无重复值,现要在指定位置执行插入操作。插入成功时输出新表,否者输出“data error”。
输入格式:
有多组数据,每组数据占两行,代表一次插入操作。 每组第一行有三个整数,第一个为表长n(0<n<50),第二个为位置i,第三个为值x; 第二行为表的各元素值。
输出格式:
每组数据的输出占一行,输出如题。新表输出时,每两个元素之间有一空格。
输入样例:
在这里给出一组输入。例如:
5 4 9
1 3 5 11 16
输出样例:
在这里给出相应的输出。例如:
1 3 5 9 11 16
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <iostream>
using namespace std;
#define MAXSIZE 50
typedef struct{
int *elem;
int length;
}SqList;
int main(){
SqList L;
int i,n,x,k;
while(cin>>n>>x>>k){
x=x-1;
L.elem=new int[MAXSIZE];
for(i=0;i<n;i++)
cin>>L.elem[i];
L.length=n;
if(x<0||x>n)
cout<<"data error"<<endl;
else{
for(i=L.length;i>x;i--){
L.elem[i]=L.elem[i-1];
}
L.elem[x]=k;
L.length=n+1;
for(i=0;i<L.length;i++){
if(i==L.length-1)
cout<<L.elem[i]<<endl;
else
cout<<L.elem[i]<<" ";
}
}
}
}