C语言之----面向对象的方法实现链表的操作
1 /*
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 * 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 * 可以用void 代替 struct student 实现对所有的结构体销毁
255 */
256 int Free(struct student * *s)
257 {
258 free(*s);
259 return ok;
260 }
C语言之----面向对象的方法实现链表的操作的更多相关文章
- C语言提高 (6) 第六天 文件(续) 链表的操作
1昨日回顾 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include &l ...
- 基于C语言的面向对象编程
嵌入式软件开发中,虽然很多的开发工具已经支持C++的开发,但是因为有时考虑运行效率和编程习惯,还是有很多人喜欢用C来开发嵌入式软件.Miro Samek说:"我在开发现场发现,很多嵌入式软件 ...
- 使C语言实现面向对象的三个要素,你掌握了吗?
- go语言面向对象之方法
1.实现方法 package main import "fmt" //在面向对象编程中,一个对象其实就是一个简单的值或者一个变量,在这个 //对象中包含一些函数 //这种带有接受者 ...
- 深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法)
作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文链接:http://zh.lucida.me/blog/java-8-lambdas-insideout-language- ...
- [java学习笔记]java语言核心----面向对象之this关键字
一.this关键字 体现:当成员变量和函数的局部变量重名时,可以使用this关键字来区别:在构造函数中调用其它构造函数 原理: 代表的是当前对象. this就是所在函数 ...
- JavaScript面向对象继承方法
JavaScript的出现已经将近20多年了,但是对这个预言的褒贬还是众说纷纭.很多人都说JavaScript不能算是面向对象的变成语言.但是JavaScript的类型非常松散,也没有编译器.这样一来 ...
- 第二十五节:Java语言基础-面向对象基础
面向对象 面向过程的代表主要是C语言,面向对象是相对面向过程而言,Java是面向对象的编程语言,面向过程是通过函数体现,面向过程主要是功能行为. 而对于面向对象而言,将功能封装到对象,所以面向对象是基 ...
- 比较分析C++、Java、Python、R语言的面向对象特征,这些特征如何实现的?有什么相同点?
一门课的课后题答案,在这里备份一下: 面向对象程序设计语言 – 比较分析C++.Java.Python.R语言的面向对象特征,这些特征如何实现的?有什么相同点? C++ 语言的面向对象特征: 对象模 ...
随机推荐
- python从图片中找图
import aircv as ac def matcha(bb,aa):#从bb查找aa,如果有则返回其坐标位置 yuan=ac.imread(bb) mubi=ac.imread(aa) resu ...
- Wordcloud(词云)安装使用以及vscode搭建虚拟环境
(电脑烧掉了主板,地方上的所有门店全部关闭了,幸好现在京东还通物流,总算是进行把电脑拿回来了.对于一些东西无法实际操作真的是很难受,言归正传,说一下Wordcloud) Wordcloud安装(全局安 ...
- python + flask轻量级框架
from flask import Flask,jsonify,make_response,abort,Response,request from flask_restful import Api,R ...
- CentOS下 Django部署 uWSGI+Django(一)
由于新冠疫情的缘故,公司要求员工停薪休假,赋闲在家的时候还是决定做点正事,学学习. 本人Linux入门水平,Python入门水平,所以在网上找的那些python部署的帖子,看的是云里雾里的,也没有达到 ...
- 【C#】C#中使用GDAL3(二):Windows下读写Shape文件及超详细解决中文乱码问题
转载请注明原文地址:https://www.cnblogs.com/litou/p/15035790.html 本文为<C#中使用GDAL3>的第二篇,总目录地址:https://www. ...
- 【搜索】棋盘 luogu-3956
分析 按照这个题目随便写一个搜索就可以了 AC代码 #include <cstdio> #include <cstring> #include <algorithm> ...
- SSM框架,在Html界面利用ajax,json,jQuery实现省市区下拉框联动
1.先生成省市区表格 2.建立实体类 3.在html画出下拉框 <select id="province"> <option value="" ...
- TypeError: attrib() got an unexpected keyword argument 'convert'
使用pyinstaller -F aaa.py时,报错 TypeError: attrib() got an unexpected keyword argument 'convert' 没有exe生成 ...
- airtest前期准备(pocoSDK+unity打apk包+安装pocoui库)
只介绍unity的环境准备,cocos的可以参考官方文档 https://poco-chinese.readthedocs.io/zh_CN/latest/source/doc/integrat ...
- DNS的原理和解析过程
DNS的解析原理和过程: 在Internet上域名和IP是对应的,DNS解析有两种:一种是正向解析,另外一种是反向解析. 正向解析:正向解析就是将域名转换成对应的 IP地址的过程,它应用于在浏览器地址 ...