复制代码
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
100
101
102
103
104
105
106
107
108
109
110
111#include<stdio.h> #include<stdlib.h> #include<assert.h> #include<string.h> #include<malloc.h> typedef int DateType; typedef struct SeqList { DateType *arr; size_t capacility; size_t size; }SeqList; //创建空间 void CheckCapa(SeqList *Seq) { assert(Seq); if (Seq->size >= Seq->capacility) { Seq->capacility = 2 * Seq->capacility + 3; Seq->arr = (DateType*)realloc(Seq->arr, Seq->capacility * sizeof(DateType)); } } //初始化动态顺序表 void initSeqList(SeqList *Seq) { Seq->arr = NULL/* malloc(sizeof(DateType) * 3)*/; Seq->capacility = 0; Seq->size = 0; } //销毁空间 void DestroySeq(SeqList *Seq) { Seq->capacility = 0; Seq->size = 0; free(Seq->arr); Seq->arr = NULL; } //尾插 void PushBack(SeqList *Seq,DateType x) { assert(Seq); CheckCapa(Seq); Seq->arr[Seq->size++] = x; } //尾删 void PopBack(SeqList *Seq) { assert(Seq); if (Seq->size <= 0) { printf("当前顺序表为空!无法继续删除n"); return; } --Seq->size; printf("n"); } //打印动态顺序表 void PrintSeq(SeqList *Seq) { assert(Seq); int index = 0; if (Seq->size == 0) { printf("当前顺序表为空!n"); return; } for (index = 0; index < Seq->size; index++) { printf("%d->", Seq->arr[index]); } printf("n"); } void Test() { SeqList Seq; initSeqList(&Seq); PushBack(&Seq,1); PushBack(&Seq, 2); PushBack(&Seq, 3); PushBack(&Seq, 4); PrintSeq(&Seq); PopBack(&Seq); PrintSeq(&Seq); DestroySeq(&Seq); } int main() { Test(); system("pause"); return 0; }
本文出自 “Han Jing's Blog” 博客,请务必保留此出处http://10740184.blog.51cto.com/10730184/1743504
最后
以上就是寒冷便当最近收集整理的关于C语言:【动态顺序表】动态顺序表的初始化、打印、尾插PushBack、尾删PopBack的全部内容,更多相关C语言:【动态顺序表】动态顺序表内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复