zoukankan      html  css  js  c++  java
  • 【问题】用C++结构体实现顺序表,数据超过数组长度后,表长自动+1

     1 #include <stdio.h>
     2 #include "stdlib.h"
     3 
     4 #define LIST_INIT_SIZE 100
     5 #define LISTINCREMENT 10
     6 
     7 typedef int ElemType;
     8 
     9 typedef struct
    10 {
    11     ElemType elem[LIST_INIT_SIZE];
    12     int length;
    13     int listsize;
    14 }SqList;
    15 
    16 class Sq
    17 {
    18     public:
    19         SqList sqList;
    20 
    21         Sq();
    22         ElemType getElem(int i);
    23         bool listInsert(int i, ElemType e);
    24         bool listDelete(int i, ElemType &e);
    25 };
    26 
    27 Sq::Sq()
    28 {
    29     this->sqList.length = 0;
    30     this->sqList.listsize = LIST_INIT_SIZE;
    31 }
    32 
    33 ElemType Sq::getElem(int i)
    34 {
    35     if (i<1 || i>sqList.length) return ElemType();
    36 
    37     ElemType elem = sqList.elem[i - 1];
    38 
    39     return elem;
    40 }
    41 
    42 bool Sq::listInsert(int i, ElemType e)
    43 {
    44     if (i<1 || i>sqList.length + 1) return false;
    45 
    46     for (int j = sqList.length - 1; j >= i; j++)
    47     {
    48         sqList.elem[j + 1] = sqList.elem[j];
    49     }
    50 
    51     if(i==101)
    52     {
    53         printf("%d
    ",sqList.length);//100
    54     }
    55     sqList.elem[i - 1] = e;
    56     if(i==101)
    57     {
    58         printf("%d
    ",sqList.length);//101
    59     }
    60     ++sqList.length;
    61     if(i==101)
    62     {
    63         printf("%d
    ",sqList.length);//102
    64     }
    65     return true;
    66 }
    67 
    68 int main()
    69 {
    70     Sq sq;
    71     for (int i = 1; i <= 102; i++)
    72     {
    73         sq.listInsert(i, i);
    74     }
    75     
    76     return 0;
    77 }
    View Code

    当i==10时,sq.listInsert(int,int)方法中,插入之后length自动+1???执行自动操作后又自动+1。
  • 相关阅读:
    分布式事务 小结
    分布式事务的CAP理论 与BASE理论
    乐观锁与悲观锁
    CentOS7中DHCP配置
    pandas 学习(2): pandas 数据结构之DataFrame
    pandas 学习(1): pandas 数据结构之Series
    NumPy 学习(3): 通用函数
    NumPy 学习(2): 数组的操作
    NumPy 学习(1): ndarrays
    在Windows宿主机中连接虚拟机中的Docker容器
  • 原文地址:https://www.cnblogs.com/junlu/p/9494439.html
Copyright © 2011-2022 走看看