结构体的定义与使用:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Student
  4. {
  5. int num;
  6. char name[30];
  7. char age;
  8. };
  9. int main(int argc, char* argv[])
  10. {
  11. struct Student stu = { 1001, "lyshark", 22 };
  12. printf("普通引用: %d --> %s \n", stu.num, stu.name);
  13. struct Student *ptr; // 定义结构指针
  14. ptr = &stu; // 指针的赋值
  15. printf("指针引用: %d --> %s \n", ptr->num, ptr->name);
  16. system("pause");
  17. return 0;
  18. }

动态分配结构体成员:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char* argv[])
  4. {
  5. struct Student
  6. {
  7. int num;
  8. char name[30];
  9. char age;
  10. };
  11. struct Student *stu = malloc(sizeof(struct Student));
  12. stu->num = 1001;
  13. stu->age = 24;
  14. strcpy(stu->name, "lyshark");
  15. printf("姓名: %s 年龄: %d \n", stu->name, stu->age);
  16. // ----------------------------------------------------------
  17. struct Person
  18. {
  19. char *name;
  20. int age;
  21. }person;
  22. struct Person *ptr = &person;
  23. ptr->name = (char *)malloc(sizeof(char)* 20);
  24. strcpy(ptr->name, "lyshark");
  25. ptr->age = 23;
  26. printf("姓名: %s 年龄: %d \n", ptr->name, ptr->age);
  27. free(ptr->name);
  28. system("pause");
  29. return 0;
  30. }

结构体变量数组:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct Person
  4. {
  5. int uid;
  6. char name[64];
  7. }Person;
  8. void Print(struct Person *p,int len)
  9. {
  10. for (int x = 0; x < len; x++)
  11. {
  12. printf("%d \n", p[x].uid);
  13. }
  14. }
  15. int main(int argc, char* argv[])
  16. {
  17. // 栈上分配结构体(聚合初始化)
  18. struct Person p1[] = {
  19. { 1, "aaa" },
  20. { 2, "bbb" },
  21. { 3, "ccc" },
  22. };
  23. int len = sizeof(p1) / sizeof(struct Person);
  24. Print(p1, len);
  25. // 在堆上分配
  26. struct Person *p2 = malloc(sizeof(struct Person) * 5);
  27. for (int x = 0; x < 5; x++)
  28. {
  29. p2[x].uid = x;
  30. strcpy(p2[x].name, "aaa");
  31. }
  32. Print(p2, 5);
  33. system("pause");
  34. return 0;
  35. }

结构体深浅拷贝

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct Person
  4. {
  5. int uid;
  6. char *name;
  7. }Person;
  8. int main(int argc, char* argv[])
  9. {
  10. struct Person p1,p2;
  11. p1.name = malloc(sizeof(char)* 64);
  12. strcpy(p1.name, "admin");
  13. p1.uid = 1;
  14. p2.name = malloc(sizeof(char)* 64);
  15. strcpy(p2.name, "guest");
  16. p2.uid = 2;
  17. // p2 = p1; 浅拷贝
  18. // 深拷贝
  19. if (p1.name != NULL)
  20. {
  21. free(p1.name);
  22. p1.name == NULL;
  23. }
  24. p1.name = malloc(strlen(p2.name) + 1);
  25. strcpy(p2.name, p1.name);
  26. p2.uid = p1.uid;
  27. printf("p2 -> %s \n", p2.name);
  28. system("pause");
  29. return 0;
  30. }

