[回顾性练习2]C++实现数组中插入数据元素(数组)

题目2:C++实现数组中插入数据元素

向类SeqList中插入数据,请根据main函数中的调用,完成Insert和output函数。

 

 测试输入期待的输出
测试用例5
1 2 3 4 5
2 9
1 2 3 4 5
1 2 9 3 4 5

代码:

//前置代码
#include<iostream>
#include<stdlib.h>
using namespace std;
class SeqList
{
private:
   int * data;  
   int last; // index of the last element
public:
   SeqList ( int sz );
   ~SeqList ( ) { delete [ ] data; }
   void input ();
   void output() ;
   void Insert   ( const int &x, int i);
}  ;
SeqList::SeqList ( int sz )
{
   if ( sz > 0 )
   { data = new int[sz];
     last = -1;
   }
}
void SeqList:: input()
{
  cin >>last;
  for (int i=0;i<last;i++)
    cin>>data [i];
  last--;
}
//答题区域
void SeqList::Insert(const int &x, int i)
{
    int number=i;
    //int data_cpy[100];
    int *data_cpy;
    data_cpy=new int[100];
    for(int j=0;j<number;j++)
    {
        data_cpy[j]=data[j];
    }
    data_cpy[number]=x;
    for(int j=number;j<=last;j++)
    {
        data_cpy[j+1]=data[j];
    }
    /*for(int iii=0;iii<=last;iii++)
    {
        cout<<data_cpy[iii]<<" ";
        if(iii==last)
            cout<<endl;
    }*/
    data=data_cpy;//疑似错误点
}
void SeqList::output()
{
    last=last+1;
    cout<<*(data+0);
    for(int ii=1;ii<last;ii++){
        cout<<" "<<*(data+ii);
    }
    cout<<endl;

}
//后置代码
int main()
 {
  SeqList myList(50);
  myList.input();
  myList.output();
  int where,value;
  cin >>where;
  cin >>value;
  myList.Insert(value,where);
  myList.output ();
  return 1;
 }

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C语言中实现删除数组中指定元素的功能,可以按照以下步骤进行操作: 1. 首先,我们需要读取输入的整数N,表示数组的长度。然后,读取N个整数作为数组元素。 [1] 2. 接下来,读取想要删除的目标元素。 3. 创建一个新的数组,用于存储删除目标元素后的数组。 4. 使用一个循环遍历原始数组,判断每个元素是否等于目标元素。 5. 如果元素不等于目标元素,则将该元素添加到新的数组中。 6. 最后,输出新的数组,即为删除目标元素后的结果。 以下是一个示例的C代码实现: ```c #include <stdio.h> int main() { int n, array != target) { array[j = array[i]; j++; } } // 输出结果 for (int i = 0; i < j; i++) { printf("%d ", array[i]); } printf("\n"); return 0; } ``` 这段代码会根据输入的数组和目标元素,输出删除目标元素后的数组。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [删除数组中的指定元素——C++实现](https://blog.csdn.net/ling08140814/article/details/106398785)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [使用Java Native Interface调用本地C++代码.txt](https://download.csdn.net/download/weixin_44609920/88226706)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值