#include<iostream>
using namespace std;
int main()
{
int a[100], n, data, item, i, k;
cin>>n; //Input the length of the string.
for(i=0; i<n; i++) //Loop for pushing each element of the string
{
cin>>a[i];
}
cin>>item; //The item we want to locate
cin>>data; //Data we wanner push before item
for(i=0; i<n; i++) //Identify the location of the item
{
if(a[i] == item)
{
k=i;
break;
}
else
k=0;
}
if(k!=0) //If find the item,them insert data before it
{
for(i=n+1; i>k-1; i--)
{
a[i+1]=a[i];
}
a[k] = data;
}
else //No target, insert at the end of the string
{
a[n]=data;
}
n++; //Plus n
for(i=0; i<n; i++) //Print.
{
cout<<a[i]<<' ';
}
return 0;
}
顺序表插入操作的实现(0943)
最新推荐文章于 2024-09-04 00:33:47 发布