-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotai_Arr.cpp
More file actions
100 lines (88 loc) · 1.58 KB
/
Dotai_Arr.cpp
File metadata and controls
100 lines (88 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Dotai_Arr.cpp : 定义控制台应用程序的入口点。
//动态数组
#include "stdafx.h"
#include<stdio.h>
#include<malloc.h>
#include<string>
#include<stdlib.h>
#include<iostream>
#define ElemType int
#define LIST_INIT_SIZE (100)
#define LISTINCREMENT (10)
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
int InitList_Sq(SqList *l)
{
l->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));//分配内存
if (! l->elem)//内存分配成功
{
printf("ERROR!!!");
exit(EOVERFLOW);
}
l->length = 0;
l->listsize = LIST_INIT_SIZE;
return 1;
}
void ReMallocMemory(SqList *l)
{
int i;
ElemType *temp_elem;
ElemType *new_temp_elem;
ElemType *delete_elem;
delete_elem = l->elem;
if (l->length == l->listsize)
{
temp_elem = (ElemType *)malloc((l->listsize + LISTINCREMENT) * sizeof(ElemType));//分配内存
new_temp_elem = temp_elem;
for (i = 0; i < l->length; i++)
{
*temp_elem = *l->elem;
temp_elem++;
l->elem++;
}
free(delete_elem);
l->elem = new_temp_elem;
l->listsize = l->listsize + LISTINCREMENT;
}
}
void AddDataToListSql(SqList *l, ElemType num)
{
if (l->length < l->listsize)
{
*(l->elem + l->length) = num;
l->length += 1;
}
else
{
ReMallocMemory(l);
*(l->elem + l->length) = num;
l->length += 1;
}
}
int main()
{
int i,num;
SqList l;
l.elem = NULL;
l.length = 0;
l.listsize = 0;
num = InitList_Sq(&l);
if (1 == num)
{
for (i = 0; i < 108; i++)
{
AddDataToListSql(&l, i);
}
for (i = 0; i < l.length; i++)
{
std::cout << *l.elem << std::endl;
l.elem++;
}
}
std::cin >> i;
return 0;
}