项目开发日报表
项目名称 | 【苏嵌实训-嵌入式 linux C 第8天】 | |
今日进度以及任务 |
|
|
本日任务完成情况 |
代码见下文: |
|
本日开发中出现的问题汇总 | 无 |
|
本日未解决的问题 | 无 | |
本日开发收获 | 加深了对c语言的理解,加深了对链表的理解 | |
其他 | 无 |
代码:
#include
#include
#include
#define MaxSize sizeof(char) * 20
#define ERROR 1
#define Succe 0
struct students
{
char * name;
char sex;
int age;
struct students *next;
};
typedef struct students Student;
typedef Student * Stu;
//初始化头指针
void Init(Stu *head)
{
*head = (Stu)malloc(sizeof(Student));
(*head)->next = NULL;
}
//链尾插入
void Insert(Stu newstudent,Stu *head)
{
Stu temp = *head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newstudent;
newstudent->next = NULL;
}
//年龄排序
int Rank(Stu *head)
{
int i;
int j;
int n;
int count = 0;
Stu p = *head;
Stu s = p->next;
Stu t = s->next;
if(((*head)->next == NULL) || ((*head)->next->next == NULL))
{
return ERROR;
}
else
{
Stu temp = (*head)->next;
while(temp != NULL)
{
count++;
temp = temp->next;
}
n = count - 1;
for(i = 0;i < n;i++)
{
for(j = 0;j < (n - i);j++)
{
if(s->age > t->age)
{
p->next = t;
s->next = t->next;
t->next = s;
s = t;
t = p->next->next;
}
t = t->next;
s = s->next;
p = p->next;
}
p = *head;
s = p->next;
t = s->next;
}
return Succe;
}
}
//逆序链表
int Reverse(Stu *head)
{
if(((*head)->next == NULL) || ((*head)->next->next == NULL))
{
return ERROR;
}
else
{
Stu p = (*head)->next;
Stu s = p->next;
Stu t = s->next;
while(t != NULL)
{
s->next = p;
p = s;
s = t;
t = t->next;
}
s->next = p;
(*head)->next->next = NULL;
(*head)->next = s;
return Succe;
}
}
//寻找最近年龄
int Search(Stu *head,Stu *head1,int age)
{
int min;
int MIN = 100;
Stu temp = (*head)->next;
Stu t = (*head)->next;
if((*head)->next == NULL)
{
return ERROR;
}
else
{
while(temp != NULL)
{
if(((temp->age) - age) >= 0)
{
min = (temp->age) - age;
}
else
{
min = age - (temp->age);
}
if(min <= MIN)
{
MIN = min;
}
temp = temp->next;
}
printf("与所查年龄相差 %d 岁\n",MIN);
while(t != NULL)
{
if(((t->age) - age) >= 0)
{
min = (t->age) - age;
}
else
{
min = age - (t->age);
}
if(min == MIN)
{
Stu find_stu = (Stu)malloc(sizeof(Student));
find_stu->name = t->name;
find_stu->sex = t->sex;
find_stu->age = t->age;
Insert(find_stu,&(*head1));
}
t = t->next;
}
return Succe;
}
}
//显示函数
void display(Stu head,char sex)
{
Stu temp = head->next;
printf(" 姓名 性别 年龄 \n");
if(sex == 'a')
{
while(temp != NULL)
{
printf("%10s",temp->name);
printf("%10c",temp->sex);
printf("%10d\n",temp->age);
temp = temp->next;
}
}
else
{
while(temp != NULL)
{
if(temp->sex == sex)
{
printf("%10s",temp->name);
printf("%10c",temp->sex);
printf("%10d\n",temp->age);
}
temp = temp->next;
}
}
}
int main()
{
int i;
int age;
int num;
int function;
int count;
int rank_result;
int reverse_result;
int search_result;
Stu head;
Init(&head);
Stu head1;
Init(&head1);
Stu stu1 = (Stu)malloc(sizeof(Student));
stu1->name = "Zhangsan";
stu1->sex = 'b';
stu1->age = 20;
Insert(stu1,&head);
Stu stu2 = (Stu)malloc(sizeof(Student));
stu2->name = "Lisi";
stu2->sex = 'b';
stu2->age = 23;
Insert(stu2,&head);
Stu stu3 = (Stu)malloc(sizeof(Student));
stu3->name = "Wanger";
stu3->sex = 'b';
stu3->age = 22;
Insert(stu3,&head);
Stu stu4 = (Stu)malloc(sizeof(Student));
stu4->name = "HanMeimei";
stu4->sex = 'g';
stu4->age = 21;
Insert(stu4,&head);
Stu stu5 = (Stu)malloc(sizeof(Student));
stu5->name = "LiLei";
stu5->sex = 'g';
stu5->age = 24;
Insert(stu5,&head);
Stu stu6 = (Stu)malloc(sizeof(Student));
stu6->name = "ZhangHua";
stu6->sex = 'b';
stu6->age = 25;
Insert(stu6,&head);
while(1)
{
printf("** 创建男生链表(1) **\n");
printf("** 创建女生链表(2) **\n");
printf("** 按年龄排序(3) **\n");
printf("** 按年龄逆序(4) **\n");
printf("** 按年龄查找学生(5) **\n");
printf("** 退出(0) **\n");
printf("*******************************\n");
printf("\n");
printf("请输入操作\n");
scanf("%d",&function);
switch(function)
{
case 1:
{
printf("需要输入几个男学生的信息?\n");
scanf("%d",&num);
for(i = 0;i < num;i++)
{
Stu newstudent = (Stu)malloc(sizeof(Student));
newstudent->sex = 'b';
printf("请输入学生 %d 姓名\n",i + 1);
newstudent->name = (char *)malloc(MaxSize);
scanf("%s",(newstudent->name));
printf("请输入学生 %d 年龄\n",i + 1);
scanf("%d",&(newstudent->age));
Insert(newstudent,&head);
}
printf("/**********男生信息**********/\n");
printf("\n");
display(head,'b');
printf("\n");
printf("\n");
printf("\n");
break;
}
case 2:
{
printf("需要输入几个女学生的信息?\n");
scanf("%d",&num);
for(i = 0;i < num;i++)
{
Stu newstudent = (Stu)malloc(sizeof(Student));
newstudent->sex = 'g';
printf("请输入学生 %d 姓名\n",i + 1);
newstudent->name = (char *)malloc(sizeof(char) * 20);
scanf("%s",newstudent->name);
printf("请输入学生 %d 年龄\n",i + 1);
scanf("%d",&(newstudent->age));
Insert(newstudent,&head);
}
printf("/**********女生信息**********/\n");
printf("\n");
display(head,'g');
printf("\n");
printf("\n");
printf("\n");
break;
}
case 3:
{
printf("/*******排序结果为(由大到小)*******/\n");
printf("\n");
rank_result = Rank(&head);
if(rank_result == ERROR)
{
printf("故无法排序!\n");
}
if(rank_result == Succe)
{
display(head,'a');
}
printf("\n");
printf("\n");
printf("\n");
break;
}
case 4:
{
printf("/**********逆序结果**********/\n");
printf("\n");
reverse_result = Reverse(&head);
if(reverse_result == ERROR)
{
printf("无法逆序!\n");
}
if(reverse_result == Succe)
{
display(head,'a');
}
printf("\n");
printf("\n");
printf("\n");
break;
}
case 5:
{
printf("请输入要找的年龄:\n");
scanf("%d",&age);
printf("/**********查找结果**********/\n");
printf("\n");
search_result = Search(&head,&head1,age);
if(search_result == ERROR)
{
printf("查无此人!\n");
}
printf("\n");
if(search_result == Succe)
{
display(head1,'a');
}
head1->next = NULL;
printf("\n");
printf("\n");
printf("\n");
break;
}
case 0:
{
return 0;
}
}
}
return 0;
}