学习日志以及个人总结 (16)

共用体

共用体
union 共用体名
{
成员列表;
};//表示定义一个共用体类型


注意:
1.共用体
 初始化 --- 只能给一个值,默认是给到第一个成员变量
2.共用体成员变量辅助
3.可以判断大小端 ----※!!
  实际用途 :节省空间
             进行数据转换
 
 比如:
 struct stu
 {
    char name[20];
    int sno;
    float score;
 };
 struct teacher
 {
    char name[20];
    int Tno;
    float salary;
 };       
 ----------------------
 
 struct member
 {
    char name[20];
    int no;
    
    union
    {
        float score;
        float salary;
    }d;
    
 };

 4.共用体变量作为函数参数,也可以是函数返回值类型
 
    共用体结构体类型定义出来之后
    a.定义变量
    b.定义数组
    c.定义指针
    d.做函数参数,返回值类型

共用体判断大小端

  1 #include<stdio.h>2 3 union demo4 {5     int a;6     char b;7     short c;8 };9 10 int isEnddian(vodi)11 {12     union demo13     {14         int a;15         char b;16     }d;17 18     d.a=1;19     return d.b;                                                                                                                                                                                         20 }21 int main(void)22 {23     union demo d={'a'};24 25     d.a=0x12345678;26     d.b='a';27     d.c=0x99;28     printf("a=%#x\n",d.a);29     printf("b=%c:%#hhx\n",d.b,d.b);30     printf("a=%#x\n",d.c);31 }

枚举

枚举:一枚一枚的列举 

enum //枚举   实际上是一种数组类型(int)
enum 枚举类型名
{
    
};

提高代码可读性。本质:int类型 所以枚举和整型类型兼容

不足 :因为枚举类型--本质上是一个整型类型
所以枚举类型变量的值 并不能真正限定在 指定的那些值范围中。
 

例题无人机

 1 #include<stdio.h>2 enum nopeopleFly3 {4     flying,5     stop,6     holding7 };8                                                                                                                                                                                                         9 int main(void)10 {11     enum nopeopleFly n;12     int a ;13     scanf("%d",&a);14     n=a;15     switch(n)16     {17         case 0:18     printf("flying\n");19         break;20         case 1:21     printf("stop\n");22         break;23         case 2:24     printf("holding\n");25         break;26     }27 28     return 0;29 }

链表

链表:链式的数据表
 
 狗链! //寻找数据
 
优点:增加 删除数据较为方便
缺点:找数据不方便
 
存放链式数据的结构
节点[数据|另外一个节点指针] 
 
 [数据域|指针域]
 
 节点:
 struct Node
 {
//数据域
    struct stu s;
//指针域
    struct Node *next;
 };
 
 数据结构对应算法 --- 操作
 
 增加数据就是增加节点
 删除数据就是减少节点
 
 操作:
 1,创建一个链表---空链表
 //有头链表 ---更方便处理链表
 //无头链表  
 
 c语言阶段:
 有头 单向链表
 
 空链表的特点为:只有头节点 并且头节点指针域为NULL //即相当于尾节点
 
 2.插入
    创建一个新的节点
    将节点链接起来


    实现链表操作


void pushBack(struct Node *head)
{    //尾插
    S1:创建一个新的节点
    struct     Node *pNew = malloc(sizeof(struct Node));
                                    栈上
    S2:找尾节点
    struct Node *p =&head;//此时p在头节点
    p = p->next;
    while(p->next != NULL)
    {
        p = p->next;//让P指向下一个节点
    }
    S3.链接到尾节点后面
    p->next = pNew;
    pNew->next = NULL;//尾节
}    
    //链表不放入栈上  放入堆
    
    int length(struct Node *head)
    {
        //统计有效节点的个数
    }
 
    //头插 
    void pushFront(struc Node *head ,int data)
    {
    //1.创建新节点
    pNew
    //2.链接
    pNew->next = p->next;
    p->next = pNew;
    }

 1 #include<stdio.h>                                                                                                                                                                                       2 #include<stdlib.h>3 struct Node4 {5     int data;6     struct Node *next;7 };8 9 void pushBack(struct Node *head,int data)10 {  //创建新节点11     struct Node *pNew = malloc(sizeof(struct Node));//放在堆区12     pNew->data =data;13     //链接14     struct Node *p =head;//此时p在头节点15 16     while(p->next != NULL)17     {18         p = p->next;//让p指向下一个节点19     }20     p->next = pNew;21 22     pNew->next = NULL;23 }24 25 int isEmpty(struct Node *head)26 {27     if(head->next == NULL)   //判断是否是空链表28     {29         return 1;30     }else31     {32         return 0;33     }34 }35 36 void printLinklist(struct Node *head)37 {38     struct Node *p =head;39     if(isEmpty(head) == 0)40     {41         p=head->next;42         while(p != NULL)43         {44             printf("%d\n",p->data);45             p=p->next;46         }47     }48 }49 50 int lengh(struct Node *head)51 {52     struct Node *p=head->next;53     int c=0;54     while(p!=NULL)55     {56         c++;57         p=p->next;58     }59     return c;60 }61 62 void pushFront(struct Node *head,int data)63 {64     struct Node *pNew = malloc(sizeof(struct Node));65     pNew->data = data;66 67     struct Node *p=head;68     pNew->next = head->next;69     p->next = pNew;70 71 }72 73 int main(void)74 {75     struct Node head;76     head.next = NULL;//空链表77     pushBack(&head,1); // 将一个新元素加到 head后面 78     pushBack(&head,2);79     pushBack(&head,3);80     pushBack(&head,4);81     pushBack(&head,5);82     printf("lengh = %d\n",lengh(&head));83     printLinklist(&head);84     return 0;85 }