作业有点难
1.创建两个学生链表,含有姓名、年龄的信息,一个链表存放男生,一个链表存放女生
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char name[20];
int age;
int num;
struct node *next;
};
typedef struct node Node;
typedef struct node *Link;
void create_link(Link *head)
{
*head = NULL;
}
create_node(Link *new_node)
{
*new_node = (Link)malloc(sizeof(Node));
if(*new_node == NULL)
{
printf("malloc error!\n");
return;
}
}
void insert_node_tail(Link *head,Link new_node)
{
Link p;
p = *head;
if(*head == NULL)
{
*head = new_node;
new_node->next = NULL;
}
else
{
while(p->next != NULL)
{
p = p->next;
}
p->next = new_node;
new_node->next = NULL;
}
}
void display_link(Link head)
{
Link p;
p = head;
int i;
if(head == NULL)
{
printf("Link is empty!\n");
return;
}
else
{
while(p != NULL)
{
printf("%d Name:%s Age:%d\n",p->num,p->name,p->age);
printf("********************************************\n");
p = p->next;
}
}
}
void release_link(Link *head)
{
Link p;
p = *head;
while(*head != NULL)
{
*head = (*head)->next;
free(p);
p = *head;
}
}
int main()
{
Link head1 = NULL;
Link head2 = NULL;
Link new_node = NULL;
create_link(&head1);
create_link(&head2);
int i;
int boy_num;
int girl_num;
Link p;
printf("Please input boy number:");
scanf("%d",&boy_num);
for(i = 0;i < boy_num;i++)
{
create_node(&new_node);
insert_node_tail(&head1,new_node);
new_node->num = i+1;
}
printf("Please input girl number:");
scanf("%d",&girl_num);
for(i = 0;i < girl_num;i++)
{
create_node(&new_node);
insert_node_tail(&head2,new_node);
new_node->num = i+1;
}
p = head1;
for(i = 0;i < boy_num;i++)
{
printf("Input boy's name:");
scanf("%s",&p->name);
printf("Input boy's age:");
scanf("%d",&p->age);
p = p->next;
}
p = head2;
for(i = 0;i < girl_num;i++)
{
printf("Input girl's name:");
scanf("%s",&p->name);
printf("Input girl's age:");
scanf("%d",&p->age);
p = p->next;
}
printf("Boy's and Girl's information:\n");
printf("********************************************\n");
display_link(head1);
display_link(head2);
release_link(&head1);
release_link(&head2);
display_link(head1);
display_link(head2);
return 0;
}
2.将上面两个链表合并,按学生的年龄进行排序,合成新的链表
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct students
{
char name[20];
int age;
// int num;
struct students *next;
}Stu;
Stu *ass(Stu *t,char name[],int age)
{
Stu *p = (Stu *)malloc(sizeof(Stu));
strcpy(p->name,name);
p->age = age;
p->next = t;
t = p;
return t;
}
Stu *connect(Stu *q,Stu *p)
{
Stu *t = q;
if(NULL == t)
{
return p;
}
while(t->next != NULL)
{
t = t->next;
}
t->next = p;
return q;
}
Stu *rank(Stu *p)
{
Stu *t;
Stu *q;
t = p;
q = p;
int m = 0;
char s[20] = {0};
while(t->next)
{
while(q->next)
{
if((q->age)>(q->next->age))
{
m = q->age;
q->age = q->next->age;
q->next->age = m;
strcpy(s,q->name);
strcpy(q->name,q->next->name);
strcpy(q->next->name,s);
}
q = q->next;
}
t = t->next;
q = p;
}
return p;
}
void display(Stu *p)
{
while(p)
{
printf("Name:%s Age:%d\n",p->name,p->age);
p = p->next;
}
}
int main()
{
Stu *student = NULL;
Stu *man = NULL;
Stu *woman = NULL;
man = ass(man,"qq",10);
man = ass(man,"ww",13);
man = ass(man,"ee",15);
woman = ass(woman,"rr",11);
woman = ass(woman,"tt",14);
woman = ass(woman,"yy",9);
student = connect(man,woman);
display(student);
printf("Boy's and Girl's information:\n");
printf("********************************************\n");
student = rank(student);
display(student);
return 0;
}
3.将上题中建立的链表进行反转,实现按年龄的逆序排列
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct students
{
char name[20];
int age;
// int num;
struct students *next;
}Stu;
Stu *ass(Stu *t,char name[],int age)//赋值
{
Stu *p = (Stu *)malloc(sizeof(Stu));
strcpy(p->name,name);
p->age = age;
p->next = t;
t = p;
return t;
}
Stu *connect(Stu *q,Stu *p)
{
Stu *t = q;
if(NULL == t)
{
return p;
}
while(t->next != NULL)//找尾部
{
t = t->next;
}
t->next = p;
return q;
}
Stu *rank(Stu *p)
{
Stu *t;
Stu *q;
t = p;
q = p;
int m = 0;
char s[20] = {0};
while(t->next)//t相当于循环次数
{
while(q->next)//q用于相邻比较
{
if((q->age)>(q->next->age))
{
m = q->age;
q->age = q->next->age;//交换年龄
q->next->age = m;
strcpy(s,q->name);//交换姓名
strcpy(q->name,q->next->name);
strcpy(q->next->name,s);
}
q = q->next;
}
t = t->next;
q = p;
}
return p;
}
void display(Stu *p)
{
while(p)
{
printf("Name:%s Age:%d\n",p->name,p->age);
p = p->next;
}
}
Stu *revese(Stu *p)
{
Stu *t = p;
Stu *q = p;
int m = 0;
char s[10] = {0};
while(t->next)
{
while(q->next)
{
if((q->age)<(q->next->age))
{
m = q->age;
q->age = q->next->age;//交换年龄
q->next->age = m;
strcpy(s,q->name);//交换姓名
strcpy(q->name,q->next->name);
strcpy(q->next->name,s);
}
q = q->next;
}
t = t->next;
q = p;
}
return p;
}
int main()
{
Stu *student = NULL;
Stu *man = NULL;
Stu *woman = NULL;
man = ass(man,"qq",10);
man = ass(man,"ww",13);
man = ass(man,"ee",15);
woman = ass(woman,"rr",11);
woman = ass(woman,"tt",14);
woman = ass(woman,"yy",9);
student = connect(man,woman);
display(student);
printf("********************************************\n");
student = rank(student);
display(student);
printf("********************************************\n");
student = revese(student);
display(student);
return 0;
}
4.在上面的实现的新链表中,给定一个年龄,迅速查找和该学生年龄最接近的学生姓名
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
int age;
char name[20];
struct node *next;
struct node *prior;
}Stu;
Stu *ass(Stu *p,char name[],int age)
{
Stu *t = (Stu*)malloc(sizeof(Stu));
strcpy(t->name,name);
t->age = age;
if(NULL == p)
{
t->next = NULL;
t->prior = NULL;
p = t;
return p;
}
t->prior = NULL;
t->next = p;
p->prior = t;
p = t;
return p;
}
void find(Stu *p)
{
int a[100] = {0};
Stu *t = p;
int age;
int i = 0;
int j = 0;
int m = 100;
int flag = -1;
int num;
printf("Input the age to find:");
scanf("%d",&age);
while(t)
{
a[i] = (t->age)-age;
t = t->next;
while(a[i] != 0)
{
if(a[i] < 0)
{
a[i] = a[i] * flag;
}
i++;
}
}
for(j = 0;j < i;j++)
{
if(a[j] < m)
{
m = a[j];
num = j;
}
}
t = p;
for(j = 0;j < num;j++)
{
t = t->next;
}
printf("the best match student is:%s Age:%d\n",t->name,t->age);
printf("年龄相差:%d\n",m);
}
void display(Stu *p)
{
while(p)
{
printf("Name:%s Age:%d\n",p->name,p->age);
p = p->next;
}
}
int main()
{
Stu *student = NULL;
student = ass(student,"qq",12);
student = ass(student,"ww",14);
student = ass(student,"ee",16);
student = ass(student,"rr",10);
student = ass(student,"tt",9);
student = ass(student,"yy",20);
display(student);
printf("**************************************\n");
find(student);
return 0;
}