1.内存管理:
1.malloc
void *malloc(size_t size);
功能:
申请堆区空间
参数:
size:申请堆区空间的大小
返回值:
返回获得的空间的首地址
失败返回NULL
2.free
void free(void *ptr);
功能:
释放堆区空间
注意:
1.free只能释放堆区空间
2.一个空间只能被free一次,多次free程序会崩溃
3.malloc需要和free搭配使用,如果只有malloc没有free会内存泄露
练习:要求申请堆区空间,将"hello world"存放到堆区空间,完成打印
2.内存溢出:
内存溢出也称为内存越界
3.内存泄露:
程序中malloc的空间没有被释放
4.内存碎片:
由于频繁申请和释放,导致连续的空间分散成一些小的碎片空间,当malloc超过碎片空间时,则无法获得该空间(空间不连续),将这样的空间称为内存碎片
==================================================================================================================================================
链表:
1 #include
2 #include
3 #include
4
5 typedef int DataType;
6
7 typedef struct node
8 {
9 DataType Data;
10 struct node *pNext;
11 }LinkNode;
12
13 LinkNode *CreateLinkList(void)
14 {
15 LinkNode *pTmpNode = NULL;
16
17 pTmpNode = malloc(sizeof(LinkNode));
18 if (NULL == pTmpNode)
19 {
20 return NULL;
21 }
22
23 pTmpNode->pNext = NULL;
24
25 return pTmpNode;
26 }
27
28 int InsertHeadLinkList(LinkNode *pHead, DataType TmpData)
29 {
30 LinkNode *pTmpNode = NULL;
31
32 pTmpNode = malloc(sizeof(LinkNode));
33 if (NULL == pTmpNode)
34 {
35 return -1;
36 }
37
38 pTmpNode->Data = TmpData;
39 pTmpNode->pNext = pHead->pNext;
40 pHead->pNext = pTmpNode;
41
42 return 0;
43 }
44
45 int ShowLinkList(LinkNode *pHead)
46 {
47 LinkNode *pTmpNode = NULL;
48
49 pTmpNode = pHead->pNext;
50 while (pTmpNode != NULL)
51 {
52 printf("%d ", pTmpNode->Data);
53 pTmpNode = pTmpNode->pNext;
54 }
55 printf("\n");
56
57 return 0;
58 }
59
60 int ReplaceLinkList(LinkNode *pHead, DataType OldData, DataType NewData)
61 {
62 LinkNode *pTmpNode = NULL;
63
64 pTmpNode = pHead->pNext;
65 while (pTmpNode != NULL)
66 {
67 if (pTmpNode->Data == OldData)
68 {
69 pTmpNode->Data = NewData;
70 }
71 pTmpNode = pTmpNode->pNext;
72 }
73
74 return 0;
75 }
76
77 int DeleteLinkList(LinkNode *pHead, DataType TmpData)
78 {
79 LinkNode *pPreNode = NULL;
80 LinkNode *pTmpNode = NULL;
81
82 pTmpNode = pHead->pNext;
83 pPreNode = pHead;
84
85 while (pTmpNode != NULL)
86 {
87 if (pTmpNode->Data == TmpData)
88 {
89 pPreNode->pNext = pTmpNode->pNext;
90 free(pTmpNode);
91 pTmpNode = pPreNode->pNext;
92 }
93 else
94 {
95 pTmpNode = pTmpNode->pNext;
96 pPreNode = pPreNode->pNext;
97 }
98 }
99
100 return 0;
101 }
102
103 int DestroyLinkList(LinkNode **ppHead)
104 {
105 LinkNode *pTmpNode = NULL;
106 LinkNode *pFreeNode = NULL;
107
108 pTmpNode = pFreeNode = *ppHead;
109 while (pTmpNode != NULL)
110 {
111 pTmpNode = pTmpNode->pNext;
112 free(pFreeNode);
113 pFreeNode = pTmpNode;
114 }
115 *ppHead = NULL;
116
117 return 0;
118 }
119
120 int InsertTailLinkList(LinkNode *pHead, DataType TmpData)
121 {
122 LinkNode *pTmpNode = NULL;
123 LinkNode *pTailNode = NULL;
124
125 pTailNode = pHead;
126 pTmpNode = malloc(sizeof(LinkNode));
127 if (pTmpNode == NULL)
128 {
129 return -1;
130 }
131 while (pTailNode->pNext != NULL)
132 {
133 pTailNode = pTailNode->pNext;
134 }
135
136 pTmpNode->Data = TmpData;
137 pTmpNode->pNext = NULL;
138 pTailNode->pNext = pTmpNode;
139
140 return 0;
141 }
142 int main(void)
143 {
144 LinkNode *linklist = NULL;
145
146 linklist = CreateLinkList();
147 InsertHeadLinkList(linklist, 1);
148 InsertHeadLinkList(linklist, 2);
149 InsertHeadLinkList(linklist, 3);
150 InsertHeadLinkList(linklist, 4);
151 InsertHeadLinkList(linklist, 5);
152 ShowLinkList(linklist);
153 ReplaceLinkList(linklist, 5, 50);
154 ReplaceLinkList(linklist, 1, 10);
155 ShowLinkList(linklist);
156 DeleteLinkList(linklist, 50);
157 DeleteLinkList(linklist, 10);
158 ShowLinkList(linklist);
159 InsertTailLinkList(linklist,30);
160 ShowLinkList(linklist);
161
162 return 0;
163 }
作业:
1.封装函数在链表中实现尾插法
int InsertTailLinkList(LinkNode *pHead, DataType TmpData);
2.从终端接收一个字符串,将字符串倒置后输出
"how are you"
"you are how"
1 #include
2 #include
3
4 int Inversion(char *phead,char *ptail)
5 {
6 char tmp = 0;
7
8
9 while (phead < ptail)
10 {
11 tmp = *phead;
12 *phead = *ptail;
13 *ptail = tmp;
14 phead++;
15 ptail--;
16 }
17
18 return 0;
19 }
20
21 int main(void)
22 {
23 char str[32] = {0};
24 char *head = NULL;
25 char *tail = NULL;
26 char *p = NULL;
27 char *q = NULL;
28 int i = 0;
29
30 gets(str);
31
32 head = str;
33 tail = str + strlen(str) - 1;
34
35 Inversion(head,tail);
36
37 p = &str[0];
38
39 while (str[i] != '\0')
40 {
41 if (str[i] == ' ')
42 {
43 Inversion(p,&str[i-1]);
44 p = &str[i+1];
45 }
46 i++;
47
48 }
49
50 Inversion(p,&str[i-1]);
51
52 printf("str = %s\n",str);
53
54 return 0;
55 }