结构体字段排序: 首先对比结构中的UID,通过冒泡排序将UID从小到大排列,也可以通过Name字段进行排序.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Student
  4. {
  5. int uid;
  6. char name[32];
  7. double score;
  8. };
  9. int StructSort(struct Student *stu,int len)
  10. {
  11. for (int x = 0; x < len - 1; x++)
  12. {
  13. for (int y = 0; y < len - x - 1; y++)
  14. {
  15. // if (strcmp(stu[y].name, stu[y + 1].name) > 0)
  16. if (stu[y].uid > stu[y + 1].uid)
  17. {
  18. // 结构体变量互换,将用户UID从小到大排列
  19. struct Student tmp = stu[y];
  20. stu[y] = stu[y + 1];
  21. stu[y+1] = tmp;
  22. }
  23. }
  24. }
  25. return 0;
  26. }
  27. void MyPrint(struct Student *stu,int len)
  28. {
  29. for (int x = 0; x < len; x++)
  30. printf("Uid: %d Name: %s Score: %.1f \n", stu[x].uid,stu[x].name,stu[x].score);
  31. }
  32. int main(int argc, char* argv[])
  33. {
  34. struct Student stu[3] = {
  35. {8,"admin",79.5},
  36. {5,"guest",89.5},
  37. {1,"root",99},
  38. };
  39. StructSort(stu, 3); // 调用排序
  40. MyPrint(stu, 3); // 输出结果
  41. system("pause");
  42. return 0;
  43. }

结构体数据之间的交换:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct Student
  5. {
  6. char *name;
  7. int score[3];
  8. };
  9. int StructExchange(struct Student *stu, int len, char *str1,char *str2)
  10. {
  11. struct Student *ptr1;
  12. struct Student *ptr2;
  13. // 找到两个名字所对应的成绩
  14. for (int x = 0; x < len; ++x)
  15. {
  16. if (!strcmp(stu[x].name, str1))
  17. ptr1 = &stu[x];
  18. if (!strcmp(stu[x].name, str2))
  19. ptr2 = &stu[x];
  20. }
  21. // 开始交换两个人的成绩
  22. for (int y = 0; y < 3; y++)
  23. {
  24. int tmp = ptr1->score[y];
  25. ptr1->score[y] = ptr2->score[y];
  26. ptr2->score[y] = tmp;
  27. }
  28. return 0;
  29. }
  30. void MyPrint(struct Student *stu,int len)
  31. {
  32. for (int x = 0; x < len; x++)
  33. {
  34. printf("Name: %s --> score: %d %d %d \n", stu[x].name, stu[x].score[0], stu[x].score[1], stu[x].score[2]);
  35. }
  36. }
  37. int main(int argc, char* argv[])
  38. {
  39. struct Student stu[3];
  40. // 动态开辟空间,并动态输入姓名与成绩
  41. // admin 1 1 1 / guest 2 2 2 / root 3 3 3
  42. for (int x = 0; x < 3; x++)
  43. {
  44. stu[x].name = (char *)malloc(sizeof(char) * 64); // 开辟空间
  45. scanf("%s%d%d%d", stu[x].name, &stu[x].score[0], &stu[x].score[1], &stu[x].score[2]);
  46. }
  47. MyPrint(&stu, 3);
  48. // 开始交换两个人名的成绩
  49. StructExchange(&stu, 3, "root", "admin");
  50. printf("----------------------------\n");
  51. MyPrint(&stu, 3);
  52. // 动态内存的释放
  53. for (int y = 0; y < 3; y++)
  54. free(stu[y].name);
  55. system("pause");
  56. return 0;
  57. }

结构体偏移量计算:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4. int main(int argc, char* argv[])
  5. {
  6. struct Student
  7. {
  8. int uid;
  9. char *name;
  10. };
  11. struct Student stu = { 1, "lyshark" };
  12. int offset = (int *)( (char *)&stu + offsetof(struct Student, name) );
  13. printf("指针首地址: %x \n", offset);
  14. // =================================================================
  15. // 第二种嵌套结构体取地址
  16. struct SuperClass
  17. {
  18. int uid;
  19. char *name;
  20. struct stu
  21. {
  22. int sid;
  23. char *s_name;
  24. }stu;
  25. };
  26. struct SuperClass super = { 1001, "lyshark" ,1,"xiaowang"};
  27. int offset1 = offsetof(struct SuperClass, stu);
  28. int offset2 = offsetof(struct stu, sid);
  29. // SuperClass + stu 找到 sid 首地址
  30. int struct_offset = ((char *)&super + offset1) + offset2;
  31. printf("sid首地址: %x --> %x \n", struct_offset, &super.stu.sid);
  32. int stu_sid = *(int *) ((char *)&super + offset1) + offset2;
  33. printf("sid里面的数值是: %d \n", stu_sid);
  34. int stu_sid_struct = ((struct stu *)((char *)&super + offset1))->sid;
  35. printf("sid里面的数值是: %d \n", stu_sid_struct);
  36. system("pause");
  37. return 0;
  38. }

