栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c语言 :结构体与共用体

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c语言 :结构体与共用体

#include
#include
#define NULL 0              
#define LEN sizeof(struct info)      

struct info
{
int num;
float score;
struct info *next;
};
int n;        

struct info *creat(void)     
{
struct info *head, *p1, *p2;
head=NULL;
n=0;                
p1=p2=(struct info *)malloc(LEN);
printf("Please input the number and score:n");
scanf("%d%f", &p1->num, &p1->score);      
while(p1->num!=0)   
{
n=n+1;             
if(n==1)head=p1;       
else p2->next=p1;      
p2=p1;
p1=(struct info *)malloc(LEN);       
scanf("%d%f", &p1->num, &p1->score);
}
p2->next=NULL;   
free(p1);     
return(head);   
}

void print(struct info *head)  

struct info *p;
p=head;
if(head)        
do

printf("%d%5.1fn", p->num, p->score);
p=p->next;
}while(p!=NULL);
}

struct info *del(struct info *head, int num) 
{
struct info *p1, *p2;
if(!head){printf("nlist null!n"); return(head);}    
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{   p2=p1; p1=p1->next; }    
if(num==p1->num) 
{  if(p1==head)
head=p1->next;    
else
p2->next=p1->next;
free(p1);
printf("delete:%dn",num);
}
else
printf("%d not been found!n", num);
return(head);
}

struct info *insert(struct info *head, struct info *stud)
{
struct info *p0, *p1, *p2;
p1=head;
p0=stud;
if(!head)
{head=p0;
p0->next=NULL;}
else
{ while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1)head=p0;
else p2->next=p0;
p0->next=p1;
}
else{p1->next=p0;p0->next=NULL;}
}
return(head);
}

main()
{
struct info *head, *stu;
int delnum;
head=creat();                  
print(head);                                
printf("nPlease input the del num:");
scanf("%d",&delnum);
while(delnum)  
{
head=del(head, delnum);
print(head);
printf("nPlease input the del num:");
scanf("%d", &delnum);
}
printf("nPlease insert the record:");
stu=(struct info*)malloc(LEN);          
scanf("%d%f", &stu->num, &stu->score);
while(stu->num!=0)
{
head=insert(head, stu);      
printf("nPlease insert the record:");
stu=(struct info*)malloc(LEN);
scanf("%d%f", &stu->num, &stu->score);
}
free(stu);  
print(head);
}
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/312256.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号