zoukankan      html  css  js  c++  java
  • 单向链表

    *win-tc*/
    #include <stdio.h>
    #include <malloc.h>
    #include <stdlib.h>
    #define Esc 27       /*定义键盘退出键Esc*/
    #include"ctype.h"
    #define NULL 0
    #define LEN sizeof(struct student)
      struct student
      {  long num;
          float score;     
          struct student *next;
      };
      int n;


      /*创建链表*/
      struct student *creat()
      {
        struct student *head;     
        struct student *p1,*p2;     
        n=0;
        p1=p2=( struct student*) malloc(LEN);
        scanf("%ld,%f",&p1->num,&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,%f",&p1->num,&p1->score);
           }
        p2->next=NULL;     
        return(head);
        }


        /*打印链表*/
        void print(struct student *head)
         {
          struct student *p;
          printf("\nNow,These %d records are:\n",n);         
          p=head;
          if(head!=NULL)
              do
                 {
                   printf("%ld %5.1f\n",p->num,p->score);
                   p=p->next;
                  }while(p!=NULL);
           printf("\npress a key to continue.");
           getch();
         }


        /*删除链表元素*/
        struct student *del(struct student *head,long num)
        {
           struct student *p1,*p2;
           if (head==NULL)
             {
               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;
                   printf("delete:%ld\n",num);    
                   n=n-1;
                 }
              else
                 printf("%ld not been found!\n",num);
            return(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 *find(struct student *head, long num)
         {
              struct student *p1,*p2;

              p1=head;

              if(head==NULL)
              {
                    printf("\nlist null!\n");
                    return(head);
              }
              else
              {
                    while(num!=p1->num && p1->next!=NULL)
                    {
                        p2=p1;
                        p1=p1->next;
                    }
                    if(num==p1->num)
                    {
                        printf("have fond the number is:\n%ld %5.1f\n",p1->num,p1->score);
                        return(head);
                    }
                    else
                    {
                        printf("the find number is not in the list!\n");
                        return(head);
                    }
              }
           }


           /*改变链表元素的成绩*/
           struct student *change(struct student *head, long num)
           {
              struct student *p1,*p2;
              struct student stu;
              if(head==NULL)
              {
                    printf("\nlist null!\n");
                    return(head);
              }
              else
              {
                    while(num!=p1->num && p1->next!=NULL)
                    {
                        p2=p1;
                        p1=p1->next;
                    }
                    if(num==p1->num)
                    {
                        printf("the before score is:%5.1f\n",p1->score);
                        printf("please input the change score!\n");
                        scanf ("%f",&stu.score) ;
                        p1->score=stu.score ;
                        return(head);
                    }
                    else
                    {
                        printf("the find number is not in the list!\n");
                        return(head);
                    }
              }  
           }

    /*主涵数*/
    void main()
         {     
         struct student *head,stu;
         long  del_num;
         long  find_num;
         long  change_num;
         char ckey='a';
         int istate=0;
         do
         {
            clrscr();
            printf("1:intput records:\n");
            printf ("2:intput the  deleted number:\n");
            printf ("3:intput the inserted number:\n");
            printf("4:please input the finding number:\n");
            printf("5:please input the change number:\n");
            printf("6:output records:\n");
            printf("Esc:exit\n");
            printf("please input your choose(1-4)\n\n");
            ckey=getch();
            if(ckey=='1')    /*创建*/
            {
            printf("please input records(num,score),by the end of 0:\n\n");
            head=creat();
            print(head);
            istate=1;
            }
            else if((istate==0)&&(ckey!=Esc))
            {
                printf("\nERROR:you must input records first!");
                printf("\npress a key to continue.");
                getch();
            }
            else if(ckey=='2')   /*删除*/
            {
            printf ("\nplease intput the  deleted number:\n\n");
            scanf ("%ld",&del_num);
            head=del(head,del_num);
            print(head);
            }
            else if(ckey=='3')   /*插入*/
            {
            printf (" \nplease intput the inserted number(num,score):\n\n");
            scanf ("%ld,%f",&stu.num,&stu.score) ;
            head=insert(head,&stu);
            print(head);
            }
            else if(ckey=='4')   /*查找*/
            {
            printf("  \nplease input the finding number:\n\n");
            scanf ("%ld,",&find_num) ;
            head=find(head,find_num);
            print(head);
            }
            else if(ckey=='5')    /*改变*/
            {
            printf("  \nplease input the change number:\n\n");
            scanf ("%ld,",&change_num) ;
            head=change(head,change_num);
            print(head);
            }
            else if(ckey=='6')    /*打印*/
            {
                print(head);   
            }
         }
         while(ckey!=Esc);       /*按键盘上的Esc键退出*/
    }

  • 相关阅读:
    Codeforces 777B Game of Credit Cards
    Codeforces 777A Shell Game
    零基础学贪心算法
    1283 最小周长
    容斥原理
    Humble Numbers(丑数) 超详解!
    1284 2 3 5 7的倍数
    1305 Pairwise Sum and Divide
    1347 旋转字符串
    HDU 2549 壮志难酬
  • 原文地址:https://www.cnblogs.com/greencolor/p/1686661.html
Copyright © 2011-2022 走看看