-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilm2.c
123 lines (98 loc) · 2.38 KB
/
film2.c
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
112
113
114
115
116
117
118
119
120
121
122
123
//
// Created by liushan on 18-7-28.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zconf.h>
#include "film2.h"
/* 链表 */
void film2(unsigned long num)
{
struct film * head = NULL;
struct film * pre;
struct film * current;
unsigned long i;
// 创建链表
for (i = 0; i < num; i++)
{
// printf("name_%ld %d\n", i, (int ) i % 10000);
if (head == NULL)
{
if ( (head = (struct film * )malloc(sizeof(struct film) ) ) == NULL)
{
fprintf(stderr, "malloc error, now exit!\n");
exit(EXIT_FAILURE);
}
else
{
snprintf(head->name, 44, "name_%ld", i);
head->rating = (int ) i % 10000;
head->next = NULL;
current = head;
}
}
else
{
if ( (current = (struct film * )malloc(sizeof(struct film) ) ) == NULL)
{
fprintf(stderr, "malloc error, now exit!\n");
exit(EXIT_FAILURE);
}
else
{
snprintf(current->name, 44, "name_%ld", i);
current->rating = (int ) i % 10000;
current->next = NULL;
pre->next = current;
}
}
if (i % 2000 == 0)
{
printf("malloc i: %ld\n", i);
sleep(1);
}
pre = current;
}
// 显示链表
current = head;
if (current == NULL)
printf("no data input!\n");
else
printf("movie_name rating\n");
while (current != NULL)
{
printf("%s %d\n", current->name, current->rating);
current = current->next;
}
// 释放链表
current = head;
for (i = 0; current != NULL; i++)
{
head = current->next;
free(current);
current = head;
if (i % 2000 == 0)
{
printf("free i: %ld\n", i);
sleep(1);
}
}
// for (i = 0; i < 100000; i++)
// {
// (struct film * )malloc(sizeof(struct film) );
//
// if (i % 2000 == 0)
// {
// printf("molloc again i: %ld\n", i);
// sleep(1);
// }
// }
//
// while (current != NULL)
// {
// head = current->next;
// free(current);
// current = head;
// }
}