美文网首页
(c基础)上课笔记 12.20

(c基础)上课笔记 12.20

作者: 霸王小 | 来源:发表于2016-12-20 16:44 被阅读21次
    • 结构体

    基本定义:
    struct 结构名
    {
    //成员列表
    };
    成员列表:有基本数据类型定义的变量或者是构造类型的变量
    example:
    struct student
    {
    int grade;
    int age;
    char name[32];
    };
    student :结构体名称
    ----结构体变量可以直接赋值给相同类型的结构体变量。
    struct student :结构数据类型,相当于int,double,char等基本数据类型;
    struct student stu; stu为结构体变量
    访问结构体成员:
    stu.name;
    stu.grade;
    stu.age;

    • 结构体的初始化1

    struct student
    {
    char name[32]
    char sex;
    int age;
    };
    struct student boy;
    strcpy(boy.name,"jeck");
    boy.age=24;
    boy.sex='m';

    • 初始化2

    struct student girl={"jecl"}

    #include <stdio.h>
    #include <string.h>
    struct student 
    {
        char name[32];
        char sex;
        int age;
    };
    int main()
    {
        struct student stu;
        strcpy(stu.name,"lisan");
        stu.sex='m';
        stu.age=24;//结构体的初始化
        printf("name %s,sex %c,age %d\n",stu.name,stu.sex,stu.age);
        printf("方法二\n");
        //初始化必须要和成员列表一一对应。
        struct student stu1={"lily",'f',20};//结构体的初始化
        printf("name %s,sex %c,age %d\n",stu1.name,stu1.sex,stu1.age);
        return 0;
    }
    
    • 初始化3(不建议):定义的同时初始化

    #include <stdio.h>
    #include <string.h>
    struct student 
    {
        char name[32];
        char sex;
        int age;
    }stu,stu1,stu2;//stu={"xiaoming",'m',23};
    
    • 无名结构体

    (一般不使用)

    #include <stdio.h>
    #include <string.h>
    struct
    {
        char name[32];
        int age;
    }stu;
    
    • 宏定义结构体

    #include <stdio.h>
    #include <string.h>
    struct student
    {
        char name[32];
        int age;
    };
    #define STU struct student
    STU stu;
    
    • 结构体的嵌套

    #include <stdio.h>
    #include <string.h>
    struct date
    {
        int year;
        int month;
        int day;
    };
    struct sutdent
    {
        char name[32];
        int age;
        struct date birthday;
    };
    int main()
    {
        struct student sut;
        strcpy(stu.name,"hello")'\;
        stu.age=34;
        stu.birthday.year=1900;
        stu.birthday.month=5;
        stu.birthday.day=6;
        printf("%s,%d,%d,%d,%d\n",stu.name,sut.birthday.year,sut.birthday.month,stu.birthday.day);
        return 0;
    }
    
    • 嵌套定义结构体

    struct sutdent
    {
        int a;
        char b;
        struct student stu;//无法确定大小,不能分配空间
    };
    struct sutdent1
    {
        int a;
        char b;
        struct student1 *ps;//所以的指针类型大小都为8个字节
    };
    
    • 结构体数组

    #include <stdio.h>
    #include <string.h>
    struct sutdent
    {
            int age;
        char name[32];
    };
    int main()
    {
        struct student stu[3]=
        {
         {24,"hello"},
         {20,"hello"},
         {26,"hello"},
        };
        //结构体数组的访问
        printf("stu[1]=%d,stu[1]=%s.\n",stu[1].age,stu[1].name);
        return 0;
    }
    
    • 结构体指针

    #include <stdio.h>
    #include <string.h>
    struct date
    {
        int year;
        int month;
        int day;
    };
    struct sutdent
    {
        char name[32];
        int age;
        struct date birthday;
    };
    void main()
    {
       struct sutdent stu;
       struct sutdent *p=&stu;//空指针不能访问其值
       strcpy(p->name,"hello");
       p->age=13;
       p->birthday.year=1990;
       p->birthday.month=2;
       p->birthday.day=13;
       printf("%s,%d,%d,%d,%d\n",p->name,p->age,p->birthday.year,p->birthday.month,p->birthday.day);
    }
    
    • 堆栈

    #include <stdio.h>
    #include <stdlib.h>
    malloc;//申请堆空间
    free;//释放空间
    pa=(struct date *)malloc(sizeof(struct date));//申请一个大小为sizeof(struct date)的堆空间。
    free(pa);//释放申请的堆空间
    
    • typedef

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    {
       typedef int I;//相当于把int重新定义了名称为I.
       I a=4;
       int b=5;
       I c=a+b;
       printf("a=%d,b=%d,c=%d.",a,b,c);
       return 0;
    }
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct sutdent
    {
       int age;
       char name[23];
    }STU;
    /*
    struct sutdent
    {
       int age;
       char name[23];
    };
    typedef struct sutdent STU;//这样定义是一样的
    */
    int main()
    {
       STU stu;
       strcpy(stu.name,"wang");
       stu.age=34;
       printf("name=%s,age=%d.\n",stu.name,stu.age);
       return 0;
    }
    

    ---- 和宏定义的区别
    typedef struct student STU;
    #define STU struct student

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct sutdent
    {
       int age;
       char name[23];
    }STU;
    typedef struct student* STT;
    #define STD struct student*
    int main()
    {
       STT stu1,stu2;//stu1和stu2都是结构体指针
       STD stu3,stu4;//struct student *stu3,stu4  stu3是指针,stu4是变量
       printf("name=%s,age=%d.\n",stu.name,stu.age);
       return 0;
    }
    
    • 结构体大小

    内存对其:
    Linux: 4字节
    Windows: 8字节
    默认从偏移量为0的位置开始储存,每个成员

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct A    //8
    {           //[0-3]
        char a; //[0]
        int b;  //[4-7]
    };
    struct B    //12
    {
        char a; //[0]
        int c;  //[4-7]
        char b; //[8]
    };
    struct C
    {
        int a;
        char b;
    };
    struct D    //8
    {
        char a; //[0]
        char b; //[1]
        int c; //[4-7]
    };
    struct E   //8
    {
        char a; //[0]
        int c; //[4-7]
        char b[23]; //[1]
    };
    struct F    //6
    {
        char a; //[0]
        short c //[2-3]
        char b; //[4]
    };
    struct G    //8
    {
        char a; //[0]
        char b; //[1]
        short c; //[2-3]
    };
    int main()
    {
       printf("A:%ld\n",sizeof(struct A));
       printf("B:%ld\n",sizeof(struct B));
       printf("C:%ld\n",sizeof(struct C));
       printf("D:%ld\n",sizeof(struct D));
    }
    
    • 联合体union untype

    {
    int a;
    long b;
    int arr[4];//联合体的内存大小以最大的为准
    };
    特点,每次只能操作一个成员变量。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    union untype
    {
       int a;
       double b;
       char c;
    };//最多只能操作一个变量
    int main()
    {
       union untype un;
       un.a=123;
       un.b=3.14;
       un.c='f';
       printf("a=%d,b=%lf,c=%c.\n",un.a,un.b,un.c);//此次错误,不管是赋值还是输出同一时间只能访问一个值。
       return 0;
    }
    
    • 枚举类型

    #include <stdio.h>
    enum entype//成员一般用大写
    {
       A,   //0
       B,   //1
       C=12,
       D,   //13
       E    //14 
    };
    int main()
    {
       printf("A=%d\n",A);
       printf("B=%d\n",B);
       printf("C=%d\n",C);
       printf("D=%d\n",D);
       printf("E=%d\n",E);
       return 0;
    }
    

    ----与switch的连用

    #include <stdio.h>
    enum entype//成员一般用大写
    {
       FIRST=1,   //0
       SEC,   //1
       THIRD
    };
    int main()
    {
       int op=0;
       printf("input op:");
       scanf("%d",&op);
       switch(op)
       {
          case FIRST:printf("第一名!\n");break;
          case SEC:printf("第二名!\n");break;
          case THIRD:printf("第三名!\n");break;
          default :printf("谢谢参与!\n");break;
       }
       return 0;
    }
    

    ----定义enum型变量

    #include <stdio.h>
    enum entype//成员一般用大写
    {
       A,
       B,
       C
    };
    int main()
    {
       enum type en;//定义变量
       en=A;
       return 0;
    }
    
    • 链表

    ----链式存储结构,线性存储结构,其大小可动态改变,链表是由一个个节点串起来的数据链。
    ----节点:由数据域和指针域组成。
    ----数据域:存放数据。
    ----指针域:存放下一个节点的地址

    • 创建链表

    #include <stdio.h>
    struct student
    {
        int id;
        struct student *next;
    }
    struct student *head;
    

    malloc():申请空间
    free():关闭空间
    创建一个头节点:

    struct student *head;
    head=(struct student *)/*强制类型转换*/malloc(sizeof(struct student));
    

    头节点标识一个链表,即链表名称。
    头节点的数据域不存放数据,指针域存放

    #include <stdio.h>
    #include <stdlib.h>//malloc();,free();
    struct student 
    {
       int ID;
       char name[32];
       struct student *next;
    };
    #define LEN sizeof(struct student)
    struct student *add_link(struct student *head)
    {
      struct student *temp=(struct student *)malloc(LEN);
      printf("input ID:");
      scanf("%d",&temp->ID);
      printf("input name:");
      scanf("%s",temp->name);
      temp->next=head->next;
      head->next=temp;
      temp=NULL;
      return head;
    }
    void show_link(struct student *head)
    {
       struct student *p=head->next;
       while(p!=NULL)
       {
        printf("\t%d\t%s\n",p->ID,p->name);
        p=p->next;
       }
    }
    int main()
    {
       struct student *head;//创建链表
       head=(struct student *)malloc(LEN);
       head->next=NULL;//需要将next指针置空,防止野指针
       head=add_link(head);
       show_link(head);
       return 0;
    }
    
    • 作业

    1.尾增

    #include <stdio.h>
    #include <stdlib.h>
    typedef struct student
    {
        int id;
        char name[32];
        int num;
        struct student *next;
    }STU,*pSTU;
    
    pSTU add(pSTU head)
    {
        pSTU temp=(pSTU)malloc(sizeof(STU));
        printf("in put ID:");
        scanf("%d",&temp->id);
        printf("in put name:");
        scanf("%s",temp->name);
        printf("in put num:");
        scanf("%d",&temp->num);
        temp->next=head->next;
        head->next=temp;
        temp=NULL;
        return head;
    }
    void show_add(pSTU head)
    {
        pSTU p=head->next;
        while(1)
        {
        if(p!=NULL)
        {
           printf("\t%d\t%s\t%d\n",p->id,p->name,p->num);
           p=p->next;
        }
           else break;
        }
    }
    void show_del(pSTU head)
    {
        pSTU p=head->next;
        head->next=p->next;
        free(p);
        p=NULL;
    }
    void main()
    {
        int i,n;
        pSTU head=(pSTU)malloc(sizeof(STU));
        head->next=NULL;
        for(i=0;i<3;i++)
        head=add(head);
        show_add(head);
        printf("\n");
        show_del(head);
        show_add(head);
    }
    

    2.尾减

    #include <stdio.h>
    #include <stdlib.h>
    typedef struct student
    {
       int ID;
       char name[10];
       struct student *next;
    }STU,*pSTU;
    pSTU add_STU(pSTU head)
    {
        pSTU temp=(pSTU)malloc(sizeof(STU)),p=head;
        temp->next=NULL;
        printf("in put(ID,name) :");
        scanf("%d,%s",&temp->ID,temp->name);
        while(p->next!= NULL)
        {
         p=p->next;
        }
         p->next=temp;
         temp=NULL;
        return head;
    }
    void show_del_STU(pSTU head)
    {
      pSTU p=head,p1=head->next->next;
      if(head->next!=NULL)
      {
      while(p1!=NULL)
      {
      p=p->next;
      p1=p1->next;
      }
      free(p->next);
      p->next=NULL;
      }
    }
    void show_add_STU(pSTU head)
    {
        pSTU p=head->next;
        while(1)
        {
           if(p!=NULL)
           {
           printf("%5d,%s\n",p->ID,p->name);
           p=p->next;
           }
           else break;
        }
    }
    void main()
    {
        pSTU head=(pSTU)malloc(sizeof(STU));
        head->next=NULL;
        int i;
        for(i=0;i<5;i++)
        head=add_STU(head);
        show_add_STU(head);
        printf("\n");
        show_del_STU(head);
        show_add_STU(head);
    }
    

    相关文章

      网友评论

          本文标题:(c基础)上课笔记 12.20

          本文链接:https://www.haomeiwen.com/subject/wrexvttx.html