可以用C语言编程

来源:百度知道 编辑:UC知道 时间:2024/05/25 10:10:16
语言编程用随机函数rand(),产生50个随机整数,并依产生的次序放入一数组中用随机函数rand(),产生50个随机整数,并依产生的次序放入一数组中。用随机函数rand(),产生50个随机整数,并依产生的次序放入一数组中。
定义一个有序线性表,其最大容量为50,初始时为空。将数组中的数据,陆续插入到此线性表中,并要求每次插入后保持线性表的有序性。然后,将此有序线性表打印输出。
依数据在数组中的次序将线性表中的元素逐个删除,直至表空为止。

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>

typedef struct _Node
{
int num;
struct _Node *next;
} Node;

void main()
{
int i, rnum[50];
Node *head, *tail, *node;

srand((unsigned)time(NULL));
for (i = 0; i < 50; i++)
{
rnum[i] = rand();
}

for (i = 0; i < 50; i++)
{
node = (Node *)malloc(sizeof(Node));
node->num = rnum[i];
node->next = NULL;
if (i == 0)
{
head = node;
tail = node;
}
else
{
tail->next = node;
tail = node;
}
}

node = head;
i = 1;
while (node)
{
printf("%d: %d\n", i++, node->num);
node = node->next;
}

while (head)
{
node = head->next;
free(head);
head = node;