【问题】用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\n",sqList.length);//100
54     }
55     sqList.elem[i - 1] = e;
56     if(i==101)
57     {
58         printf("%d\n",sqList.length);//101
59     }
60     ++sqList.length;
61     if(i==101)
62     {
63         printf("%d\n",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。
posted @ 2018-08-17 17:21  junlu  阅读(417)  评论(0编辑  收藏  举报