结构体嵌套一级指针

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct Person
  4. {
  5. int id;
  6. char *name;
  7. int age;
  8. }Person;
  9. // 分配内存空间,每一个二级指针中存放一个一级指针
  10. struct Person ** allocateSpace()
  11. {
  12. // 分配3个一级指针,每一个指针指向一个结构首地址
  13. struct Person **tmp = malloc(sizeof(struct Person *) * 3);
  14. for (int x = 0; x < 3; x++)
  15. {
  16. tmp[x] = malloc(sizeof(struct Person)); // (真正的)分配一个存储空间
  17. tmp[x]->name = malloc(sizeof(char) * 64); // 分配存储name的空间
  18. sprintf(tmp[x]->name, "name_%d", x);
  19. tmp[x]->id = x;
  20. tmp[x]->age = x + 10;
  21. }
  22. return tmp;
  23. }
  24. // 循环输出数据
  25. void MyPrint(struct Person **person)
  26. {
  27. for (int x = 0; x < 3; x++)
  28. {
  29. printf("Name: %s \n", person[x]->name);
  30. }
  31. }
  32. // 释放内存空间,从后向前,从小到大释放
  33. void freeSpace(struct Person **person)
  34. {
  35. if (person != NULL)
  36. {
  37. for (int x = 0; x < 3; x++)
  38. {
  39. if (person[x]->name != NULL)
  40. {
  41. printf("%s 内存被释放 \n",person[x]->name);
  42. free(person[x]->name);
  43. person[x]->name = NULL;
  44. }
  45. free(person[x]);
  46. person[x] = NULL;
  47. }
  48. free(person);
  49. person = NULL;
  50. }
  51. }
  52. int main(int argc, char* argv[])
  53. {
  54. struct Person **person = NULL;
  55. person = allocateSpace();
  56. MyPrint(person);
  57. freeSpace(person);
  58. system("pause");
  59. return 0;
  60. }

结构体嵌套二级指针

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Student
  4. {
  5. char * name;
  6. }Student;
  7. struct Teacher
  8. {
  9. char *name;
  10. char **student;
  11. }Teacher;
  12. void allocateSpace(struct Teacher ***ptr)
  13. {
  14. // 首先分配三个二级指针,分别指向三个老师的结构首地址
  15. struct Teacher **teacher_ptr = malloc(sizeof(struct Teacher *) * 3);
  16. for (int x = 0; x < 3; x++)
  17. {
  18. // 先来分配老师姓名存储字符串,然后赋初值
  19. teacher_ptr[x] = malloc(sizeof(struct Teacher)); // 给teacher_ptr整体分配空间
  20. teacher_ptr[x]->name = malloc(sizeof(char)* 64); // 给teacher_ptr里面的name分配空间
  21. sprintf(teacher_ptr[x]->name, "teacher_%d", x); // 分配好空间之后,将数据拷贝到name里面
  22. // -------------------------------------------------------------------------------------
  23. // 接着分配该老师管理的学生数据,默认管理四个学生
  24. teacher_ptr[x]->student = malloc(sizeof(char *) * 4); // 给teacher_ptr 里面的student分配空间
  25. for (int y = 0; y < 4; y++)
  26. {
  27. teacher_ptr[x]->student[y] = malloc(sizeof(char) * 64);
  28. sprintf(teacher_ptr[x]->student[y], "%s_stu_%d", teacher_ptr[x]->name, y);
  29. }
  30. }
  31. // 最后将结果抛出去
  32. *ptr = teacher_ptr;
  33. }
  34. // 输出老师和学生数据
  35. void MyPrint(struct Teacher **ptr)
  36. {
  37. for (int x = 0; x < 3; x++)
  38. {
  39. printf("老师姓名: %s \n", ptr[x]->name);
  40. for (int y = 0; y < 4; y++)
  41. {
  42. printf("--> 学生: %s \n", ptr[x]->student[y]);
  43. }
  44. }
  45. }
  46. // 最后释放内存
  47. void freeSpace(struct Teacher **ptr)
  48. {
  49. for (int x = 0; x < 3; x++)
  50. {
  51. if (ptr[x]->name != NULL)
  52. {
  53. free(ptr[x]->name);
  54. ptr[x]->name = NULL;
  55. }
  56. for (int y = 0; y < 4; y++)
  57. {
  58. if (ptr[x]->student[y] != NULL)
  59. {
  60. free(ptr[x]->student[y]);
  61. ptr[x]->student[y] = NULL;
  62. }
  63. }
  64. free(ptr[x]->student);
  65. ptr[x]->student = NULL;
  66. }
  67. }
  68. int main(int argc, char* argv[])
  69. {
  70. struct Teacher **teacher_ptr = NULL;
  71. allocateSpace(&teacher_ptr);
  72. MyPrint(teacher_ptr);
  73. freeSpace(teacher_ptr);
  74. system("pause");
  75. return 0;
  76. }

