嵌入式第十八天
2019-07-12 17:58发布
生成海报
复习:
画图
线型-数组 链表 栈 队列
树型-
图型
顺序表
struct node
{
int data[30];
int last;
}
一、单链表基本操作:增删改查
#include
#include
typedef struct node
{
int score;
struct node *next;
}list_t;
void create_empty_list(list_t **p)//p=&phead *p phead
{
*p=(list_t*)malloc(sizeof(list_t));
if(NULL==*p)
{
puts("malloc error");
exit(-1);
}
(*p)->next = NULL;
}
void insert_list(list_t*phead,int data,int pos)
{
int i;
list_t *pinsert=NULL;
list_t *ptmp = phead;
for(i=0;i
{
ptmp = ptmp->next;
}
pinsert = (list_t*)malloc(sizeof(list_t));//创建新节点
pinsert->score = data;//数据赋值
pinsert->next = ptmp->next;//连接节点
ptmp->next = pinsert;
}
void print_list(list_t *phead)
{
while(phead->next!=NULL)
{
phead = phead->next;
printf("%d ",phead->score);
}
printf("
*********************
");
}
int main()
{
list_t *phead=NULL;
create_empty_list(&phead);
insert_list(phead,100,1);
insert_list(phead,90,1);
insert_list(phead,60,1);
insert_list(phead,40,2);
//delete_list(phead);
print_list(phead);
return 0;
}
练习:实现如下程序
a、函数实现创建空链表
b、实现向链表中插入学生信息:姓名 年龄 成绩
c、实现遍历打印每个学生信息
#include
#include
#include
typedef struct node
{
char name[20];
int age;
int score;
struct node *next;
}list_t;
void create_empty_list(list_t **p)//p=&phead *p phead
{
*p=(list_t*)malloc(sizeof(list_t));
if(NULL==*p)
{
puts("malloc error");
exit(-1);
}
(*p)->next = NULL;
}
void insert_list(list_t*phead,list_t insert_data,int pos)
{
int i;
list_t *pinsert=NULL;
list_t *ptmp = phead;
for(i=0;i
{
ptmp = ptmp->next;
}
pinsert = (list_t*)malloc(sizeof(list_t));//创建新节点
strcpy(pinsert->name,insert_data.name);
pinsert->age = insert_data.age;
pinsert->score = insert_data.score;
pinsert->next = ptmp->next;//连接节点
ptmp->next = pinsert;
}
void insert_data(list_t *phead)
{
list_t x;
int position;
puts("please input the data:name age score");
scanf("%s%d%d",x.name,&x.age,&x.score);
puts("please input the position:");
scanf("%d",&position);
insert_list(phead,x,position);
}
void print_list(list_t *phead)
{
while(phead->next!=NULL)
{
phead = phead->next;
printf("name:%s age:%d score:%d
",phead->name,phead->age,phead->score);
}
printf("*********************
");
}
void deal_menu_list(list_t *phead)
{
while(1)
{
puts("************************************");
puts("*****1.insert 2.print 3.exit*****");
puts("************************************");
int choose;
scanf("%d",&choose);
switch(choose)
{
case 1:
insert_data(phead);
break;
case 2:
print_list(phead);
break;
case 3:
exit(0);
case 4:
puts("choose error");
break;
}
sleep(1);
system("clear");
}
}
int main()
{
list_t*phead=NULL;
create_empty_list(&phead);
deal_menu_list(phead);
}
练习:编写函数实现求链表的长度(不包括头结点)
#include
#include
#define N 1024
typedef struct node
{
int score;
struct node *next;
}list_t;
void create_empty_list(list_t **p)//p=&phead *p phead
{
*p=(list_t*)malloc(sizeof(list_t));
if(NULL==*p)
{
puts("malloc error");
exit(-1);
}
(*p)->next = NULL;
}
int length_list(list_t *phead)//计算链表长度
{
int count=0;
while(phead->next!=NULL)
{
phead = phead->next;
count++;
}
return count;
}
void insert_list(list_t*phead,int data,int pos)//向指定位置插入数据
{
if(pos>length_list(phead)+1||pos<1)//插入位置的错误处理
{
puts("pos error!");
return;
}
int i;
list_t *pinsert=NULL;
list_t *ptmp = phead;
for(i=0;i
{
ptmp = ptmp->next;
}
pinsert = (list_t*)malloc(sizeof(list_t));//创建新节点
pinsert->score = data;//数据赋值
pinsert->next = ptmp->next;//连接节点
ptmp->next = pinsert;
}
void print_list(list_t *phead)
{
while(phead->next!=NULL)
{
phead = phead->next;
printf("%d ",phead->score);
}
printf("
*********************
");
}
int main()
{
list_t *phead=NULL;
create_empty_list(&phead);
insert_list(phead,100,1);
insert_list(phead,90,1);
insert_list(phead,60,1);
insert_list(phead,40,2);
//delete_list(phead);
print_list(phead);
printf("%d
",length_list(phead));
return 0;
}
删除功能:
#include
#include
#define N 1024
typedef struct node
{
int score;
struct node *next;
}list_t;
void create_empty_list(list_t **p)//p=&phead *p phead
{
*p=(list_t*)malloc(sizeof(list_t));
if(NULL==*p)
{
puts("malloc error");
exit(-1);
}
(*p)->next = NULL;
}
int length_list(list_t *phead)//计算链表长度
{
int count=0;
while(phead->next!=NULL)
{
phead = phead->next;
count++;
}
return count;
}
void insert_list(list_t*phead,int data,int pos)//向指定位置插入数据节点
{
if(pos>length_list(phead)+1||pos<1)
{
return;
}
int i;
list_t *pinsert=NULL;
list_t *ptmp = phead;
for(i=0;i
{
ptmp = ptmp->next;
}
pinsert = (list_t*)malloc(sizeof(list_t));//创建新节点
pinsert->score = data;//数据赋值
pinsert->next = ptmp->next;//连接节点
ptmp->next = pinsert;
}
void delete_list(list_t *phead,int pos)//删除指定位置节点
{
if(pos<1||pos>length_list(phead))
{
puts("pos error!");
return;
}
list_t *ptmp = phead;
list_t *pdel=NULL;
int i;
for(i=0;i
{
ptmp = ptmp->next;
}
pdel = ptmp->next;
ptmp->next = pdel->next;
pdel->next = NULL;
free(pdel);
}
void print_list(list_t *phead)
{
while(phead->next!=NULL)
{
phead = phead->next;
printf("%d ",phead->score);
}
printf("
*********************
");
}
int main()
{
list_t *phead=NULL;
create_empty_list(&phead);
insert_list(phead,100,1);
insert_list(phead,90,1);
insert_list(phead,60,1);
insert_list(phead,40,2);
print_list(phead);
delete_list(phead,1);
print_list(phead);
return 0;
}
练习:实现如下程序
a、函数实现创建空链表
b、实现向链表中插入学生信息:姓名 年龄 成绩
c、实现遍历打印每个学生信息
d、删除功能:(按年龄删除 如果有多个与删除年龄相同的结点 都要删除掉)
#include
#include
#include
#define N 1024
typedef struct node
{
char name[20];
int age;
int score;
struct node *next;
}list_t;
void create_empty_list(list_t **p)//p=&phead *p phead
{
*p=(list_t*)malloc(sizeof(list_t));
if(NULL==*p)
{
puts("malloc error");
exit(-1);
}
(*p)->next = NULL;
}
int length_list(list_t *phead)//计算链表长度
{
int count=0;
while(phead->next!=NULL)
{
phead = phead->next;
count++;
}
return count;
}
void insert_list(list_t*phead,list_t insert_data,int pos)
{
if(pos>length_list(phead)+1||pos<1)
{
return;
}
int i;
list_t *pinsert=NULL;
list_t *ptmp = phead;
for(i=0;i
{
ptmp = ptmp->next;
}
pinsert = (list_t*)malloc(sizeof(list_t));//创建新节点
strcpy(pinsert->name,insert_data.name);
pinsert->age = insert_data.age;
pinsert->score = insert_data.score;
pinsert->next = ptmp->next;//连接节点
ptmp->next = pinsert;
}
void insert_data(list_t *phead)
{
list_t x;
int position;
puts("please input the data:name age score");
scanf("%s%d%d",x.name,&x.age,&x.score);
puts("please input the position:");
scanf("%d",&position);
insert_list(phead,x,position);
}
void print_list(list_t *phead)
{
while(phead->next!=NULL)
{
phead = phead->next;
printf("name:%s age:%d score:%d
",phead->name,phead->age,phead->score);
}
printf("*********************
");
}
void delete_list(list_t *phead,int del_age)//删除指定位置节点
{
list_t *ptmp=phead;
list_t *pdel=NULL;
while(ptmp->next!=NULL)
{
if(ptmp->next->age==del_age)
{
pdel = ptmp->next;
ptmp->next = pdel->next;
pdel->next = NULL;
free(pdel);
puts("delete suc!");
continue;
}
ptmp = ptmp->next;
}
}
void delete_data(list_t *phead)
{
int age;
puts("please input the age you delete:");
scanf("%d",&age);
delete_list(phead,age);//按年龄删除
}
void deal_menu_list(list_t *phead)
{
while(1)
{
puts("************************************");
puts("*****1.insert 2.print 3.delete 4.exit*****");
puts("************************************");
int choose;
scanf("%d",&choose);
switch(choose)
{
case 1:
insert_data(phead);
break;
case 2:
print_list(phead);
break;
case 3:
delete_data(phead);
break;
case 4:
exit(0);
case 5:
puts("choose error");
break;
}
sleep(2);
system("clear");
}
}
int main()
{
list_t*phead=NULL;
create_empty_list(&phead);
deal_menu_list(phead);
}
完整版:
#include
#include
#define N 1024
typedef struct node
{
int score;
struct node *next;
}list_t;
void create_empty_list(list_t **p)//p=&phead *p phead
{
*p=(list_t*)malloc(sizeof(list_t));
if(NULL==*p)
{
puts("malloc error");
exit(-1);
}
(*p)->next = NULL;
}
int length_list(list_t *phead)//计算链表长度
{
int count=0;
while(phead->next!=NULL)
{
phead = phead->next;
count++;
}
return count;
}
void insert_list(list_t*phead,int data,int pos)//向指定位置插入数据节点
{
if(pos>length_list(phead)+1||pos<1)
{
return;
}
int i;
list_t *pinsert=NULL;
list_t *ptmp = phead;
for(i=0;i
{
ptmp = ptmp->next;
}
pinsert = (list_t*)malloc(sizeof(list_t));//创建新节点
pinsert->score = data;//数据赋值
pinsert->next = ptmp->next;//连接节点
ptmp->next = pinsert;
}
void delete_list(list_t *phead,int pos)//删除指定位置节点
{
if(pos<1||pos>length_list(phead))
{
puts("pos error!");
return;
}
list_t *ptmp = phead;
list_t *pdel=NULL;
int i;
for(i=0;i
{
ptmp = ptmp->next;
}
pdel = ptmp->next;
ptmp->next = pdel->next;
pdel->next = NULL;
free(pdel);
}
void print_list(list_t *phead)
{
while(phead->next!=NULL)
{
phead = phead->next;
printf("%d ",phead->score);
}
printf("
*********************
");
}
void destroy_list(list_t *phead)//释放整个链表
{
list_t *pdel=NULL;
while(phead->next!=NULL)
{
pdel = phead->next;//删除释放第一个数据节点
phead->next = pdel->next;
pdel->next = NULL;
free(pdel);
pdel=NULL;
puts("****");
}
free(phead);//释放头节点
phead=NULL;
}
void search_list(list_t *phead,int data)
{
list_t *ptmp=phead;
while(ptmp->next!=NULL)
{
if(ptmp->next->score==data)
{
puts("have found!");
return;
}
ptmp = ptmp->next;
}
puts("not found!");
}
int main()
{
list_t *phead=NULL;
create_empty_list(&phead);
insert_list(phead,100,1);
insert_list(phead,90,1);
insert_list(phead,60,1);
insert_list(phead,40,2);
print_list(phead);
delete_list(phead,1);
print_list(phead);
search_list(phead,90);
destroy_list(phead);
return 0;
}
作业1:链表实现:已知链表中数据有32 45 67 89 12 键盘数据数据 及位置 将数据插入指定位置
作业2:有两个链表 其头指针分别为:pHeadA pHeadB 写函数将两个链表中data相同的结点删除
作业3:选做 将一个单链表翻转 1-2-3-4 翻转后4-3-2-1
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