zoukankan      html  css  js  c++  java
  • 实验十 链表

    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');
    }
  • 相关阅读:
    系统分析与设计——作业9
    系统分析与设计——作业8
    系统分析与设计——作业7
    系统分析与设计——作业6
    系统分析与设计——作业5
    系统分析与设计——作业4
    从循环添加事件谈起对JS闭包的理解
    对JS prototype的理解
    系统分析与设计——作业2
    JavaScript 预编译(变量提升和函数提升的原理)
  • 原文地址:https://www.cnblogs.com/qixin622/p/651408.html
Copyright © 2011-2022 走看看