结构体内嵌共用体:

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. struct Person
  5. {
  6. int uid; // 编号
  7. char name[20]; // 姓名
  8. char jobs; // 老师=t 或 学生 = s
  9. union
  10. {
  11. char stu_class[32]; // 学生所在班级
  12. char tea_class[32]; // 老师的所教课程
  13. }category;
  14. };
  15. int main(int argc, char* argv[])
  16. {
  17. struct Person person[3];
  18. for (int x = 0; x < 3; x++)
  19. {
  20. // 首先输入前三项,因为这三个数据是通用的,老师学生都存在的属性
  21. printf("输入: ID 姓名 工作类型(s/t) \n");
  22. scanf("%d %s %c", &person[x].uid, &person[x].name, &person[x].jobs);
  23. if (person[x].jobs == 's') // 如果是学生,输入stu_class
  24. scanf("%s", person[x].category.stu_class);
  25. if (person[x].jobs == 't') // 如果是老师,输入tea_class
  26. scanf("%s", person[x].category.tea_class);
  27. }
  28. printf("--------------------------------------------------------------\n");
  29. for (int y = 0; y < 3; y++)
  30. {
  31. if (person[y].jobs == 's')
  32. printf("老师: %s 职务: %s \n", person[y].name, person[y].category.tea_class);
  33. if (person[y].jobs == 't')
  34. printf("学生: %s 班级: %s \n", person[y].name, person[y].category.stu_class);
  35. }
  36. system("pause");
  37. return 0;
  38. }

结构体与链表

结构体基本定义:

  1. #include <stdio.h>
  2. typedef struct Person
  3. {
  4. int uid;
  5. char name[64];
  6. }Person;
  7. int main(int argc, char* argv[])
  8. {
  9. // 在栈上分配空间
  10. struct Person s1 = { 100, "admin" };
  11. printf("%s \n", s1.name);
  12. // 在堆上分配空间
  13. struct Person *s2 = malloc(sizeof(struct Person));
  14. strcpy(s2->name, "lyshark");
  15. printf("%s \n", s2->name);
  16. system("pause");
  17. return 0;
  18. }

结构体变量数组:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct Person
  4. {
  5. int uid;
  6. char name[64];
  7. }Person;
  8. void Print(struct Person *p,int len)
  9. {
  10. for (int x = 0; x < len; x++)
  11. {
  12. printf("%d \n", p[x].uid);
  13. }
  14. }
  15. int main(int argc, char* argv[])
  16. {
  17. // 栈上分配结构体(聚合初始化)
  18. struct Person p1[] = {
  19. { 1, "aaa" },
  20. { 2, "bbb" },
  21. { 3, "ccc" },
  22. };
  23. int len = sizeof(p1) / sizeof(struct Person);
  24. Print(p1, len);
  25. // 在堆上分配
  26. struct Person *p2 = malloc(sizeof(struct Person) * 5);
  27. for (int x = 0; x < 5; x++)
  28. {
  29. p2[x].uid = x;
  30. strcpy(p2[x].name, "aaa");
  31. }
  32. Print(p2, 5);
  33. system("pause");
  34. return 0;
  35. }

