⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dlb.txt

📁 关于单链表的C、C++ 语言实现, 上传给那些需要数据结构实验的 人
💻 TXT
字号:
#include<malloc.h> 
#define NULL 0 
#define LEN sizeof(struct student) 
struct student 
{ 
   long num; 
   char name[20]; 
   char sex[5]; 
   long age; 
   long score; 
   struct student*next; 
}; 
int n; 
struct student*creat(void) 
{ 
    struct student*head; 
    struct student*p1,*p2; 
    n=0; 
    p1=p2=(struct student*)malloc(LEN); 
    scanf("%ld%s%s%ld%ld",&p1->num,&p1->name,&p1->sex,&p1->age,&p1->score); 
    head=NULL; 
   while(p1->num!=0) 
   { 
      n=n+1; 
       if(n==1)head=p1; 
       else 
        p2->next=p1; 
        p2=p1; 
        p1=(struct student*)malloc(LEN); 
      scanf("%ld%s%s%ld%ld",&p1->num,&p1->name,&p1->sex,&p1->age,&p1->score); 
   } 
p2->next=NULL; 
return(head); 
} 



void print(struct student *head) 
{ 
struct student *p; 
printf("\nNow,these %d records are(结果数是%d,结果是):\n",n,n); 
p=head; 
if(head!=NULL) 
do 
{ 
printf("%ld %s %s %ld %ld\n",p->num,p->name,p->sex,p->age,p->score); 
p=p->next; 
} 
while(p!=NULL); 
} 


void paixu(struct student *head) 
{ 
struct student *p,*q,*small; 
int temp; 
for(p=head;p->next!=NULL;p=p->next) 
{ 
small=p; 
for(q=p;q;q=q->next) 
if(q->num<small->num) 
small=q; 
if(small!=p) 
{ 
temp=p->num; 
p->num=small->num; 
small->num=temp; 
} 
} 
print(head); 
} 



struct student *insert(struct student *head,struct student *stud) 
{ 
struct student *p0,*p1,*p2; 
p1=head; 
p0=stud; 
if(head==NULL) 
{ 
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; 
} 
} 
n=n+1; 
return(head); 
} 


struct student *search(struct student *head,long num) 
{ 
struct student *p; 
if(head==NULL) 
{ 
printf("\nlist null! \n"); 
} 
p=head; 
while((num!=p->num)&&(p->next!=NULL)) 
{ 
p=p->next; 
} 
if(num==p->num) 
printf("%ld %s %s %ld %ld",p->num,p->name,p->sex,p->age,p->score); 
else 
printf("num not been found(找不到)!\n"); 
return(head); 
} 




struct student *del(struct student *head,long num) 
{ 
struct student *p1,*p2; 
if(head==NULL) 
{ 
printf("\nlist null(无数据)! \n"); 
} 
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; 
printf("delete(删除):%ld\n",num); 
n=n-1; 
} 
else 
printf("num not been found(无效的数字)!\n"); 
return(head); 
} 

main() 
{ 
struct student *head,*stu; 
long del_num; 
long search_num; 
int x; 
do 
{ 
printf("***************\n1 create new chain(制作新链表)\n2 insert data(插入数据)\n3 query data(查询数据)\n4 output data(打印数据)\n5 delete data(删除数据)\n0 end(退出)\n***************\n"); 
printf("Please input number 0-5 to test the program(请输入0-5选择测试):\n"); 

scanf("%d",&x); 
if(x==1) 
{ 
printf("input records(Ex: num name sex age score)(输入数据如:学号 姓名 性别 年龄 分数):\n"); 
head=creat(); 
} 
if(x==4) 
{ 
paixu(head); 
} 
if(x==2) 
{ 
printf("\ninput the inserted record(插入新数据):"); 
stu=(struct student*)malloc(LEN); 
scanf("%ld%s%s%ld%ld",&stu->num,&stu->name,&stu->sex,&stu->age,&stu->score); 
while(stu->num!=0) 
{ 
head=insert(head,stu); 
print(head); 
printf("input the inserted record(插入新数据):"); 
stu=(struct student*)malloc(LEN); 
scanf("%ld%s%s%ld%ld",&stu->num,&stu->name,&stu->sex,&stu->age,&stu->score); 
} 
} 
if(x==3) 
{ 
printf("\ninput the searched number(输入查询学号):"); 
scanf("%ld",&search_num); 
while(search_num!=0) 
{ 
head=search(head,search_num); 
printf("\ninput the searched number(输入查询学号):"); 
scanf("%ld",&search_num); 
} 
} 
if(x==5) 
{ 
printf("\ninput the deleted number(输入删除学号):"); 
scanf("%ld",&del_num); 
while(del_num!=0) 
{ 
head=del(head,del_num); 
print(head); 
printf("input the deleted number(输入删除学号):"); 
scanf("%ld",&del_num); 
} 
} 
}while(x!=0); 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -