|
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 |
<br><br>栈的创建,入栈,打印栈内数据<br>#include <stdio.h>#include <stdlib.h>typedef int
StackElementType;typedef struct
node {StackElementType data;struct
node *next;}LinkStackNode;typedef LinkStackNode *LinkStack;void
InitStack(LinkStack *s){*s = (LinkStack)malloc(sizeof(LinkStackNode));LinkStack s1 = *s;s1->data = -1;s1->next = NULL;}void
push(LinkStack *s){int
x;LinkStack s1;LinkStack s2 = *s;if(s2->next == NULL){s1 = (LinkStackNode *)malloc(sizeof(LinkStackNode));scanf("%d", &x);if(x == -1){free(s1);return;} s1->data = x;s1->next = NULL;s2->next = s1;}while(1){s1 = (LinkStackNode *)malloc(sizeof(LinkStackNode));scanf("%d", &x);if(x == -1){free(s1);return;}s1->data = x;s1->next = s2->next;s2->next = s1;}}void
print(LinkStack *s){LinkStack s1 = *s;s1 = s1->next;while(s1 != NULL){printf("%d ", s1->data);s1 = s1->next;}}int
main(){LinkStack s;InitStack(&s);push(&s);print(&s);} |
|
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 |
//创建队列并且进行初始化<br>#include <stdio.h>#include <stdlib.h>typedef int
QueueElementType;typedef struct
Node { QueueElementType data; struct
Node *next;}LinkQueueNode;typedef struct{ LinkQueueNode *front; LinkQueueNode *rear;}LinkQueue;int
initQueue(LinkQueue *q){ int
x; LinkQueue *q2 = q; LinkQueueNode *q3; LinkQueueNode * q1 = (LinkQueueNode *)malloc(sizeof(LinkQueueNode)); scanf("%d", &x); if(x == -1) { (q)->front = NULL; (q)->rear = NULL; } q1->data = x; q1->next = NULL; q2->front = q1; q2->rear = q1; while(1) { scanf("%d", &x); if(x == -1) return; q3 = (LinkQueueNode *)malloc(sizeof(LinkQueueNode)); q3->data = x; q1->next = q3; q1 = q1->next; q3->next = NULL; (q)->rear = q3; printf("aaaa "); }}void
print(LinkQueue *q){ LinkQueueNode *q1 = q->front; for(; q1 != NULL; q1 = q1->next) { printf("%d", q1->data); } return
;}int
main(){ LinkQueue q; initQueue(&q);// enterQueue(&q); print(&q);} |
栈区(stack区):由编译器创建和释放,存放函数的参数值,局部变量值,起操作方式类似于数据结构中的栈。
堆区(heap):由程序员创建和释放,若程序员不释放,则由操作系统收回。
全局区:全局变量和静态变量是存储在一起的。
栈:只要剩余栈的空间大于申请栈的空间,系统则为程序提供内存,否则,将报异常栈溢出。
堆:操作系统有一个记录空闲内存地址的链表,当系统收到程序申请时,会遍历该链表,寻找第一个空间大于所申请的堆节点,然后将该节点从堆节点中删除,并将该节点分配给程序,大多数操作系统会在这块内存空间的首地址记录本次分配的大小,这样代码的delete 语句才能正确的释放本内存空间。
原文:http://www.cnblogs.com/xiongge/p/3594381.html