跟我学数据结构:(2) 数组的遍历、插入和删除

 

跟我学数据结构:(2) 数组的遍历、插入和删除
摘要:
数组广泛的应用于各种数据结构和算法之中。本文通过代码演示了:
产生数组,遍历数组,查找数组中的最小值,在指定位置插入和删除元素。
 
本文仍然是作为学习数据结构的基础。
 
代码和运行结果
本课内容比较单纯,我们直接上代码,同学们可以多次运行代码,观察结果。
 
Code:
  1. /*  
  2.  desc: this program illustration  how to use an array.  
  3. */  
  4.   
  5. #include <iostream>   
  6. #include<iomanip>   
  7. #include<string>   
  8. #include <math.h>   
  9. #include <time.h>   
  10. #include <cstdlib>   
  11.     
  12. using namespace std;   
  13. const int MAX_SIZE = 10;    
  14.   
  15. int data[MAX_SIZE];   
  16. int pos = 0;   
  17.   
  18. void showArray()   
  19. {   
  20.     int i;   
  21.     for(i = 0; i < MAX_SIZE; i++)   
  22.     {   
  23.         cout << "/t data[ " << i << " ] = " << data[i] << endl;   
  24.     }   
  25.     cout << endl;   
  26.   
  27. }   
  28.   
  29. int main(int argc, char* argv[])   
  30. {   
  31.    cout << argv[0]<< " start here" << endl;   
  32.    cout << " this program illustration  how to use an array." << endl;   
  33.   
  34.    cout << "initialize  array by random numb:" << endl;   
  35.   
  36.   
  37.    srand((unsigned int)time(0));   
  38.   
  39.    cout << " RAND_MAX  = " << RAND_MAX<< endl;   
  40.    int i;   
  41.    for(i = 0; i < MAX_SIZE; i++)   
  42.    {   
  43.        ::data[i] = rand() % 100;   
  44.    }   
  45.    showArray();   
  46.   
  47.   cout << " find out the minimum value in the array" << endl;   
  48.    int pos = 0;   
  49.       
  50.    for(i = 1; i < MAX_SIZE; i++)   
  51.    {   
  52.        if (data[pos] > ::data[i])   
  53.        {   
  54.            pos = i;   
  55.        }   
  56.    }   
  57.    cout << " the minimum value in the array is  "    
  58.         << "data[ " << pos << " ] = " << data[pos] << endl;   
  59.   
  60.   
  61.    cout << "generate a new position and new value, then insert it into the array"  
  62.         << endl;   
  63.    int newPos = rand() % MAX_SIZE;   
  64.    int newValue = rand() % 100;   
  65.    cout << "new pos = " << newPos <<  ", and new value = " << newValue << endl;   
  66.   
  67.    for( i = MAX_SIZE-1; i > newPos; i--)   
  68.    {   
  69.        ::data[i] = ::data[i-1];   
  70.    }   
  71.    ::data[newPos] = newValue;   
  72.   
  73.    showArray();   
  74.   
  75.   
  76.    cout << " generate a new position , and make the value is zero" << endl;   
  77.    newPos = rand() % MAX_SIZE;   
  78.    cout << " new pos = " << newPos << endl;   
  79.     ::data[newPos] = 0;   
  80.    showArray();   
  81.   
  82.    cout << " let's delete the no. " <<  newPos << " element in the array,/n"  
  83.         << " and make the last value is minus one." << endl;   
  84.    for(int i = newPos; i <MAX_SIZE-1; i++)   
  85.    {   
  86.        ::data[i] = ::data[i+1];   
  87.    }   
  88.    ::data[MAX_SIZE-1] = -1;   
  89.   
  90.    showArray();   
  91.   
  92.    return 0;   
  93. }   
  94.   
  95.   

test start here
 this program illustration how to use an array.
initialize array by random numb:
 RAND_MAX = 32767
          data[ 0 ] = 42
          data[ 1 ] = 28
          data[ 2 ] = 16
          data[ 3 ] = 4
          data[ 4 ] = 34
          data[ 5 ] = 71
          data[ 6 ] = 12
          data[ 7 ] = 30
          data[ 8 ] = 99
          data[ 9 ] = 14
 
 find out the minimum value in the array
 the minimum value in the array is data[ 3 ] = 4
generate a new position and new value, then insert it into the array
new pos = 4, and new value = 53
          data[ 0 ] = 42
          data[ 1 ] = 28
          data[ 2 ] = 16
          data[ 3 ] = 4
          data[ 4 ] = 53
          data[ 5 ] = 34
          data[ 6 ] = 71
          data[ 7 ] = 12
          data[ 8 ] = 30
          data[ 9 ] = 99
 
 generate a new position , and make the value is zero
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 22
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值