|
|
![]() |
|
动态数据结构是一种常用的数据结构,在事先不知道所处理数据容量的情况,用动态数据是一种行之有效的方法,也为许多C语言程序员所采用。在汇编语言中,我们也可以采用动态数据的方式来存储数据,并进行链表的遍历。
为了使读者尽快理解本例的功能,我们把与之相似功能的C语言程序书写如下:
#include <stdio.h> | ||
#include <alloc.h> | ||
struct link { | ||
int data; |
||
struct link *next; |
||
}; |
||
void main( ) | ||
{struct link *head=NULL, *temp, *pt; | ||
int i; | ||
for (i = 20; i > 0; i--) { |
;生成20个结点的链表 | |
temp = (struct link *)calloc(1,sizeof(struct link)); |
;申请一个结点的空间 | |
if (temp == NULL) break; |
;若分配内存失败 |
|
temp->data = i; |
||
temp->next = NULL; |
||
if (head == NULL) head = temp; |
||
else pt->next = temp; |
||
pt = temp; |
||
} |
||
while (head != NULL) { |
;遍历结点并输出其数据字段 | |
printf("%d\n",head->data); |
||
pt = head; head = head->next; free(pt); |
||
} |
||
} |
例10.13 编写一个程序用动态链表存储20,19,……,1,并用遍历链表的方法来显示每个结点的数值。
解:显示解答