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。
  • 相关阅读:
    [USACO08FEB]酒店Hotel 线段树 BZOJ 1593
    有趣的数 zoj 月赛
    [ZJOI2008]生日聚会 BZOJ1037 dp
    借教室 差分+二分答案
    HackerRank
    旅行计划 记忆化搜索
    灾后重建 Floyd
    [USACO10OCT]湖计数Lake Counting 联通块
    [TJOI2013]循环格 费用流 BZOJ3171
    高斯消元
  • 原文地址:https://www.cnblogs.com/junlu/p/9494439.html
Copyright © 2011-2022 走看看