《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
5.6 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "list.h"
  4. struct entry {
  5. list_entry_t node;
  6. int num;
  7. };
  8. int main() {
  9. struct entry head;
  10. list_entry_t* p = &head.node;
  11. list_init(p);
  12. head.num = 0;
  13. int i;
  14. for (i = 1; i != 10; i ++) {
  15. struct entry * e = (struct entry *)malloc(sizeof(struct entry));
  16. e->num = i;
  17. list_add(p, &(e->node));
  18. p = list_next(p);
  19. }
  20. //reverse list all node
  21. while ((p = list_prev(p)) != &head.node)
  22. printf("%d\n", ((struct entry *)p)->num);
  23. return 0;
  24. }
  25. // other examples
  26. // ex1
  27. #if 0
  28. include "list.h";
  29. void main() {
  30. node_t node1;
  31. node1.data = 1;
  32. list_entry_t *n1 = &nodes1.node_link;
  33. node_t node2;
  34. node2.data = 2;
  35. list_init(n1);
  36. list_add_after(n1, &nodes2.node_link);
  37. printf("\n");
  38. }
  39. #endif
  40. //ex2
  41. #if 0
  42. #include "list.h"
  43. #include "defs.h"
  44. #include <stdio.h>
  45. struct page {
  46. int test;
  47. list_entry_t page_link;
  48. };
  49. #define le2page(le, member) to_struct((le), struct page, member)
  50. #define to_struct(ptr, type, member) \
  51. ((type *)((char *)(ptr) - offsetof(type, member)))
  52. #define offsetof(type, member) \
  53. ((size_t)(&((type *)0)->member))
  54. typedef struct {
  55. list_entry_t free_list;
  56. unsigned int nr_free;
  57. }free_area_t;
  58. int main(){
  59. free_area_t free_area;
  60. struct page pg;
  61. free_area.free_list.next = &pg.page_link;
  62. pg.test = 1;
  63. pg.page_link.next = &free_area.free_list;
  64. list_entry_t* le = &free_area.free_list;
  65. while ( (le = list_next(le)) != &free_area.free_list ) {
  66. struct page* p = le2page(le, page_link);
  67. printf ( "%d\n", p->test );
  68. }
  69. return 0;
  70. }
  71. #endif
  72. //ex3
  73. #if 0
  74. #include <stdio.h>
  75. #include "list.h"
  76. int main(int argc, const char * argv[])
  77. {
  78. list_entry_t HS0,HS1,HS2,HS3;
  79. list_init(&HS0);
  80. printf("%d %d %d\n",HS0.prev, HS0.next, &HS0);
  81. HS0.prev = NULL;
  82. HS0.next = &HS1;
  83. HS1.prev = &HS0;
  84. HS1.next = &HS2;
  85. HS2.prev = &HS1;
  86. HS2.next = NULL;
  87. list_add(&HS1, &HS3);
  88. printf("%d %d %d\n",HS0.prev, HS0.next, &HS0);
  89. printf("%d %d %d\n",HS1.prev, HS1.next, &HS1);
  90. printf("%d %d %d\n",HS3.prev, HS3.next, &HS3);
  91. printf("%d %d %d\n",HS2.prev, HS2.next, &HS2);
  92. list_del(&HS3);
  93. printf("%d %d %d\n",HS0.prev, HS0.next, &HS0);
  94. printf("%d %d %d\n",HS1.prev, HS1.next, &HS1);
  95. printf("%d %d %d\n",HS2.prev, HS2.next, &HS2);
  96. }
  97. #endif
  98. //ex4
  99. #if 0
  100. //一个简单的随机生成一个长度的链表并计算长度的程序
  101. #include <iostream>
  102. #include <cstdlib>
  103. #include <cstdio>
  104. #include <ctime>
  105. #include "list.h"
  106. using namespace std;
  107. void randlength( list_entry_t *&elm )
  108. {
  109. list_init(elm);
  110. int countt = rand()%100;
  111. printf("the length of the list that will be created: %d\n", countt );
  112. for( int i = 0; i < countt; i++ )
  113. {
  114. list_entry_t * node = new list_entry_t();
  115. list_add_after(elm, node);
  116. }
  117. }
  118. int getlength( list_entry_t *&elm )
  119. {
  120. int countt = 0;
  121. list_entry_t * current_node = elm;
  122. while( current_node->next!=elm )
  123. {
  124. countt ++;
  125. current_node = current_node->next;
  126. }
  127. return countt;
  128. }
  129. int main()
  130. {
  131. srand( (unsigned)time(NULL));
  132. list_entry_t * root = new list_entry_t();
  133. randlength( root );
  134. printf(" the length of this list is %d", getlength(root) );
  135. return 0;
  136. }
  137. #endif
  138. //ex5
  139. #if 0
  140. // compile with -nostdinc and explicitly provide header file directories
  141. #include "list.h"
  142. #include <stdio.h>
  143. struct MyDataType {
  144. list_entry_t list;
  145. int32_t data;
  146. };
  147. struct MyDataType x, y, z;
  148. void display() {
  149. printf("x = %lx prev = %lx next = %lx \n", &x.list, x.list.prev, x.list.next);
  150. printf("y = %lx prev = %lx next = %lx \n", &y.list, y.list.prev, y.list.next);
  151. printf("z = %lx prev = %lx next = %lx \n", &z.list, z.list.prev, z.list.next);
  152. printf("----------------------------------\n");
  153. }
  154. int main() {
  155. // initialize
  156. list_init(&x.list);
  157. list_init(&y.list);
  158. list_init(&z.list);
  159. display();
  160. // insert element
  161. list_add(&x.list, &y.list);
  162. display();
  163. list_add_before(&x.list, &z.list);
  164. display();
  165. // delete element
  166. list_del_init(&x.list);
  167. display();
  168. return 0;
  169. }
  170. #endif
  171. //ex6
  172. #if 0
  173. #include <stdio.h>
  174. #include <list.h>
  175. int main() {
  176. struct list_entry first, second, third;
  177. list_init(&first);
  178. list_init(&second);
  179. list_init(&third);
  180. printf("Is empty:%d\n", list_empty(&first));
  181. list_add_after(&first, &second);
  182. printf("Is empty:%d\n", list_empty(&first));
  183. list_add_before(&first, &third);
  184. struct list_entry *temp = &first;
  185. int num = 0;
  186. while ((temp = list_prev(temp)) != &first)
  187. num++;
  188. printf("Total elem:%d\n", num);
  189. list_del_init(&second);
  190. list_del_init(&first);
  191. printf("Is empty:%d\n", list_empty(&third));
  192. return 0;
  193. }
  194. #endif
  195. //ex7
  196. #if 0
  197. #include <stdio.h>
  198. #include <list.h>
  199. struct Ints {
  200. int data;
  201. list_entry_t le;
  202. };
  203. #define le2struct(ptr) to_struct((ptr), struct Ints, le)
  204. #define to_struct(ptr, type, member) \
  205. ((type *)((char *)(ptr) - offsetof(type, member)))
  206. #define offsetof(type, member) \
  207. ((size_t)(&((type *)0)->member))
  208. int main() {
  209. struct Ints one, two, three, *now_int;
  210. list_entry_t *now;
  211. one.data = 1;
  212. two.data = 2;
  213. three.data = 3;
  214. list_init(&one.le);
  215. list_add_before(&one.le, &two.le);
  216. list_add_after(&one.le, &three.le);
  217. now = &two.le;
  218. while (1) {
  219. now_int = le2struct(now);
  220. printf("Current: %d\n", now_int->data);
  221. now = now->next;
  222. if (now == &two.le)
  223. break;
  224. }
  225. return 0;
  226. }
  227. //输出
  228. //Current: 2
  229. //Current: 1
  230. //Current: 3
  231. #endif