惠 州 外 围 - 惠 州 的 外 围 资 源

  惠 州 外 围 - 惠 州 的 外 围 资 源 -薇芯【81343628】【81343628】【高端外围资源】【诚信合作,非诚勿扰!】可直接添加 !2 * 详细运行过程: 本程序实现的是对链表的简单的操作,即链表的增 删 改 查 销毁 初始化
  3 * 运用面向对象的思想,实现一个类op,op中包括了所有的链表操作方法
  4 * 其他的程序调用op类,实现对表链表的操作
  5 * 链表包括
  6 * 面向对象,简单易学程序更加紧凑,更加健壮,更安全
  7 */
  8 #include<string.h>
  9 #include<stdlib.h>
  10 #include<stdio.h>
  11
  12 #define ok 1
  13 #define err 0
  14 #define null NULL
  15 #define len sizeof(struct student) //结构体student 的长度
  16 #define llen sizeof(struct Op) //结构体op 的长度
  17 /*
  18 *惠 州 外 围 - 惠 州 的 外 围 资 源 -薇芯【81343628】【81343628】【高端外围资源】【诚信合作,非诚勿扰!】可直接添加 ! c语言中的结构体,包括3个简单的数据类型,
  19 * 面向对象中的类,包括3个属性
  20 */
  21 struct student {
  22 char *name;
  23 int age;
  24 struct student *next;
  25 };
  26 /*
  27 * 实现类op,op包含了所有的对链表操作的方法
  28 * 对数据及方法的封装,有保护数据的功能
  29 */
  30 struct Op {
  31 int l; //记录链表的长度
  32 int (*sinit) (struct student * *s); //初始化一个链表
  33 int (*Free) (struct student * *s); //销毁一个链表
  34 int (*selects) (struct student * *s); //遍历一个链表
  35 int (*add1) (struct student * *s, struct student * *stu, int i); //增加一个节点
  36 int (*add2) (struct student * *s, char *name, int age, int i);
  37 //初始化一个链表节点
  38 struct student *(*getstudent) (char *name, int age);
  39 //更新一个链表节点
  40 struct student *(*updateage) (struct student * *s, int age, int i);
  41 struct student *(*updatename) (struct student * *s, char *name,
  42 int i);
  43 struct student *(*updates) (struct student * *s, char *name,
  44 int age, int i);
  45 //删除一个链表节点
  46 struct student *(*deletes) (struct student * *s, int i);
  47 };
  48 struct Op *op; //声明一个op类
  49 //声明链表的操作方法,即链表的增 删 改 查 销毁 初始化
  50 struct student *deletes(struct student * *s, int i);
  51 struct student *updates(struct student * *s, char *name, int age,int i);
  52 struct student *updateage(struct student * *s, int age, int i);
  53 struct student *updatename(struct student * *s, char *name, int i);
  54 struct student *getstudent(char *name, int age);
  55 int add1(struct student * *s, struct student * *stu, int i);
  56 int add2(struct student * *s, char *name, int age, int i);
  57 int selects(struct student * *s);
  58 int sinit(struct student * *s);
  59 int Free(struct student * *s);
  60
  61
  62 int main()
  63 {
  64 struct student *p;
  65 init(&op); //初始化一个OP操作
  66 (*(op->sinit)) (&p); //调用op类实现初始化一个链表的头节点
  67 (*(op->add2)) (&p, "my", 22, 1); //调用op类实现添加为链表一个节点
  68 (*(op->add2)) (&p, "you", 23, 1);
  69 (*(op->add2)) (&p, "she", 24, 1); //调用op类实现添加为链表一个节点
  70 (*(op->add2)) (&p, "he", 25, 1);
  71 printf("---------------------------------------\n");
  72 (*(op->selects)) (&p); //调用op类 遍历链表
  73 (*(op->updates)) (&p, "123", 100, 1); //调用op类 更新一个节点
  74 printf("---------------------------------------\n");
  75 (*(op->selects)) (&p);
  76 (*(op->deletes)) (&p, 2); //调用op类 删除一个节点
  77 printf("---------------------------------------\n");
  78 (*(op->selects)) (&p);
  79 (*(op->Free)) (&p); //调用op类 销毁链表
  80 return ok;
  81 }
  82
  83 //一下内容可以包含在一个头文件中
  84 /*
  85 * 初始化一个链表节点 并为链表节点赋值
  86 * @return 返回一个链表节点 这个节点就是以name和age 为属性的student节点
  87 * @param name age student 的两个属性
  88 */
  89 struct student *getstudent(char *name, int age)
  90 {
  91 struct student *p;
  92 (*(op->sinit)) (&p); //初始化一个链表的节点
  93 p->name = name;
  94 p->age = age;
  95 return p;
  96 }
  97
  98 /*
  99 * 初始化一个op类
  100 * 并对op的方法进行封装
  101 */
  102 int init(struct Op * *op)
  103 {
  104 *op = (struct Op *) malloc(llen);
  105 //对链表的所有的操作进行封装
  106 (*op)->l = 0; //对属性l封装
  107 (*op)->sinit = sinit;
  108 (*op)->Free = Free;
  109 (*op)->selects = selects;
  110 (*op)->add1 = add1;
  111 (*op)->add2 = add2;
  112 (*op)->getstudent = getstudent;
  113 (*op)->updateage = updateage;
  114 (*op)->updatename = updatename;
  115 (*op)->updates = updates;
  116 (*op)->deletes = deletes;
  117 return ok;
  118 }
  119
  120 /*
  121 * 删除一个链表节点
  122 * 并返回删除前的链表节点
  123 */
  124 struct student *deletes(struct student * *s, int i)
  125 {
  126 struct student *p, *student;
  127 p = *s;
  128 if (i > op->l || i < 0) {
  129 printf("请输入正确的数据!\n");
  130 return null;
  131 }
  132 int j = 0;
  133 for (; j < i - 1; j++) {
  134 p = p->next;
  135 }
  136 student = p->next;
  137 p->next = p->next->next;
  138 op->l--;
  139 return student;
  140 }
  141
  142 /*
  143 * 更新链表的数据
  144 * 返回更新前的链表
  145 */
  146 struct student *updates(struct student * *s, char *name, int age, int i)
  147 {
  148 struct student *p;
  149 (*(op->updateage)) (s, age, i);
  150 p = (*(op->updatename)) (s, name, i);
  151 return p;
  152 }
  153
  154 /*
  155 * 更新链表的数据
  156 * 返回更新前的链表
  157 */
  158 struct student *updateage(struct student * *s, int age, int i)
  159 {
  160 struct student *p, *student;
  161 p = *s;
  162 if (i <= 0 || i > op->l) {
  163 printf("请检查你的数据!\n");
  164 return null;
  165 }
  166 int j = 0;
  167 for (; j != i; j++) {
  168 p = p->next;
  169 }
  170 student = p;
  171 p->age = age;
  172 return student;
  173 }
  174
  175 /*
  176 * 更新链表的数据
  177 * 返回更新前的链表
  178 */
  179 struct student *updatename(struct student * *s, char *name, int i)
  180 {
  181 struct student *p, *student;
  182 p = *s;
  183 if (i <= 0 || i > op->l) {
  184 printf("请检查你的数据!\n");
  185 return null;
  186 }
  187 int j = 0;
  188 for (; j != i; j++) {
  189 p = p->next;
  190 }
  191 student = p;
  192 p->name = name;
  193 return student;
  194 }
  195
  196 /*
  197 * 增加一个链表节点
  198 */
  199 int add2(struct student * *s, char *name, int age, int i)
  200 {
  201 struct student *p;
  202 p = (*(op->getstudent)) (name, age);
  203 (*(op->add1)) (s, &p, i);
  204 }
  205
  206 /*
  207 * 增加一个链表节点
  208 */
  209 int add1(struct student * *s, struct student * *stu, int i)
  210 {
  211 struct student *p;
  212 p = *s;
  213 if (i > op->l + 1 || i < 0) {
  214 printf("请检查你的输入!\n");
  215 return err;
  216 }
  217 op->l++;
  218 int j = 0;
  219 for (; j < i - 1; j++) {
  220 p = p->next;
  221 }
  222 (*stu)->next = p->next;
  223 p->next = *stu;
  224 return ok;
  225 }
  226
  227 /*
  228 * 初始化一个链表
  229 */
  230 int sinit(struct student * *s)
  231 {
  232 (*s) = (struct student *) malloc(len);
  233 (*s)->name = "hello";
  234 (*s)->age = 23;
  235 (*s)->next = null;
  236 return ok;
  237 }
  238
  239 /*
  240 * 遍历一个链表
  241 */
  242 int selects(struct student * *s)
  243 {
  244 struct student *p;
  245 p = *s;
  246 while (p) {
  247 printf("%s %d\n", p->name, p->age);
  248 p = p->next;
  249 }
  250 return ok;
  251 }
  252 /*
  253 * 销毁链表
  254 * 惠 州 外 围 - 惠 州 的 外 围 资 源 -薇芯【81343628】【81343628】【高端外围资源】【诚信合作,非诚勿扰!】可直接添加 !可以用void 代替 struct student 实现对所有的结构体销毁
  255 */
  256 int Free(struct student * *s)
  257 {
  258 free(*s);
  259 return ok;
  260 }

猜你喜欢

转载自www.cnblogs.com/zhaojun-20002003/p/12784155.html