结构体深浅拷贝

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct Person
  4. {
  5. int uid;
  6. char *name;
  7. }Person;
  8. int main(int argc, char* argv[])
  9. {
  10. struct Person p1,p2;
  11. p1.name = malloc(sizeof(char)* 64);
  12. strcpy(p1.name, "admin");
  13. p1.uid = 1;
  14. p2.name = malloc(sizeof(char)* 64);
  15. strcpy(p2.name, "guest");
  16. p2.uid = 2;
  17. // p2 = p1; 浅拷贝
  18. // 深拷贝
  19. if (p1.name != NULL)
  20. {
  21. free(p1.name);
  22. p1.name == NULL;
  23. }
  24. p1.name = malloc(strlen(p2.name) + 1);
  25. strcpy(p2.name, p1.name);
  26. p2.uid = p1.uid;
  27. printf("p2 -> %s \n", p2.name);
  28. system("pause");
  29. return 0;
  30. }

结构体嵌套一级指针

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct Person
  4. {
  5. int id;
  6. char *name;
  7. int age;
  8. }Person;
  9. // 分配内存空间,每一个二级指针中存放一个一级指针
  10. struct Person ** allocateSpace()
  11. {
  12. // 分配3个一级指针,每一个指针指向一个结构首地址
  13. struct Person **tmp = malloc(sizeof(struct Person *) * 3);
  14. for (int x = 0; x < 3; x++)
  15. {
  16. tmp[x] = malloc(sizeof(struct Person)); // (真正的)分配一个存储空间
  17. tmp[x]->name = malloc(sizeof(char) * 64); // 分配存储name的空间
  18. sprintf(tmp[x]->name, "name_%d", x);
  19. tmp[x]->id = x;
  20. tmp[x]->age = x + 10;
  21. }
  22. return tmp;
  23. }
  24. // 循环输出数据
  25. void MyPrint(struct Person **person)
  26. {
  27. for (int x = 0; x < 3; x++)
  28. {
  29. printf("Name: %s \n", person[x]->name);
  30. }
  31. }
  32. // 释放内存空间,从后向前,从小到大释放
  33. void freeSpace(struct Person **person)
  34. {
  35. if (person != NULL)
  36. {
  37. for (int x = 0; x < 3; x++)
  38. {
  39. if (person[x]->name != NULL)
  40. {
  41. printf("%s 内存被释放 \n",person[x]->name);
  42. free(person[x]->name);
  43. person[x]->name = NULL;
  44. }
  45. free(person[x]);
  46. person[x] = NULL;
  47. }
  48. free(person);
  49. person = NULL;
  50. }
  51. }
  52. int main(int argc, char* argv[])
  53. {
  54. struct Person **person = NULL;
  55. person = allocateSpace();
  56. MyPrint(person);
  57. freeSpace(person);
  58. system("pause");
  59. return 0;
  60. }

