下列是代码
#include "stdio.h"
#include "malloc.h"
typedef struct Class CLASE;
typedef struct Person PERSON;
//静态双向链表 环状
typedef struct Person {
char *name;
int age;
Person *person_class;
Person *next;
Person *last;
CLASE *class1;
void (*function)(int cnt, ...);
} Person;
typedef struct Class {
char *name;
char *choose_name;
PERSON *Person;
} Class;
void Person_Choose(Class *param) {
}
void DoubleLinkList_Init(Person *list, const char *name, int age) {
list->age = age;
list->name = (char *)name;
list->next = NULL;
list->last = NULL;
list->person_class = NULL;
}
//如果传入的是一个形式参数 而不是一个 指针参数的话 后面的 指针指向 会指向一个 错误的地方
void LinkList_Person_Init(Person *list, const char *name, int age, Class *temp, Person *temp1, Person *temp2,
void (*funtcion)(int cnt, ...)) {
list->name = (char *)name;
list->age = age;
list->person_class = temp->Person;
list->next = temp1;
list->last = temp2;
list->class1 = temp;
list->function = funtcion;
}
void funtion1(int cnt, ...) {
printf("MY_name_is person 1\n");
printf("cnt %d\n", cnt);
}
void funtion2(int cnt, ...) {
printf("MY_name_is person 2\n");
printf("cnt %d\n", cnt);
}
void funtion3(int cnt, ...) {
printf("MY_name_is person 3\n");
printf("cnt %d\n", cnt);
}
void function(Person *person1) {
void (*function)(int cnt, ...);
printf("%s %d %s %s %s\n", person1->name, person1->age, person1->class1->name, person1->next->name,
person1->last->name);
function = person1->function;
(*function)(1);
}
int main() {
int a;
int flag = 0;
Class class1;
Person *test;
Person person1;
Person person2;
Person person3;
class1.name = (char *)"YueWuMian";
LinkList_Person_Init(&person1, "LaoLi", 40, &class1, &person2, &person3, funtion1);
LinkList_Person_Init(&person2, "LaoWang", 43, &class1, &person3, & person1, funtion2);
LinkList_Person_Init(&person3, "LaoSun", 45, &class1, & person1, & person2, funtion3);
//printf("%s %d %s %s %s", person1.name, person1.age, person1.class1->name, person1.next->name, person1.last->name);
// printf("%s %d \n", person1.name, person1.age);
// printf("%s %d \n", person2.name, person2.age);
// printf("%s %d \n", person3.name, person3.age);
// printf("%s \n", class1.name);
// printf("%s\n", person1.next->name);
// printf("%s\n", person1.last->name);
printf("OK\n");
test = &person1;
while (1) {
if (flag == 0) {
scanf("%d ", & a);
flag = 1;
}
if (flag == 1) {
switch (a) {
case 1:
test = test->next;
break;
case 2:
test = test->last;
break;
case 3:
function(test);
break;
default:
return 0;
}
flag = 0;
}
}
return 0;
}
//单向链表
//
//typedef struct Person {
// int age;
// char *name;
// struct Person *next;
//} Person;
//void LinkList_Init(Person *temp) {
// temp->age = 20;
// temp->name = (char *)"wang";
// temp->next = NULL;
//}
//
//void LinkList_Cal(Person *temp) {
// temp->age = 10;
// temp->name = (char *)"LI";
// temp->next = NULL;
//}
//
//void CreatNew_LinkList(Person *newtemp) {
// Person *newidem;
// newidem = (Person *)malloc(sizeof(Person));
// newtemp->next = newidem;
// newidem->next = NULL;
//}
//
//void ToChangeLinkListContent(Person *temp) {
// temp->age = 100;
// temp->name = (char *)"non";
// temp->next = NULL;
//}
//
//void DeleTheLinkList(Person *temp) {
// Person *item;
// item = temp->next;
// free(item);
//
// temp->next = NULL;
//}
//
//
//int main() {
// Person p;
// Person *temp;
// temp = &p;
// p.age = 0;
// LinkList_Init(&p);
// printf("%d %s\n", p.age, p.name);
// LinkList_Cal(&p);
// printf("%d %s\n", p.age, p.name);
// CreatNew_LinkList(&p);
// while (temp->next != NULL) {
// temp = temp->next;
// ToChangeLinkListContent(temp);
// printf("%d %s\n", temp->age, temp->name);
// }
//
// printf(" %d \n", 1);
// printf("Dele %d \n", p.next->age);
// DeleTheLinkList(&p);
// if (p.next != NULL)
// printf("NO NULL\n");
// else
// printf("NULL \n");
//
// return 0;
//}
运行结果很奇怪 不知道是为什么 我必须 输入两次 才会输出一次 而且上一次的 输入效果会保留下来。