实验十 链表

1、
实验十 链表 #include < stdio.h >
实验十 链表#include
< stdlib.h >
实验十 链表
void  main()
实验十 链表
{
实验十 链表    
int *a,*b,*c,*min;
实验十 链表    a
=(int *)malloc(sizeof(int));
实验十 链表    b
=(int *)malloc(sizeof(int));
实验十 链表    c
=(int *)malloc(sizeof(int));
实验十 链表    min
=(int *)malloc(sizeof(int));
实验十 链表    printf(
"输入三个整数:");scanf("%d %d %d",a,b,c);
实验十 链表    printf(
"输出这三个整数:%d %d %d \n",*a,*b,*c);
实验十 链表    
*min=*a;
实验十 链表    
if(*a>*b) *min=*b;
实验十 链表    
if(*a>*c) *min=*c;
实验十 链表    printf(
"输出最小整数:%d\n",*min);
实验十 链表    free(a);free(b);free(c);free(min);
实验十 链表}

2、
实验十 链表 #include < stdio.h >
实验十 链表#include
< stdlib.h >
实验十 链表
struct  list
实验十 链表
{
实验十 链表    
char data;
实验十 链表    
struct list *next;
实验十 链表}
;
实验十 链表
struct  list  * create()
实验十 链表
{
实验十 链表    
struct list *h,*p,*q;
实验十 链表    
char ch;
实验十 链表    h
=(struct list *)malloc(sizeof(struct list));
实验十 链表    p
=q=h;
实验十 链表    ch
=getchar();
实验十 链表    
while(ch!='?')
实验十 链表    
{
实验十 链表        p
=(struct list *)malloc(sizeof(struct list));
实验十 链表        p
->data=ch;
实验十 链表        q
=p;
实验十 链表        ch
=getchar();
实验十 链表    }

实验十 链表    p
->next=NULL;
实验十 链表    
return h;
实验十 链表}

实验十 链表
实验十 链表
void  main()
实验十 链表
{
实验十 链表}

3、
实验十 链表 #include < stdio.h >
实验十 链表#include
< stdlib.h >
实验十 链表#include
< conio.h >
实验十 链表#include
< ctype.h >
实验十 链表
#define  LEN sizeof(struct student)
实验十 链表
struct  student
实验十 链表
{
实验十 链表    
long num;
实验十 链表    
char name[20];
实验十 链表    
float score[3];
实验十 链表    
struct student *next;
实验十 链表}
;
实验十 链表
int  n;
实验十 链表
struct  student  * create()
实验十 链表
{
实验十 链表    
struct student *h,*p,*q;
实验十 链表    n
=0;h=NULL;
实验十 链表    p
=(struct student *)malloc(LEN);
实验十 链表    scanf(
"%ld %s %f %f %f",&p->num,p->name,&p->score[0],&p->score[1],&p->score[2]);
实验十 链表    p
->next=NULL;
实验十 链表    
while(p->num!=0)
实验十 链表    
{
实验十 链表        
++n;
实验十 链表        
if(n==1) h=p;
实验十 链表        
else q->next=p;
实验十 链表        q
=p;
实验十 链表        p
=(struct student *)malloc(LEN);
实验十 链表        scanf(
"%ld %s %f %f %f",&p->num,p->name,&p->score[0],&p->score[1],&p->score[2]);
实验十 链表        p
->next=NULL;
实验十 链表    }

实验十 链表    free(p);
实验十 链表    
return (h);
实验十 链表}

实验十 链表
实验十 链表
void  print( struct  student  * head)
实验十 链表
{
实验十 链表    
struct student *p;
实验十 链表    printf(
"\n现在共有%d个学生信息:\n",n);
实验十 链表    p
=head;
实验十 链表    
if(head!=NULL)
实验十 链表    
do
实验十 链表    
{
实验十 链表        printf(
"学号\t姓名\t语文\t数学\t英语\n");
实验十 链表        printf(
"%ld\t%s\t%.2f\t%.2f\t%.2f\n",p->num,p->name,p->score[0],p->score[1],p->score[2]);
实验十 链表        p
=p->next;
实验十 链表    }
while(p!=NULL);
实验十 链表}

