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

📄 vain.c

📁 一个关于C语言的结构体链表程序
💻 C
字号:
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
 int num;
 char name[10];
 int score;
 struct student *next;
};
int n;
struct student *creat()
{
 struct student *head;
 struct student *p1,*p2;
 p1=p2=(struct student *)malloc(LEN);
 printf("please creat detes:\n");
 printf("-NO---name---score-\n");
 scanf("%d%s%d",&p1->num,p1->name,&p1->score);
 head=NULL;
 n=0;
 while(p1->num!=NULL)
 {
  n=n+1;
  if(n==1) head=p1;
  else p2->next=p1;
  p2=p1;
  p1=(struct student *)malloc(LEN);
  scanf("%d%s%d",&p1->num,p1->name,&p1->score);
 }
 p2->next=NULL;
 return(head);
}
void print(struct student *head)
{
 struct student *p;
 p=head;
 if(head!=NULL)
 {
  printf("-NO---name---score-\n");
  while(p!=NULL)
  {
   printf("%d  %s    %d\n",p->num,p->name,p->score);
   p=p->next;
  }
 }
 else printf("no list !\n");
}
struct student *del(int num,struct student *head)
{
 int a=0;
 struct student *p1,*p2;
 do
 {
  p1=head;
  if(head==NULL)
  {
  printf("no list !\n");
  goto end;
  }
  while(p1->num!=num&&p1->next!=NULL)
  {
   p2=p1;p1=p1->next;
  }
  if(p1->num==num)
  {
   if(p1==head) head=p1->next;
   else p2->next=p1->next;
   printf("deleted N0.%d\n",num);
   n=n-1;
   a++;
  }
 }while(p1->next!=NULL);
 if(a==0)
 printf("Number not been found !\n");
 end:
 return(head);
}
struct student *add(int num,char name[],int score,struct student *head)
{
 struct student *p0,*p1,*p2;
 p0=(struct student *)malloc(LEN);
 p0->num=num,strcpy(p0->name,name),p0->score=score;
 p1=head;
 if(num==0)
 return(head);
 if(head==NULL)
 {
  head=p0;
  p0->next=NULL;
 }
 while(p0->num>p1->num&&p1->next!=NULL)
 {
  p2=p1;
  p1=p1->next;
 }
 if(p0->num<=p1->num)
 {
  if(p1==head) head=p0;
  else p2->next=p0;
  p0->next=p1;
 }
 else
 {
  p1->next=p0;
  p0->next=NULL;
 }
 printf("added NO.%d\n",num);
 n=n+1;
 return(head);
}
void num(struct student *head)
{
 int num,a=0;
 struct student *p;
 printf("------please input \"0\" quit--------\n");
 do
 {
  p=head;
  printf("please input search NO:");
  scanf("%d",&num);
  if(num!=0)
  printf("-NO---name---score-\n");
  while(p!=NULL)
  {
   if(p->num==num)
   {
    printf("%d  %s     %d\n",p->num,p->name,p->score);
    a++;
   }
   p=p->next;
  }
  if(a==0&num!=0)
  printf("the number not been found !\n");
  a=0;
 }while(num!=0);
 clrscr();
}
void name(struct student *head)
{
 char name[10],a=0;
 struct student *p;
 printf("------please input \"0\" quit------\n");
 do
 {
  p=head;
  printf("please input search name:");
  scanf("%s",name);
  if(name[0]!='0')
  printf("-NO---name---score-\n");
  while(p!=NULL)
  {
   if(strcmp(p->name,name)==0)
   {
    printf("%d  %s   %d\n",p->num,p->name,p->score);
    a++;
   }
   p=p->next;
  }
  if(a==0&&name[0]!='0')
  printf("the name not been found !\n");
  a=0;
 }while(name[0]!='0');
 clrscr();
}
void search(struct student *head)
{
 int a;
 struct student *p;
 p=head;
 do
 {
  printf("1.search for number\n2.search for name\n3.quit\n");
  scanf("%d",&a);
  clrscr();
  switch(a)
  {
   case 1:num(p);break;
   case 2:name(p);break;
  }
 }while(a!=3);
}
struct student *sort(struct student *head)
{
 struct student *p1,*p2;
 int i,j;
 int num0,score0;
 char string[10];
 for(i=0;i<n-1;i++)
 {
  p1=head;
  for(j=n-1;j>0;j--)
  while(p1!=NULL)
  {
   p2=p1;p1=p1->next;
   if(p2->score>p1->score)
   {
    num0=p1->num;
    p1->num=p2->num;
    p2->num=num0;
    strcpy(string,p1->name);
    strcpy(p1->name,p2->name);
    strcpy(p2->name,string);
    score0=p1->score;
    p1->score=p2->score;
    p2->score=score0;
   }
  }
 }
 return(head);
}
void main()
{
 struct student *head=NULL;
 int num,score,a;
 char name[10];
 do
 {
  printf("1.creat\n2.del\n3.add\n4.search\n5.print\n6.scort\n7.quit\n");
  scanf("%d",&a);
  clrscr();
  switch(a)
  {
   case 1:printf("------please input \"0 0 0\" quit------\n");head=creat(),print(head),getch(),clrscr();break;
   case 2:printf("------please input \"0\" quit--------\n");do{printf("please input delete NO:"),scanf("%d",&num),head=del(num,head),print(head);}while(num!=0);clrscr();break;
   case 3:printf("------please input \"0 0 0\" quit------\n");do{printf("please input add date:"),scanf("%d%s%d",&num,name,&score),head=add(num,name,score,head),print(head);}while(num!=0);clrscr();break;
   case 4:search(head);break;
   case 5:print(head),getch(),clrscr();break;
   case 6:head=sort(head);print(head);getch();clrscr();break;
  }
 }while(a!=7);
}

⌨️ 快捷键说明

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