结构体嵌套二级指针

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Student
  4. {
  5. char * name;
  6. }Student;
  7. struct Teacher
  8. {
  9. char *name;
  10. char **student;
  11. }Teacher;
  12. void allocateSpace(struct Teacher ***ptr)
  13. {
  14. // 首先分配三个二级指针,分别指向三个老师的结构首地址
  15. struct Teacher **teacher_ptr = malloc(sizeof(struct Teacher *) * 3);
  16. for (int x = 0; x < 3; x++)
  17. {
  18. // 先来分配老师姓名存储字符串,然后赋初值
  19. teacher_ptr[x] = malloc(sizeof(struct Teacher)); // 给teacher_ptr整体分配空间
  20. teacher_ptr[x]->name = malloc(sizeof(char)* 64); // 给teacher_ptr里面的name分配空间
  21. sprintf(teacher_ptr[x]->name, "teacher_%d", x); // 分配好空间之后,将数据拷贝到name里面
  22. // -------------------------------------------------------------------------------------
  23. // 接着分配该老师管理的学生数据,默认管理四个学生
  24. teacher_ptr[x]->student = malloc(sizeof(char *) * 4); // 给teacher_ptr 里面的student分配空间
  25. for (int y = 0; y < 4; y++)
  26. {
  27. teacher_ptr[x]->student[y] = malloc(sizeof(char) * 64);
  28. sprintf(teacher_ptr[x]->student[y], "%s_stu_%d", teacher_ptr[x]->name, y);
  29. }
  30. }
  31. // 最后将结果抛出去
  32. *ptr = teacher_ptr;
  33. }
  34. // 输出老师和学生数据
  35. void MyPrint(struct Teacher **ptr)
  36. {
  37. for (int x = 0; x < 3; x++)
  38. {
  39. printf("老师姓名: %s \n", ptr[x]->name);
  40. for (int y = 0; y < 4; y++)
  41. {
  42. printf("--> 学生: %s \n", ptr[x]->student[y]);
  43. }
  44. }
  45. }
  46. // 最后释放内存
  47. void freeSpace(struct Teacher **ptr)
  48. {
  49. for (int x = 0; x < 3; x++)
  50. {
  51. if (ptr[x]->name != NULL)
  52. {
  53. free(ptr[x]->name);
  54. ptr[x]->name = NULL;
  55. }
  56. for (int y = 0; y < 4; y++)
  57. {
  58. if (ptr[x]->student[y] != NULL)
  59. {
  60. free(ptr[x]->student[y]);
  61. ptr[x]->student[y] = NULL;
  62. }
  63. }
  64. free(ptr[x]->student);
  65. ptr[x]->student = NULL;
  66. }
  67. }
  68. int main(int argc, char* argv[])
  69. {
  70. struct Teacher **teacher_ptr = NULL;
  71. allocateSpace(&teacher_ptr);
  72. MyPrint(teacher_ptr);
  73. freeSpace(teacher_ptr);
  74. system("pause");
  75. return 0;
  76. }

静态链表 理解一下

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // 定义链表节点类型
  4. struct LinkNode
  5. {
  6. int data;
  7. struct LinkNode *next;
  8. };
  9. int main(int argc, char* argv[])
  10. {
  11. struct LinkNode node1 = { 10, NULL };
  12. struct LinkNode node2 = { 20, NULL };
  13. struct LinkNode node3 = { 30, NULL };
  14. struct LinkNode node4 = { 40, NULL };
  15. node1.next = &node2;
  16. node2.next = &node3;
  17. node3.next = &node4;
  18. node4.next = NULL;
  19. // 遍历链表结构
  20. struct LinkNode *ptr = &node1;
  21. while (ptr != NULL)
  22. {
  23. printf("%d \n", ptr->data);
  24. ptr = ptr->next;
  25. }
  26. system("pause");
  27. return 0;
  28. }