实验十 链表
实验十 链表
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;
实验十 链表    
return head;
实验十 链表}

实验十 链表
实验十 链表
struct  student  * del( struct  student  * head, long  num)
实验十 链表
{
实验十 链表    
struct student *p1,*p2;
实验十 链表    
if(head==NULL)
实验十 链表    
{
实验十 链表        printf(
"\n无学生信息!\n");
实验十 链表        
goto end;
实验十 链表    }

实验十 链表    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(
"删除:%ld\n",num);
实验十 链表        free(p1);
实验十 链表        n
--;
实验十 链表    }

实验十 链表    
else
实验十 链表        printf(
"无学号%ld记录!\n",num);
实验十 链表end:
实验十 链表    
return head;
实验十 链表}

实验十 链表
实验十 链表
struct  student  * find( struct  student  * head, long  num)
实验十 链表
{
实验十 链表    
struct student *p1,*p2;
实验十 链表    
if(head==NULL)
实验十 链表    
{
实验十 链表        printf(
"\n无学生信息!\n");
实验十 链表        
goto end;
实验十 链表    }

实验十 链表    p1
=head;
实验十 链表    
while(num!=p1->num&&p1->next!=NULL)
实验十 链表    
{p2=p1;p1=p1->next;}
实验十 链表    
if(num==p1->num)
实验十 链表        printf(
"查找结果:%ld\t%s\t%.2f\t%.2f\t%.2f\n",num,p1->name,p1->score[0],p1->score[1],p1->score[2]);
实验十 链表    
else
实验十 链表        printf(
"没有发现%ld学生记录!\n",num);
实验十 链表end:
实验十 链表    
return head;
实验十 链表}

实验十 链表
void  main()
实验十 链表
{
实验十 链表    
struct student *head,*stu;
实验十 链表    
long find_num,del_num;
实验十 链表    
char letter;
实验十 链表    printf(
"请输入学生信息(学号为0结束):\n");
实验十 链表    head
=create();
实验十 链表    print(head);
实验十 链表    
do
实验十 链表    
{
实验十 链表        printf(
"A 插入学生信息\n");
实验十 链表        printf(
"B 查找学生信息\n");
实验十 链表        printf(
"C 删除学生信息\n");
实验十 链表        printf(
"Q 退出\n");
实验十 链表        printf(
"请选择: \n");
实验十 链表        
实验十 链表        letter 
= getch();     
实验十 链表        letter 
= toupper(letter);
实验十 链表        
实验十 链表        
switch (letter)
实验十 链表        
{
实验十 链表        
case 'A'
实验十 链表            printf(
"请输入要插入的学生信息:\n");
实验十 链表            stu
=(struct student *)malloc(LEN);
实验十 链表            scanf(
"%ld %s %f %f %f",&stu->num,stu->name,&stu->score[0],&stu->score[1],&stu->score[2]);
实验十 链表            head
=insert(head,stu);
实验十 链表            print(head);
实验十 链表            
break;
实验十 链表        
case 'B'
实验十 链表            printf(
"请输入要查找的学生学号:\n");
实验十 链表            scanf(
"%ld",&find_num);
实验十 链表            head
=find(head,find_num);
实验十 链表            
//print(head);
实验十 链表
            break;
实验十 链表        
case 'C'
实验十 链表            printf(
"请输入要删除的学生学号:\n");
实验十 链表            scanf(
"%ld",&del_num);
实验十 链表            head
=del(head,del_num);
实验十 链表            print(head);
实验十 链表            
break;
实验十 链表        }

实验十 链表    }
while (letter != 'Q');
实验十 链表}

你可能感兴趣的:(链表)