内存分配的函数

void malloc(unsigned int size);创建长度为size的连续空间,返回一个起始地址的指针,如果创建内存空间失败返回NULL;
void
calloc(unsigned n, unsigned size); 在内存的动态区存储中分配n个长度为size的连续空间,返回一个起始地址的指针,失败返回NULL;
void free(void p); 释放由p指向的内存区,使这部分内存区域能被其他变量使用,p使malloc或calloc函数时的返回值;
注意:在老版本C中,malloc和calloc返回的是char类型的指针,ANSI C返回的是 void
类型;

简单的链表

  1. #include <stdio.h>
  2. struct student {
  3. long num;
  4. float score;
  5. struct student *next;
  6. };
  7. int main() {
  8. struct student a,b,c, *head, *p;
  9. a.num = 10001;
  10. a.score = 100.00;
  11. b.num = 10002;
  12. b.score = 80.00;
  13. c.num = 10003;
  14. c.score = 95.00;
  15. head = &a;
  16. a.next = &b;
  17. b.next = &c;
  18. c.next = NULL;
  19. p = head;
  20. do {
  21. printf("%ld %5.1f\n", p -> num, p -> score);
  22. p = p -> next;
  23. } while(p != NULL);
  24. }

链表

  1. #include<stdlib.h>
  2. #include<string.h>
  3. #include<stdio.h>
  4. #define LEN sizeof(struct student)
  5. struct student {
  6. long num; // 学号
  7. float score;
  8. struct student *next;
  9. };
  10. int n;
  11. struct student *creat(void) { // 创建学生信息,输入0结束
  12. struct student *head;
  13. struct student *p1;
  14. struct student *p2;
  15. n = 0;
  16. p1 = p2 = (struct student *) malloc(LEN); // 开辟一个新单元
  17. scanf("%ld,%f", &p1 -> num, &p1 -> score);
  18. head = NULL;
  19. while (p1 -> num != 0) {
  20. n += 1;
  21. if (n == 1) {
  22. head = p1;
  23. } else {
  24. p2 -> next = p1;
  25. }
  26. p2 = p1;
  27. p1 = (struct student *) malloc(LEN);
  28. scanf("%ld,%f", &p1 -> num, &p1 -> score);
  29. }
  30. p2 -> next = NULL;
  31. return (head);
  32. }
  33. void print(struct student *head) {
  34. struct student *p;
  35. printf("\nNow, These %d records are:\n", n);
  36. p = head;
  37. if (p != NULL) {
  38. do {
  39. printf("%ld %5.1f\n", p -> num, p -> score);
  40. p = p -> next;
  41. } while (p != NULL);
  42. }
  43. }
  44. struct student *del(struct student *head, long num) { // 按学号删除
  45. struct student *p1, *p2;
  46. if (head == NULL) {
  47. printf("\nlist null! \n");
  48. return (head);
  49. }
  50. p1 = head;
  51. while (num != p1 -> num && p1 -> next != NULL) {
  52. p2 = p1;
  53. p1 = p1 -> next;
  54. }
  55. if (num == p1 -> num) {
  56. if (p1 == head) {
  57. head = p1 -> next;
  58. } else {
  59. p2 -> next = p1 -> next;
  60. }
  61. printf("delete: %ld\n", num);
  62. n -= 1;
  63. } else {
  64. printf("%ld not been found!\n", num);
  65. }
  66. return (head);
  67. }
  68. struct student *insert(struct student *head, struct student *stud) {
  69. struct student *p0, *p1, *p2;
  70. p1 = head;
  71. p0 = stud;
  72. if (head == NULL) {
  73. head = p0;
  74. p0 -> next = NULL;
  75. } else {
  76. while ((p0 -> num > p1 -> num) && (p1 -> next != NULL)) {
  77. p2 = p1;
  78. p1 = p1 -> next;
  79. }
  80. if (p0 -> num <= p1 -> num) {
  81. if (head == p1) {
  82. head = p0;
  83. } else {
  84. p2 -> next = p0;
  85. }
  86. p0 -> next = p1;
  87. } else {
  88. p1 -> next = p0;
  89. p0 -> next = NULL;
  90. }
  91. }
  92. n += 1;
  93. return (head);
  94. }
  95. int main(){
  96. struct student *head, *stu;
  97. long del_num;
  98. printf("input records:\n");
  99. head = creat();
  100. print(head);
  101. printf("\ninput the deleter number:");
  102. scanf("%ld", &del_num);
  103. while(del_num != 0) {
  104. head = del(head, del_num);
  105. print(head);
  106. printf("\ninput the deleted number:");
  107. scanf("%ld", &del_num);
  108. }
  109. printf("\ninput the inserted record:");
  110. stu = (struct student *)malloc(LEN);
  111. scanf("%ld,%f", &stu -> num, &stu -> score);
  112. while(stu -> num != 0) {
  113. head = insert(head, stu);
  114. print(head);
  115. printf("\ninput the inserted record:");
  116. stu = (struct student *)malloc(LEN);
  117. scanf("%ld,%f", &stu -> num, &stu -> score);
  118. }
  119. }