苏嵌嵌入式linux C实训 第 8 天

                                                                                      项目开发日报表

项目名称【苏嵌实训-嵌入式 linux C 第8天】
今日进度以及任务
链表:单链表,环链表

本日任务完成情况

  1. 题目:创建两个学生链表,含有姓名、年龄的信息,一个链表存放男生,一个链表存放女生
  2. 题目:将上面两个链表合并,按学生的年龄进行排序,合成新的链表
  3. 题目:将上题中建立的链表进行反转,实现按年龄的逆序排列
  4. 题目:在上面的实现的新链表中,给定一个年龄,迅速查找和该学生年龄最接近的学生姓名

代码见下文:

本日开发中出现的问题汇总

本日未解决的问题
本日开发收获加深了对c语言的理解,加深了对链表的理解
其他

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#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;
}
 
 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

身在江湖的郭大侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值