动态链表

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // 定义链表节点类型
  4. struct LinkNode
  5. {
  6. int data;
  7. struct LinkNode *next;
  8. };
  9. struct LinkNode *init_link()
  10. { // 创建一个头结点,头结点不需要添加任何数据
  11. struct LinkNode *header = malloc(sizeof(struct LinkNode));
  12. header->data = 0;
  13. header->next = NULL;
  14. struct LinkNode *p_end = header; // 创建一个尾指针
  15. int val = -1;
  16. while (1)
  17. {
  18. scanf("%d", &val); // 输入插入的数据
  19. if (val == -1) // 如果输入-1说明输入结束了
  20. break;
  21. // 先创建新节点
  22. struct LinkNode *newnode = malloc(sizeof(struct LinkNode));
  23. newnode->data = val;
  24. newnode->next = NULL;
  25. // 将节点插入到链表中
  26. p_end->next = newnode;
  27. // 更新尾部指针指向
  28. p_end = newnode;
  29. }
  30. return header;
  31. }
  32. // 遍历链表
  33. int foreach_link(struct LinkNode *header)
  34. {
  35. if (NULL == header || header->next == NULL)
  36. return 0;
  37. while (header->next != NULL)
  38. {
  39. printf("%d \n", header->data);
  40. header = header->next;
  41. }
  42. return 1;
  43. }
  44. // 在header节点中oldval插入数据
  45. void insert_link(struct LinkNode *header,int oldval,int newval)
  46. {
  47. struct LinkNode *pPrev = header;
  48. struct LinkNode *Current = pPrev->next;
  49. if (NULL == header)
  50. return;
  51. while (Current != NULL)
  52. {
  53. if (Current->data == oldval)
  54. break;
  55. pPrev = Current;
  56. Current = Current->next;
  57. }
  58. // 如果值不存在则默认插入到尾部
  59. //if (Current == NULL)
  60. // return;
  61. // 创建新节点
  62. struct LinkNode *newnode = malloc(sizeof(struct LinkNode));
  63. newnode->data = newval;
  64. newnode->next = NULL;
  65. // 新节点插入到链表中
  66. newnode->next = Current;
  67. pPrev->next = newnode;
  68. }
  69. // 清空链表
  70. void clear_link(struct LinkNode *header)
  71. {
  72. // 辅助指针
  73. struct LinkNode *Current = header->next;
  74. while (Current != NULL)
  75. {
  76. // 保存下一个节点地址
  77. struct LinkNode *pNext = Current->next;
  78. printf("清空数据: %d \n", Current->data);
  79. free(Current);
  80. Current = pNext;
  81. }
  82. header->next = NULL;
  83. }
  84. // 删除值为val的节点
  85. int remove_link(struct LinkNode *header, int delValue)
  86. {
  87. if (NULL == header)
  88. return;
  89. // 设置两个指针,指向头结点和尾结点
  90. struct LinkNode *pPrev = header;
  91. struct LinkNode *Current = pPrev->next;
  92. while (Current != NULL)
  93. {
  94. if (Current->data == delValue)
  95. {
  96. // 删除节点的过程
  97. pPrev->next = Current->next;
  98. free(Current);
  99. Current = NULL;
  100. }
  101. }
  102. // 移动两个辅助指针
  103. pPrev = Current;
  104. Current = Current->next;
  105. }
  106. // 销毁链表
  107. void destroy_link(struct LinkNode *header)
  108. {
  109. if (NULL == header)
  110. return;
  111. struct LinkNode *Curent = header;
  112. while (Curent != NULL)
  113. {
  114. // 先来保存一下下一个节点地址
  115. struct LinkNode *pNext = Curent->next;
  116. free(Curent);
  117. // 指针向后移动
  118. Curent = pNext;
  119. }
  120. }
  121. // 反响排序
  122. void reverse_link(struct LinkNode *header)
  123. {
  124. if (NULL == header)
  125. return;
  126. struct LinkNode *pPrev = NULL;
  127. struct LinkNode *Current = header->next;
  128. struct LinkNode * pNext = NULL;
  129. while (Current != NULL)
  130. {
  131. pNext = Current->next;
  132. Current->next = pPrev;
  133. pPrev = Current;
  134. Current = pNext;
  135. }
  136. header->next = pPrev;
  137. }
  138. int main(int argc, char* argv[])
  139. {
  140. struct LinkNode * header = init_link();
  141. reverse_link(header);
  142. foreach_link(header);
  143. clear_link(header);
  144. system("pause");
  145. return 0;
  146. }

