Cチェーンテーブル操作(一方向チェーンテーブル)の復習
21669 ワード
Object-CはCのパッケージ言語(ランタイム、メッセージメカニズム)として使われています.Cに慣れていないとあまりにも遊びすぎます.勝手にocの内部に深く入り込んでもCに触れます.
runtime、GCD、Block、メッセージメカニズム..
すべての強力な機能を使用しない包装体(struct構造体).GCC/Clang(googleの下で使用できます.ClangはGCCよりも最適化されています)を使用して命令をコンパイルします.OCをCに変換します.
端末使用Clangコマンドリファレンス
clang -rewrite-objc file.m
ファイルを表示する.cppファイルはコンパイル変換のC
一方向チェーンテーブルの作成、挿入、反転
//struct
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student_st
{
char name[10];
int point;
struct Student_st *stu;
} Student;
void CheckIsLinkTable(Student *student){
Student *next = student->stu;
if (next == NULL) {
printf(" ,
");
exit(0);
}
}
Student * CreateLink_Table(int num){
printf("num is %d
",num);
Student * head,*current,*temp;
//
head = (Student *)malloc(sizeof(Student));
if (head == NULL) {
printf("
");
exit(0);
}
head ->name[0] = '\0';
head ->point = 0;
head -> stu = NULL;
temp = head;
for (int i = 0; i< num; i++) {
current = (Student *)malloc(sizeof(Student));
if (current == NULL) {
printf("
");
exit(0);
}
current ->stu = NULL;
printf(" %d :name|89
",i+1);
char stuName[10] = " ";
int student_point = 0;
char argValue[15] = " ";
scanf("%s",argValue);
if (strstr(argValue,"|")==NULL) {
printf(" | Y|N Y ,N
");
char content[5];
scanf("%s",content);
if (strcmp(content,"Y")==0) {
scanf("%s",argValue);
}else{
exit(0);
}
}
//
char *token;
token = strtok(argValue,"|");
int i = 0;
while (token != NULL) {
if (i>0) {
student_point = atoi(token);
printf("%d
",student_point);
}else{
////char* ?----> c , ,
strncpy(stuName,token,sizeof(stuName));
printf("%s
",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 =0;
while (next) {
printf("index %d; studentName is %s; point is %d
",i+1,next->name,next->point);
next = next ->stu;
i++;
}
}
//
Student * insertStudentLinkTable(Student *student,char *insertContent){
//
if (!insertContent) {
exit(0);
}
//
char *token;
char stuName[10];
char preStuName[10];
int student_point;
token = strtok(insertContent,"|");
int i = 0;
while (token != NULL) {
if (i==1) {
////char* ?----> c , ,
strncpy(stuName,token,sizeof(stuName));
printf("%s
",token);
}else if(i==2){
student_point = atoi(token);
printf("%d
",student_point);
}else{
strncpy(preStuName,token,sizeof(preStuName));
printf("%s
",token);
}
token = strtok(NULL,"|");
i++;
}
Student *preStu;
CheckIsLinkTable(student);
//
Student *next = student->stu;
int ind = 0;
while (next) {
if ((next->name) && strcmp(preStuName,next->name)==0) {
printf(" %s %d
",preStuName,ind+1);
preStu = next;
break;
}
next = next->stu;
ind++;
}
Student *nextStu = preStu->stu;
//
Student *newStu = (Student *)malloc(sizeof(Student));
if (newStu==NULL) {
printf(" ");
exit(0);
}
strncpy(newStu->name,stuName,sizeof(char[10]));
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]='\0';
head->point = 0;
head->stu = newLinkTable;
return head;
}
int main(void){
char sf[15];
/**
*
*/
int num;
printf ("
");
scanf("%d",&num);
Student *link_stu = CreateLink_Table(num);
/**
*
*/
printf (" , | |
");
scanf("%s",sf);
link_stu = insertStudentLinkTable(link_stu,sf);
/**
*
*/
printf(" Y|N
");
scanf("%s",sf);
if (strcmp(sf,"Y")==0) {
Student *newLt= ReversionStudentLinkTable(link_stu);
//
selectStudent(newLt);
}
return 0;
}