Object-C 作为C 的包装语言(运行时、消息机制)。如果不熟悉C 的话实在玩得太肤浅。 随便深入oc 内部都会接触到C。
runtime 、GCD、Block、消息机制。。。
所有强大的功能无不用的 包装体(struct 结构体)。使用GCC/Clang (可以google 下。Clang 比GCC 更优化,) 编译指令。 转换OC 为 C 。
终端 使用Clang 命令参考
clang -rewrite-objc file.m
查看file.cpp 文件为编译转换的C
单向链表 创建 、插入 、反转
//struct
#include <stdio.h>
#include <stdlib.h>
#include <string.h> typedef struct Student_st
{
char name[];
int point;
struct Student_st *stu;
} Student; void CheckIsLinkTable(Student *student){
Student *next = student->stu;
if (next == NULL) {
printf("查询失败,不是链式结构\n");
exit();
}
} Student * CreateLink_Table(int num){
printf("num is %d\n",num); Student * head,*current,*temp;
//分配内存
head = (Student *)malloc(sizeof(Student));
if (head == NULL) {
printf("内存分配失败 \n");
exit();
} head ->name[] = '\0';
head ->point = ;
head -> stu = NULL; temp = head;
for (int i = ; i< num; i++) {
current = (Student *)malloc(sizeof(Student));
if (current == NULL) {
printf("内存分配失败 \n");
exit();
}
current ->stu = NULL; printf("请输入第%d位学生姓名和成绩 如:name|89 \n",i+);
char stuName[] = "未知";
int student_point = ;
char argValue[] = "未知"; scanf("%s",argValue); if (strstr(argValue,"|")==NULL) {
printf("请使用|分割内容 Y|N Y 重新输入,N 退出\n");
char content[];
scanf("%s",content);
if (strcmp(content,"Y")==) {
scanf("%s",argValue);
}else{
exit();
}
} //分割字符串
char *token; token = strtok(argValue,"|");
int i = ;
while (token != NULL) {
if (i>) {
student_point = atoi(token);
printf("%d\n",student_point);
}else{
////char*的值赋给数组怎么做?----> c是常量指针,不可以改变,用
strncpy(stuName,token,sizeof(stuName));
printf("%s\n",token);
} token = strtok(NULL,"|");
i++;
}
strncpy(current->name,stuName,sizeof(current->name));
current ->point = student_point; current->stu = NULL;
temp ->stu = current;
temp = current; } return head;
} //查询
void selectStudent(Student *student){
CheckIsLinkTable(student); Student *next = student->stu;
int i =;
while (next) {
printf("index %d; studentName is %s; point is %d\n",i+,next->name,next->point);
next = next ->stu;
i++;
}
}
//插入
Student * insertStudentLinkTable(Student *student,char *insertContent){
//分割字符串
if (!insertContent) {
exit();
} //分割字符串
char *token;
char stuName[];
char preStuName[];
int student_point;
token = strtok(insertContent,"|");
int i = ;
while (token != NULL) {
if (i==) {
////char*的值赋给数组怎么做?----> c是常量指针,不可以改变,用
strncpy(stuName,token,sizeof(stuName));
printf("%s\n",token);
}else if(i==){
student_point = atoi(token);
printf("%d\n",student_point);
}else{
strncpy(preStuName,token,sizeof(preStuName));
printf("%s\n",token);
} token = strtok(NULL,"|");
i++;
}
Student *preStu; CheckIsLinkTable(student);
//查找节点名字是否存在
Student *next = student->stu;
int ind = ;
while (next) {
if ((next->name) && strcmp(preStuName,next->name)==) {
printf("找到节点%s 位于第%d个节点 开始插入节点\n",preStuName,ind+);
preStu = next;
break;
}
next = next->stu;
ind++;
} Student *nextStu = preStu->stu;
//创建节点
Student *newStu = (Student *)malloc(sizeof(Student));
if (newStu==NULL) {
printf("分配内存失败");
exit();
}
strncpy(newStu->name,stuName,sizeof(char[]));
newStu->point = student_point;
newStu->stu = nextStu; preStu->stu = newStu; //查询节点
selectStudent(student);
return student;
} //反转
Student *ReversionStudentLinkTable(Student *student){
CheckIsLinkTable(student); Student *next,*current,*newLinkTable;
next = NULL;
current =NULL;
newLinkTable = NULL;
current = student->stu;
while (current) {
//保存游标节点 以后的节点
next = current->stu;
//断开游标节点后面的节点,并设置游标节点的后续节点为 新链表的第一个节点
current->stu = newLinkTable;
//保存当前游标节点 为新链表
newLinkTable = current;
//重置游标节点
current = next;
} //给游标节点添加 头节点
Student *head = NULL;
head = (Student *)malloc(sizeof(Student));
head->name[]='\0';
head->point = ;
head->stu = newLinkTable; return head;
}
int main(void){
char sf[]; /**
* 创建单向链表
*/
int num;
printf ("请输入学生人数\n");
scanf("%d",&num);
Student *link_stu = CreateLink_Table(num); /**
* 单向链表插入节点
*/
printf ("请插入节点内容 在 已存在节点名字的后面,如 已存在节点名字|待插入名字|待插入分数 \n");
scanf("%s",sf); link_stu = insertStudentLinkTable(link_stu,sf); /**
* 反转单向链表
*/
printf("反转链表Y|N \n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
Student *newLt= ReversionStudentLinkTable(link_stu);
//查询
selectStudent(newLt);
}
return 0;
}