zoukankan      html  css  js  c++  java
  • 【数据结构学习笔记】数组

     1 #include "stdafx.h"
     2 #include <iostream>
     3 using namespace std;
     4 #define maxSize 100
     5 
     6 //length要改变 使用引用类型
     7 int insertElem(int sqList[], int &length, int p, int e) {
     8     if (p < 0||p>length||length==maxSize) {
     9         return 0;
    10     }
    11     for (int i = length - 1; i >= p; --i) {
    12         sqList[i + 1] = sqList[i];
    13     }
    14     sqList[p] = e;
    15     ++length;
    16     return 1;
    17 }
    18 
    19 //可删除元素下标p的取值范围为:0~length-1
    20 //当表长length等于0的时候不可以再删除元素,移动元素从前往后进行
    21 //&e删除元素取出来 p是位置
    22 
    23 int deleteElem(int sqList[], int &length, int p, int &e) {
    24     if (p<0||p>length-1) {
    25         return 0;
    26     }
    27     e = sqList[p];
    28     for (int i = p; i < length - 1;++i) {
    29         sqList[i] = sqList[i + 1];
    30     }
    31     --length;
    32     return 1;
    33 }
    34 
    35 void main() {
    36 
    37     int sqList[maxSize] = { 1,2,3,4,5,6,7 };
    38     int length = 7;
    39     for (int i = 0; i < length - 1;i++) {
    40         cout << "原数组第"<<i<<"个位置: " << sqList[i] << endl;
    41     }
    42     int l = sizeof(sqList) / sizeof(sqList[0]);
    43     
    44 
    45     int r = insertElem(sqList, length, 3, 9);
    46     cout << "插入元素返回 " << r << endl;
    47 
    48     for (int i = 0; i < 7; i++) {
    49         cout << "新数组第" << i << "个位置: " << sqList[i] << endl;
    50     }
    51 
    52 
    53 }
  • 相关阅读:
    nxn随机矩阵乘以概率向量依旧是概率向量
    关于飞行器姿态计算
    两矩阵相乘后的秩
    关于矩阵A*b=A*c 中b是否等于c
    5.5节24题
    推论5.2.5
    js中function参数默认值
    陈经纶学校分析数据导出情况
    支付宝申请
    外国javascript资源搜索
  • 原文地址:https://www.cnblogs.com/dream-to-pku/p/11381622.html
Copyright © 2011-2022 走看看