C/C++ 结构体与指针笔记的更多相关文章

  1. 【学习笔记】【C语言】指向结构体的指针

    1.指向结构体的指针的定义 struct Student *p;  2.利用指针访问结构体的成员 1> (*p).成员名称 2> p->成员名称 3.代码 #include < ...

  2. c语言中较常见的由内存分配引起的错误_内存越界_内存未初始化_内存太小_结构体隐含指针

    1.指针没有指向一块合法的内存 定义了指针变量,但是没有为指针分配内存,即指针没有指向一块合法的内浅显的例子就不举了,这里举几个比较隐蔽的例子. 1.1结构体成员指针未初始化 struct stude ...

  3. C#将结构体和指针互转的方法

    . 功能及位置 将数据从托管对象封送到非托管内存块,属于.NET Framework 类库 命名空间:System.Runtime.InteropServices 程序集:mscorlib(在 msc ...

  4. C语言结构体和指针

    指针也可以指向一个结构体,定义的形式一般为: struct 结构体名 *变量名; 下面是一个定义结构体指针的实例: struct stu{ char *name; //姓名 int num; //学号 ...

  5. 37深入理解C指针之---结构体与指针

    一.结构体与指针 1.结构体的高级初始化.结构体的销毁.结构体池的应用 2.特征: 1).为了避免含有指针成员的结构体指针的初始化复杂操作,将所有初始化动作使用函数封装: 2).封装函数主要实现内存的 ...

  6. 深入了解Windows句柄到底是什么(句柄是逻辑指针,或者是指向结构体的指针,图文并茂,非常清楚)good

    总是有新入门的Windows程序员问我Windows的句柄到底是什么,我说你把它看做一种类似指针的标识就行了,但是显然这一答案不能让他们满意,然后我说去问问度娘吧,他们说不行网上的说法太多还难以理解. ...

  7. 01.C语言关于结构体的学习笔记

    我对于学习的C语言的结构体做一个小的学习总结,总结如下: 结构体:structure 结构体是一种用户自己建立的数据类型,由不同类型数据组成的组合型的数据结构.在其他高级语言中称为记录(record) ...

  8. 关于C语言结构体,指针,声明的详细讲解。——Arvin

    关于结构体的详细分析 只定义结构体 struct Student { int age; char* name; char sex;//结构体成员 };//(不要忘记分号) Student是结构体的名字 ...

  9. c语言结构体&常指针和常量指针的区别

    结构体: 关系密切但数据类型不尽相同, 常指针和常量指针的区别: char * const cp : 定义一个指向字符的指针常数,即const指针,常指针. const char* p : 定义一个指 ...

  10. C#调用c++Dll 结构体数组指针的问题

    参考文章http://blog.csdn.net/jadeflute/article/details/5684687 但是这里面第一个方案我没有测试成功,第二个方案我感觉有点复杂. 然后自己写啦一个: ...

随机推荐

  1. Docker--简介&&安装

    Docker 是一种应用容器引擎 一 容器 Linux系统提供了Namespace和Cgroup技术实现环境隔离和资源控制 其中Namespace是Linux提供的一种内核级别环境隔离的方法,能使一个 ...

  2. Java字节码与反射机制

    字节码(Byte Code)是Java语言跨平台特性的重要保障,也是反射机制的重要基础.通过反射机制,我们不仅能看到一个类的属性和方法,还能在一个类里调用另外一个类的方法,但前提是我们得有相关类的字节 ...

  3. Java面试——数据库知识点

    MySQL 1.建 主键:数据库表中对储存数据对象予以唯一和完整标识的数据列或属性的组合.一个数据列只能有一个主键,且主键的取值不能缺失,即不能为空值(Null). 超键:在关系中能唯一标识元组的属性 ...

  4. 每天学五分钟 Liunx 1000 | 软件篇:源码安装

    软件安装流程 前面软件篇提到了通过 RPM 和 YUM 在线安装的机制安装软件,除了这两种方式之外还有一种通过源码来安装软件的方式.

  5. 【Altium Designer】五颜六色标识的PCB布板(增强PCB可视化特性)

    出现上图中五颜六色的网络标识,对比各个网络会更加清晰,实现步骤如下 打开或关闭  View--->Net Color Override Active   快捷键     F5 设置 displa ...

  6. 2023第十四届极客大挑战 — PWN WP

    WP可能有点简陋,因为是直接从docx导入到博客的,实在不想再重新写了!大家凑合着看吧!哈哈哈,问题不大! pwn方向出自:队友 nc_pwntools 只要过了chal1和chal2即可执行任意命令 ...

  7. 如何让pc端网站在手机上可以等比缩放的整个显示

      将 头部标签的  <meta name="viewport" content="width=device-width, initial-scale=1.0&qu ...

  8. [转帖]oracle 审计日志清理

    https://www.cnblogs.com/bangchen/p/7268086.html   --进入审计日志目录: cd $ORACLE_BASE/admin/$ORACLE_SID/adum ...

  9. [转帖]Jmeter之界面语言设置

    https://developer.aliyun.com/article/1173114#:~:text=%E6%B0%B8%E4%B9%85%E6%80%A7%E8%AE%BE%E7%BD%AE%E ...

  10. [转帖]linux 内核协议栈 TCP time_wait 原理、配置、副作用

    https://my.oschina.net/u/4087916/blog/3051356   0. 手把手教你做中间件.高性能服务器.分布式存储技术交流群 手把手教你做中间件.高性能服务器.分布式存 ...