项目名称 | 【苏嵌实训-嵌入式linuxC第六天】 |
---|---|
今日进度以及任务 | 单链表的理论学习与操作、练习编程题 |
本日任务完成情况 | 对链表操作进行了练习 |
本日开发中出现的问题汇总 | 见下 |
本日未解决的问题 | 无 |
本日开发收获 | 通过敲写代码,对单链表的操作更加熟悉了 |
其他 | 无 |
- 嵌入式方向需要掌握哪些数据结构:栈、队列、链表、二叉树、平衡二叉树、红黑树、哈希表、图
- 数据结构推荐的书籍:大话数据结构、csdn
- 数据结构的作用:管理数据结构的存和取
- 链表:动态管理,在使用时分配内存<为什么需要链表?数组静态存储,空间的利用率不高>
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
//创建两个链表,含有姓名年龄的信息
//定义结构体
typedef struct node
{
char name[30];
int age;
char sex[5];
struct node *next;
}stu,*link_stu;
//判断是否成功申请空间
int Is_malloc(link_stu p)
{
if(NULL == p)
{
printf("malloc error!\n");
return -1;
}
//printf("link创建成功\n");
return 0;
}
//初始化带头节点的链表
void Inintlist(link_stu *head)
{
*head = (link_stu)malloc(sizeof(stu));
if(!Is_malloc(*head))
{
(*head)->next = NULL;
}
}
//头插法
void Insert_head_node(link_stu newnode,link_stu *head)
{
newnode->next = (*head)->next;
(*head)->next = newnode;
}
//尾插法
void Insert_trail_node(link_stu newnode,link_stu *head)
{
link_stu temp = (*head)->next;
link_stu pre = temp;
while(temp!=NULL)
{
pre = temp;
temp = temp->next;
}
pre->next=newnode;
pre->next->next=NULL;
}
//打印
void Display(link_stu head)
{
link_stu temp = head->next;
while(temp!=NULL)
{
printf("name : ");
puts(temp->name);
printf("age : %d\n",temp->age);
printf("sex : ");
puts(temp->sex);
temp = temp->next;
}
}
//把两个链表连接起来
void link_list(link_stu head1,link_stu *head_link)
{
link_stu temp = *head_link;
link_stu pre = temp;
while(temp!=NULL)
{
pre = temp;
temp = temp->next;
}
pre->next = head1->next;
}
//排序,时间复杂度O(n),空间复杂度O(1),从大到小排序
void sort(link_stu *head)
{
link_stu p = (*head)->next;
link_stu pre;
link_stu r = p->next;
p->next = NULL;
p=r;
while(p!=NULL)
{
r=p->next;
pre=*head;
while(pre->next!=NULL&&pre->next->age>p->age)
pre = pre->next;
p->next=pre->next;
pre->next=p;
p=r;
}
}
//逆序,时间复杂度O(n),空间复杂度O(1)
void reverse(link_stu *head)
{
link_stu p = (*head)->next;
link_stu r = p->next;
p->next=NULL;
p=r;
while(p!=NULL)
{
r = r->next;
Insert_head_node(p,head);
p=r;
}
}
//查找,时间复杂度O(n),空间复杂度O(1)
void find_short_age(int age,link_stu head)
{
link_stu p = head->next;
char name[30]="NULL";
int min=200;
while(p!=NULL)
{
if(min>=abs((p->age)-age))
{
min=abs((p->age)-age);
strcpy(name,p->name);
}
p=p->next;
}
printf("name is:\n");
puts(name);
}
int main()
{
link_stu head_women,head_men,head_all;
link_stu newnode;
int age;
//初始化女生链表,并且插入数据、打印
Inintlist(&head_women);
printf("please input stu_women's name:\n");
newnode = (link_stu)malloc(sizeof(stu));
if(!Is_malloc(newnode))
{
gets(newnode->name);
}
while(strcmp(newnode->name,"abc"))
{
printf("age: ");
scanf("%d",&newnode->age);
getchar();
printf("sex: ");
gets(newnode->sex);
Insert_head_node(newnode,&head_women);
newnode = (link_stu)malloc(sizeof(stu));
if(!Is_malloc(newnode))
{
printf("please input stu_women's name:\n");
gets(newnode->name);
}
}
//Display(head_women);
//初始化男生链表,并且插入数据、打印
Inintlist(&head_men);
printf("please input stu_men's name:\n");
newnode = (link_stu)malloc(sizeof(stu));
if(!Is_malloc(newnode))
{
gets(newnode->name);
}
while(strcmp(newnode->name,"abc"))
{
printf("age: ");
scanf("%d",&newnode->age);
getchar();
printf("sex: ");
gets(newnode->sex);
Insert_head_node(newnode,&head_men);
newnode = (link_stu)malloc(sizeof(stu));
if(!Is_malloc(newnode))
{
printf("please input stu_men's name:\n");
gets(newnode->name);
}
}
// Display(head_men);
//初始化链表用来保存男生女生信息
Inintlist(&head_all);
//把表先连接起来
link_list(head_women,&head_all);
link_list(head_men,&head_all);
//Display(head_all);
//再进行排序
sort(&head_all);
//Display(head_all);
//逆序
reverse(&head_all);
//Display(head_all);
printf("please input age:\n");
scanf("%d",&age);
find_short_age(age,head_all);
return 0;
}