zoukankan      html  css  js  c++  java
  • (2010计本3班-杨蒙)面向对象的C--实现链表操作

    2010计本3班-杨蒙-面向对象的C--实现链表操作

    ym@ubuntu:~/桌面$ gedit student.c


    点击(此处)折叠或打开

    1. /*
    2. * 详细运行过程: 本程序实现的是对链表的简单的操作,即链表的增 删 改 查 销毁 初始化
    3. * 运用面向对象的思想,实现一个类op,op中包括了所有的链表操作方法
    4. * 其他的程序调用op类,实现对表链表的操作
    5. * 链表包括
    6. * 面向对象,简单易学程序更加紧凑,更加健壮,更安全
    7. */
    8. #include<string.h>
    9. #include<stdlib.h>
    10. #include<stdio.h>
    11. #define ok 1
    12. #define err 0
    13. #define null NULL
    14. #define len sizeof(struct student)    //结构体student 的长度
    15. #define llen sizeof(struct Op)    //结构体op 的长度
    16. /*
    17. * c语言中的结构体,包括3个简单的数据类型,
    18. * 面向对象中的类,包括3个属性
    19. */
    20. struct student {
    21.     char *name;
    22.     int age;
    23.     struct student *next;
    24. };
    25. /*
    26. * 实现类op,op包含了所有的对链表操作的方法
    27. * 对数据及方法的封装,有保护数据的功能
    28. */
    29. struct Op {
    30.     int l;            //记录链表的长度
    31.     int (*sinit) (struct student * *s);    //初始化一个链表
    32.     int (*Free) (struct student * *s);    //销毁一个链表
    33.     int (*selects) (struct student * *s);    //遍历一个链表
    34.     int (*add1) (struct student * *s, struct student * *stu, int i);    //增加一个节点
    35.     int (*add2) (struct student * *s, char *name, int age, int i);
    36.     //初始化一个链表节点
    37.     struct student *(*getstudent) (char *name, int age);
    38.     //更新一个链表节点
    39.     struct student *(*updateage) (struct student * *s, int age, int i);
    40.     struct student *(*updatename) (struct student * *s, char *name,
    41.                  int i);
    42.     struct student *(*updates) (struct student * *s, char *name,
    43.                  int age, int i);
    44.     //删除一个链表节点
    45.     struct student *(*deletes) (struct student * *s, int i);
    46. };
    47. struct Op *op;            //声明一个op类
    48. int main()
    49. {
    50.     struct student *p;
    51.     init(&op);        //初始化一个链表节点
    52.     (*(op->sinit)) (&p);    //调用op类实现初始化一个链表的头节点
    53.     (*(op->add2)) (&p, "ym", 24, 1);    //调用op类实现添加为链表一个节点
    54.     (*(op->add2)) (&p, "ym", 25, 1);
    55.     printf("--------------------------------------- ");
    56.     (*(op->selects)) (&p);    //调用op类 遍历链表
    57.     (*(op->updates)) (&p, "ym123", 100, 1);    //调用op类 更新一个节点
    58.     printf("--------------------------------------- ");
    59.     (*(op->selects)) (&p);
    60.     (*(op->deletes)) (&p, 2);    //调用op类 删除一个节点
    61.     printf("--------------------------------------- ");
    62.     (*(op->selects)) (&p);
    63.     (*(op->Free)) (&p);    //调用op类 销毁链表
    64.     return ok;
    65. }

    66. //一下内容可以包含在一个头文件中
    67. /*
    68. * 初始化一个链表节点 并为链表节点赋值
    69. * @return 返回一个链表节点 这个节点就是以name和age 为属性的student节点
    70. * @param name age student 的两个属性
    71. */
    72. struct student *getstudent(char *name, int age)
    73. {
    74.     struct student *p;
    75.     (*(op->sinit)) (&p);    //初始化一个链表的节点
    76.     p->name = name;
    77.     p->age = age;
    78.     return p;
    79. }

    80. /*
    81. * 初始化一个op类
    82. * 并对op的方法进行封装
    83. */
    84. int init(struct Op * *op)
    85. {
    86.     //声明链表的操作方法,即链表的增 删 改 查 销毁 初始化
    87.     struct student *deletes(struct student * *s, int i);
    88.     struct student *updates(struct student * *s, char *name, int age,
    89.                 int i);
    90.     struct student *updateage(struct student * *s, int age, int i);
    91.     struct student *updatename(struct student * *s, char *name, int i);
    92.     struct student *getstudent(char *name, int age);
    93.     int add1(struct student * *s, struct student * *stu, int i);
    94.     int add2(struct student * *s, char *name, int age, int i);
    95.     int selects(struct student * *s);
    96.     int sinit(struct student * *s);
    97.     int Free(struct student * *s);
    98.     *op = (struct Op *) malloc(llen);
    99.     //对链表的所有的操作进行封装
    100.     (*op)->l = 0;        //对属性l封装
    101.     (*op)->sinit = sinit;
    102.     (*op)->Free = Free;
    103.     (*op)->selects = selects;
    104.     (*op)->add1 = add1;
    105.     (*op)->add2 = add2;
    106.     (*op)->getstudent = getstudent;
    107.     (*op)->updateage = updateage;
    108.     (*op)->updatename = updatename;
    109.     (*op)->updates = updates;
    110.     (*op)->deletes = deletes;
    111.     return ok;
    112. }

    113. /*
    114. * 删除一个链表节点
    115. * 并返回删除前的链表节点
    116. */
    117. struct student *deletes(struct student * *s, int i)
    118. {
    119.     struct student *p, *student;
    120.     p = *s;
    121.     if (i > op->l || i < 0) {
    122.         printf("请输入正确的数据! ");
    123.         return null;
    124.     }
    125.     int j = 0;
    126.     for (; j < i - 1; j++) {
    127.         p = p->next;
    128.     }
    129.     student = p->next;
    130.     p->next = p->next->next;
    131.     op->l--;
    132.     return student;
    133. }

    134. /*
    135. * 更新链表的数据
    136. * 返回更新前的链表
    137. */
    138. struct student *updates(struct student * *s, char *name, int age, int i)
    139. {
    140.     struct student *p;
    141.     (*(op->updateage)) (s, age, i);
    142.     p = (*(op->updatename)) (s, name, i);
    143.     return p;
    144. }

    145. /*
    146. * 更新链表的数据
    147. * 返回更新前的链表
    148. */
    149. struct student *updateage(struct student * *s, int age, int i)
    150. {
    151.     struct student *p, *student;
    152.     p = *s;
    153.     if (i <= 0 || i > op->l) {
    154.         printf("请检查你的数据! ");
    155.         return null;
    156.     }
    157.     int j = 0;
    158.     for (; j != i; j++) {
    159.         p = p->next;
    160.     }
    161.     student = p;
    162.     p->age = age;
    163.     return student;
    164. }

    165. /*
    166. * 更新链表的数据
    167. * 返回更新前的链表
    168. */
    169. struct student *updatename(struct student * *s, char *name, int i)
    170. {
    171.     struct student *p, *student;
    172.     p = *s;
    173.     if (i <= 0 || i > op->l) {
    174.         printf("请检查你的数据! ");
    175.         return null;
    176.     }
    177.     int j = 0;
    178.     for (; j != i; j++) {
    179.         p = p->next;
    180.     }
    181.     student = p;
    182.     p->name = name;
    183.     return student;
    184. }

    185. /*
    186. * 增加一个链表节点
    187. */
    188. int add2(struct student * *s, char *name, int age, int i)
    189. {
    190.     struct student *p;
    191.     p = (*(op->getstudent)) (name, age);
    192.     (*(op->add1)) (s, &p, i);
    193. }

    194. /*
    195. * 增加一个链表节点
    196. */
    197. int add1(struct student * *s, struct student * *stu, int i)
    198. {
    199.     struct student *p;
    200.     p = *s;
    201.     if (i > op->l + 1 || i < 0) {
    202.         printf("请检查你的输入! ");
    203.         return err;
    204.     }
    205.     op->l++;
    206.     int j = 0;
    207.     for (; j < i - 1; j++) {
    208.         p = p->next;
    209.     }
    210.     (*stu)->next = p->next;
    211.     p->next = *stu;
    212.     return ok;
    213. }

    214. /*
    215. * 初始化一个链表
    216. */
    217. int sinit(struct student * *s)
    218. {
    219.     (*s) = (struct student *) malloc(len);
    220.     (*s)->name = "yangmeng";
    221.     (*s)->age = 23;
    222.     (*s)->next = null;
    223.     return ok;
    224. }

    225. /*
    226. * 遍历一个链表
    227. */
    228. int selects(struct student * *s)
    229. {
    230.     struct student *p;
    231.     p = *s;
    232.     while (p) {
    233.         printf("%s %d ", p->name, p->age);
    234.         p = p->next;
    235.     }
    236.     return ok;
    237. }

    238. /*
    239. * 销毁链表
    240. * 可以用void 代替 struct student 实现对所有的结构体销毁
    241. */
    242. int Free(struct student * *s)
    243. {
    244.     free(*s);
    245.     return ok;
    246. }



    ym@ubuntu:~/桌面$ indent -kr -i8 student.c
    ym@ubuntu:~/桌面$ gcc student.c
    ym@ubuntu:~/桌面$ ./a.out


    点击(此处)折叠或打开

    1. /*
    2. * 详细运行过程: 本程序实现的是对链表的简单的操作,即链表的增 删 改 查 销毁 初始化
    3. * 运用面向对象的思想,实现一个类op,op中包括了所有的链表操作方法
    4. * 其他的程序调用op类,实现对表链表的操作
    5. * 链表包括
    6. * 面向对象,简单易学程序更加紧凑,更加健壮,更安全
    7. */
    8. ---------------------------------------
    9. yangmeng 23
    10. ym 25
    11. ym 24
    12. ---------------------------------------
    13. yangmeng 23
    14. ym123 100
    15. ym 24
    16. ---------------------------------------
    17. yangmeng 23
    18. ym123 100
    19. ym@ubuntu:~/桌面$


    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
    阅读(756) | 评论(0) | 转发(1) |
    给主人留下些什么吧!~~
    评论热议
  • 相关阅读:
    给程序员献礼 各种各样漂亮的qq在线状态客服代码生成工具V6.0 支持的顶起来
    CMS系统遇挂马,送大家个木马监控软件来解决问题!
    pgpoolII的性能缺陷
    socket通信,server与多客户端通信(二)
    对pgpooII的pool_process_context的 proc_id 的理解
    C语言 对Ctrl+C 的处理
    pgpoolII 的health_check_period 和 health_check_timeout
    pgpoolII的性能缺陷(二)
    模仿pgpoolII的方式,建立线程池
    pgpoolII中是如何实现进程池的
  • 原文地址:https://www.cnblogs.com/ztguang/p/12648469.html
Copyright © 2011-2022 走看看