C语言复习03(小甲鱼版本)

单链表

int main(void){
     
    struct Test{
     
        int x;
        int y;
        //错误写法:struct Test test;
        struct Test *test;//正确写法,指针指向结构体
    }
}

链表是一种常见的数据结构,根据需求,我们可以构造出单链表、双链表、循环链表和块状链表等。链表的出现很大程度上弥补了数组的先天不足。

[head]———>>[[信息域(存放信息)] [指针(指向链表的下一个元素)]]———>>[[信息域] [指针]]———>>[[信息域] [指针]]———>>[NULL]

//头插法

#include 
//建立节点
struct Book{
     
    char title[128];
    char author[40];
    struct Book *next;
};

void getInput(struct Book *book){
     
    printf("请输入书名:");
    scanf("%s", book->title);
    printf("请输入作者:");
    scanf("%s", book->author);
}

//在单链表中插入元素
//形参字面上的意思:(*library)是头节点指针library进行一层解引用,然后对其指向的内容(地址)再进行一次解引用
void addBook(struct Book **library) {
     
    struct Book *book, *temp;
    
    //新建一个新的节点
    book = (struct Book *)malloc(sizeof(struct Book));
    
    if (book == NULL){
     
        printf("内存分配失败了\n");
        exit(1);
    }
    
    //填充内容
    getInput(book);
    
    if (*library != NULL){
     
        temp = *library;
        *library = book;
        book->next = temp;
    }
    else{
     
        *library = book;
        book->next = NULL;
    }
}

void printLibrary(struct Book *library){
     
    struct Book *book;
    int count = 1;
    
    book = library;
    while (book != NULL){
     
        printf("Book%d:", count);
        printf("书名:%s:", book->title);
        printf("作者:%s:", book_>author);
        book = book->next;
        count++;
    }
}

void releaseLibrary(struct Book **library){
     
    struct book *temp;
    
    while (*library != NULL){
     
        temp = *library; 
        *library = (*library) -> next;
        free(temp);
    }
}
int main(void){
     
    //library作为头节点指针进行解引用,得到其存放的值,即下一个节点的地址指针
    struct Book *library = NULL;
    
    while(1){
     
        printf("请问是否需要录入书籍信息(Y/N):");
        do{
     
            ch = getchar();
        }while (ch != 'Y' && ch != 'N');
        
        if (ch == 'Y'){
     
            addBook(&library);
        }
        else{
     
            break;
        }
    }
    
    while(1){
     
        printf("请问是否需要打印书籍信息(Y/N):");
        do{
     
            ch = getchar();
        }while (ch != 'Y' && ch != 'N');
        
        if (ch == 'Y'){
     
            printLibrary(library);
        }
        
        releaseLibrary(&library);
        
    }
    
    return 0;
}

观察打印顺序

//尾插法

#include 
//建立节点
struct Book{
     
    char title[128];
    char author[40];
    struct Book *next;
};

void getInput(struct Book *book){
     
    printf("请输入书名:");
    scanf("%s", book->title);
    printf("请输入作者:");
    scanf("%s", book->author);
}

//在单链表中插入元素
//形参字面上的意思:(*library)是头节点指针library进行一层解引用,然后对其指向的内容(地址)再进行一次解引用
void addBook(struct Book **library) {
     
    struct Book *book, *temp;
    
    //新建一个新的节点
    book = (struct Book *)malloc(sizeof(struct Book));
    
    if (book == NULL){
     
        printf("内存分配失败了\n");
        exit(1);
    }
    
    //填充内容
    getInput(book);
    
    if (*library != NULL){
     
        temp = *library;
        //定位单链表的尾部位置
        while (temp->next != NULL){
     
            temp = temp->next;
        }
        //插入数据
        temp->next = book;
        book->next = NULL;
    }
    else{
     
        *library = book;
        book->next = NULL;
    }
}

void printLibrary(struct Book *library){
     
    struct Book *book;
    int count = 1;
    
    book = library;
    while (book != NULL){
     
        printf("Book%d:", count);
        printf("书名:%s:", book->title);
        printf("作者:%s:", book_>author);
        book = book->next;
        count++;
    }
}

void releaseLibrary(struct Book **library){
     
    struct book *temp;
    
    while (*library != NULL){
     
        temp = *library; 
        *library = (*library) -> next;
        free(temp);
    }
}
int main(void){
     
    //library作为头节点指针进行解引用,得到其存放的值,即下一个节点的地址指针
    struct Book *library = NULL;
    
    while(1){
     
        printf("请问是否需要录入书籍信息(Y/N):");
        do{
     
            ch = getchar();
        }while (ch != 'Y' && ch != 'N');
        
        if (ch == 'Y'){
     
            addBook(&library);
        }
        else{
     
            break;
        }
    }
    
    while(1){
     
        printf("请问是否需要打印书籍信息(Y/N):");
        do{
     
            ch = getchar();
        }while (ch != 'Y' && ch != 'N');
        
        if (ch == 'Y'){
     
            printLibrary(library);
        }
        
        releaseLibrary(&library);
        
    }
    
    return 0;
}

上述尾插法每次插入新元素都需要遍历一次链表,耗时多,是否可以记录下尾节点的地址呢

void addBook(struct Book **library){
     
    struct Book *book;
    //为防止每次进入函数,tail都会被初始化,我们将其变成静态变量:直到程序退出才释放,不然永久保存上一次的值
    static struct Book *tail;
    
     book = (struct Book *)malloc(sizeof(struct Book));
    
    if (book == NULL){
     
        printf("内存分配失败了\n");
        exit(1);
    }
    
    //填充内容
    getInput(book);
    
    if (*library != NULL){
     
        tail->next = book;
        book->next = NULL;
    }
    else{
     
        *library = book;
        book->next = NULL;
    }
    tail = book;
}

搜索单链表

//尾插法

#include 
#include 
//建立节点
struct Book{
     
    char title[128];
    char author[40];
    struct Book *next;
};

void getInput(struct Book *book){
     
    printf("请输入书名:");
    scanf("%s", book->title);
    printf("请输入作者:");
    scanf("%s", book->author);
}

//在单链表中插入元素
//形参字面上的意思:(*library)是头节点指针library进行一层解引用,然后对其指向的内容(地址)再进行一次解引用
void addBook(struct Book **library){
     
    struct Book *book;
    //为防止每次进入函数,tail都会被初始化,我们将其变成静态变量:直到程序退出才释放,不然永久保存上一次的值
    static struct Book *tail;
    
     book = (struct Book *)malloc(sizeof(struct Book));
    
    if (book == NULL){
     
        printf("内存分配失败了\n");
        exit(1);
    }
    
    //填充内容
    getInput(book);
    
    if (*library != NULL){
     
        tail->next = book;
        book->next = NULL;
    }
    else{
     
        *library = book;
        book->next = NULL;
    }
    tail = book;
}

void printLibrary(struct Book *library){
     
    struct Book *book;
    int count = 1;
    
    book = library;
    while (book != NULL){
     
        printf("Book%d:", count);
        printf("书名:%s:", book->title);
        printf("作者:%s:", book_>author);
        book = book->next;
        count++;
    }
}

struct Book *searchBook(struct Book *libraary, char *target){
     
    struct Book *book;
    
    book = library;
    while(book != NULL){
     
        if (!strcmp(book->title, target) || !strcmp(book->author, target)){
     
            break;
        }
        book = book->next;
    }
    return book;
}

void printBook(struct Book *book){
     
    pritnf("书名:%s", book->title);
    pritnf("作者:%s", book->author);
}

void releaseLibrary(struct Book **library){
     
    struct book *temp;
    
    while (*library != NULL){
     
        temp = *library; 
        *library = (*library) -> next;
        free(temp);
    }
}
int main(void){
     
    //library作为头节点指针进行解引用,得到其存放的值,即下一个节点的地址指针
    struct Book *library = NULL;
    struct Book *book;
    char input[128];
    int ch;
    
    while(1){
     
        printf("请问是否需要录入书籍信息(Y/N):");
        do{
     
            ch = getchar();
        }while (ch != 'Y' && ch != 'N');
        
        if (ch == 'Y'){
     
            addBook(&library);
        }
        else{
     
            break;
        }
    }
    
    while(1){
     
        printf("请问是否需要打印书籍信息(Y/N):");
        do{
     
            ch = getchar();
        }while (ch != 'Y' && ch != 'N');
        
        if (ch == 'Y'){
     
            printLibrary(library);
        }
        
        printf("\n请输入书名或作者:");
        scanf("%s", input);
        
        book = searchBook(library, input);
        if(book == NULL){
     
            printf("没找到\n");
        }
        else{
     
            do{
     
                printf("已找到\n");
                printBook(book);
            }while((book = searchBook(book->next, input)) != NULL);
        }
        
        releaseLibrary(&library);
        
        return 0;
    }
    
    return 0;
}

插入与删除

//按大小插入

#include 
#include 

struct Node{
     
    int value;
    struct Node *next;
};//结构体写完后要加分号

void insertNode(struct Node **head, int value){
     
    struct Node *previous;
    struct Node *current;
    struct Node *new;
    
    current = *head;
    previous = NULL;
    
    while (current != NULL && current->value < value){
     
        //记录current的上一个地址
        previous = current;
        //current指向原先的下一个节点
        current = current->next;
    }
    
    new = (struct Node *)malloc(sizeof(struct Node));
    if (new == NULL){
     
        printf("内存分配失败\n");
        exit(1);
    }
    
    new->value = value;
    new->next = current;
    
    if (previous == NULL){
     
        *head = new;
    }
    else{
     
        previous->next = new;
    }
}

void deleteNode(struct Node **head, int value){
     
    struct Node *previous;
    struct Node *current;
    
    current = *head;
    previous = NULL;
    
    while(current != NULL && current->value != value){
     
        previous = current;
        current = current->next;
    }
    
    if (current == NULL){
     
        printf("找不到匹配的节点!\n");
        return;
    }
    else{
     
        if (previous == NULL){
     
            *head = current->next;
        }
        else{
     
            previous->next = current->next;
        }
        
        free(current);
    }
}

void printNode(struct Node *head){
     
    struct Node *current;
    
    current = head;
    while (current != NULL){
     
        printf("%d", current->value);
        current = current->next;
    }
    
    putchar('\n');
}

int main(void){
     
    struct Node *head = NULL;
    int input;
    
    printf("开始测试插入整数...\n");
    
    while(1){
     
        printf("请输入一个整数(输入-1表示结束):");
        scanf("%d", &input);
        if(input == -1){
     
            break;
        }
        insertNode(&head, input);
        printNode(head);
    }
    
    printf("开始测试删除整数...\n");
    while(1){
     
        printf("请输入一个整数(输入-1表示结束):");
        scanf("%d", &input);
        if(input == -1){
     
            break;
        }
        deleteNode(&head, input);
        printNode(head);
    }
    return 0;
}

内存池

内存池其实就是让程序额外维护一个缓存区域

通常可使用单链表来维护一个简单的内存池。

只需要将没有用的内存空间地址依次用一个单链表记录下来;当再次需要的时候,从这个单链表中获取即可

通讯录管理程序

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-loZ0bPfS-1599027412784)(C:\Users\hp\Desktop\1597029754(1)].png)

#include 
#include 
#include 

#define MAX 1024

struct Person{
     
    char name[40];
    char phone[20];
    struct Person *next;
}

struct Person *pool = NULL;//内存池
int count;

void getInput(struct Person *person);
void printPerson(struct Person *person);
void addPerson(struct Person **contacts);
void changePerson(struct Person *contacts);
void delPerson(struct Person **contacts);
struct Person *findPerson(struct Person *contacts);
void displayContacts(struct Person *contacts);
void releaseContacts(struct Person **contacts);
void releasePool(void);

void getInput(struct Person *person){
     
    printf("输入姓名:");
    scanf("%s", person->name);
    peintf("输入号码:");
    scanf("%s", person->phone);
}

void addPerson(struct Person **contacts){
     
    struct Person *person;
    struct Person *temp;
    
    //如果内存池非空,则直接从里面获取空间
    if (pool != NULL){
     
        person = pool;
        pool = pool->next;
        count--;
    }
    
    //如果内存池为空,则调用malloc函数申请新的内存空间
    else{
     
    	person = (struct Person *)malloc(sizeof(struct Person));
    	if (person == NULL){
     
        	printf("内存分配失败!\n");
        	exit(1);
    	}
    }
    
    getInput(person);
    
    //将person用头插法添加到通讯录中
    if (*contacts != NULL){
     
        temp = *contacts;
        *contacts = person;
        person-> = temp;
    }
    else{
     
        *contacts = person;
        person-> = NULL;
    }
}

void printPerson(struct Person *person){
     
    printf("联系人:%s\n", person->name);
    printf("电话:%s\n", person->phone);
}

struct Person *findPerson(struct Person *contacts){
     
    struct Person *current;
    char input[40];
    
    printf("请输入联系人:");
    scanf("%s", input);
    
    current = contacts;
    while (current != NULL && strcmp(current->name, input)){
     
        current = current->next;
    }
    
    return current;
}

void changePerson(struct Person *contacts){
     
    struct Person *person;
    
    person = findPerson(contacts);
    if (person == NULL){
     
        printf("找不到该联系人!\n");
    }
    else{
     
        printf("请输入新的联系电话:");
        scanf("%s", person->phone);
    }
}

void delPerson(struct Person **contacts){
     
    struct Person *temp;
    struct Person *person;
    struct Person *current;
    struct Person *previous;
    
    //先找到待删除的节点指针
    person = findPerson(*contacts);
    if (person == NULL){
     
        printf("找不到该联系人!\n");
    }
    else{
     
        current = *contacts;
        previous = NULL;
        
        //current定位到待删除的节点
        while (current != NULL && current != person){
     
            previous = current;
            current = current->next;
        }
        
        if (previous == NULL){
     
            //待删除的节点是第一个节点
            *contacts = current->next;
        }
        else{
     
            //待删除的节点不是第一个节点
            previous->next = current->next;
        }
        
        //判断内存池是否有空位
        if(count < MAX){
     
            if (pool != NULL){
     
                temp = pool;
                pool = person;
                person->next = temp;
            }
            else{
     
                pool = person;
                person->next = NULL;
            }
            count++;
        }
        else{
     
            free(person);
        }
    }
}

void displayContacts(struct Person *contacts){
     
    struct Person *current;
    
    current = contacts;
    while (current != NULL){
     
        printPerson(current);
        current = current->next;
    }
}

void releaseContacts(struct Person **contacts){
     
    struct Person *temp;
    
    while (*contacts != NULL){
     
        temp = *contacts;
        *contacts = (*contacts)->next;
        free(temp);
    }
}

void releasePool(void){
     
    struct Person *temp;
    
    while (pool != NULL){
     
        temp = pool;
        pool = pool->next;
        free(temp);
    }
}
int main(void){
     
    int code;
    struct Person *contacts = NULL;
    struct Person *person;
    
    printf("|  欢迎使用通讯录管理程序 |\n");
    printf("|--- 1:插入新的联系人 ---|\n");
    printf("|--- 2:查找已有联系人 ---|\n");
    printf("|--- 3:更改已有联系人 ---|\n");
    printf("|--- 4:删除已有联系人 ---|\n");
    printf("|--- 5:显示当前通讯录 ---|\n");
    printf("|--- 6:退出通讯录程序 ---|\n");
    printf("| Powered by FishC.com |\n");
    
    while(1){
     
        printf("\n请输入指令代码:");
        scanf("%d", &code);
        switch (code){
     
            case 1: addPerson(&contacts); break;
            case 2: person = findPerson(contacts);
                if (person == NULL){
     
                    printf("找不到该联系人!\n");
                }
                else{
     
                    printPerson(person);
                }
                break;
                
            case 3: changePerson(contacts); break;
            case 4: delPerson(&contacts); break;
            case 5: displayContacts(contacts); break;
            case 6: goto END;
        }
    }
END:
    releaseContacts(&contacts);
    releasePool();
    
    return 0;
}

typedef

C语言最重要的关键字之一

#include 

//定义int的别名
typedef int integer;
/*其实用define也可以,但是两者的本质是不一样的
#define integer int
*/

int main(void){
     
    integer a;
    int b;
    
    a = 520;
    b = a;
    
    printf("a = %d\n", a);
    printf("b = %d\n", a);
    printf("size of a = %d\n", sizeof(a));
    
    returb 0;
}

/*输出
a = 520
b = 520
size of a = 4*/

对比差别:

#include 

#define integer int
//若直接用typedef会报错

int main(void){
     
    unsigned integer a;
    
    a = -1;
    printf("a = %d\n", a);
    
    return 0;
}

/*输出
a = 4294967295*/
#include 

typedef int INTEGER;
typedef int *PTRINT;

int main(void){
     
    INTEGER a = 520;
    PTRINT b, c;
    
    b = &a;
    c = b;
    
    printf("addr of a = %p\n", c);
    
    return 0;
}

/*输出
addr of a = 0xbf9fc514*/

相比起宏定义的直接替换,typedef是对类型的封装

//没使用封装
#include 
#include 

struct Date{
     
    int year;
    int month;
    int day;
};
int main(void){
     
    struct Date *date;
    
    date = (struct Date *)malloc(sizeof(struct Date));
    if (date == NULL){
     
        printf("内存分配失败!\n");
        exit(1);
    }
    
    date->year = 2017;
    date->month = 5;
    date->day = 15;
    
    printf("%d-%d-%d\n", date->year,date->month, date->day);
    
    return 0;
}

/*2017-5-15*/
#include 
#include 

typedef struct Date{
     
    int year;
    int month;
    int day;
}DATE;
int main(void){
     
    struct Date *date;
    
    date = (DATE  *)malloc(sizeof(DATE));
    if (date == NULL){
     
        printf("内存分配失败!\n");
        exit(1);
    }
    
    date->year = 2017;
    date->month = 5;
    date->day = 15;
    
    printf("%d-%d-%d\n", date->year,date->month, date->day);
    
    return 0;
}

/*2017-5-15*/

国际上通常在定义结构体的同时,定义多一个指针

#include 
#include 

typedef struct Date{
     
    int year;
    int month;
    int day;
}DATE, *PDATE;
int main(void){
     
    struct Date *date;
    
    date = (PDATE)malloc(sizeof(DATE));
    if (date == NULL){
     
        printf("内存分配失败!\n");
        exit(1);
    }
    
    date->year = 2017;
    date->month = 5;
    date->day = 15;
    
    printf("%d-%d-%d\n", date->year,date->month, date->day);
    
    return 0;
}

/*2017-5-15*/

typedef进阶

#include 

typedef int (*PTR_TO_ARRA)[3];

int main(void){
     
    int array[3] = {
     1, 2, 3};
    PTR_TO-ARREY ptr_to_array = &array;
    
    int i;
    for (i = 0; i < 3; i++){
     
        printf("%d\n", *ptr_to_array);
    }
    return 0;       
}
/*1
2
3*/

int (*fun)(void) ->> typedef int (*PTR_TO_FUN)(void);

第一个括号说明是定义一个指针,后面若是括号,说明是定义一个指向函数的指针,若是方括号则是指向数组的指针

#inlcude 

typedef int (*PTR_TO_FUN)(void);

int fun(void){
     
    return 520;
}

int main(void){
     
    PTR_TO_FUN ptr_to_fun = &fun;
    printf("%d\n", (*ptr_to_fun)());
    retutrn 0;
}

/*520*/

解析int *(*array[3])(int);

(1)首先 (*array[3])就是一个指针数组

(2)int *A(int);由于A左边的星号比右边的括号要低,所以A首先是一个函数,再是一个指针,返回值是一个指向整型变量的指针,参数也是整型变量

(3)若是直接在上面那个语句前面加上typedef,就固定了只能定义数组的元素是三个,为了把其声明变得跟灵活,可这样写typedef int *(*PTR_TO_FUN)(int);这样就可以定义任意元素数量的数组。

#include 
typedef int *(PTR_TO_FUN)(int);

int *FunA(int num){
     
    printf("%d\t", num);
    return &num;
}
int *FunB(int num){
     
    printf("%d\t", num);
    return &num;
}
int *FunC(int num){
     
    printf("%d\t", num);
    return &num;
}

int main(void){
     
    PTR_TO_FUN array[3] = {
     &FunA, &FunB, &FunC};
    int i;
    
    for (i = 0; i < 3; i++){
     
        //第一个括号里array的元素是一个地址(array是一个指针数组),对其解引用,然后得到的地址是函数的地址,后面的括号就是参数
        printf("addr of num: %p\n", (*array[i])(i));
    }
    return 0;
}
/*输出
addr of num: 0xbf872c30
addr of num: 0xbf872c30
addr of num: 0xbf872c30*/
//因为函数用完以后,里面的信息就无用了,被清除,每一次调用函数就是一个压栈和弹栈的过程
//所以输出的地址都是一样的,但是运行的时候会出现警告

解析 void (*funA(int, void (*funB)(int)))(int);

(1)void (*funA(参数))(int);

(2)分析参数内部( int, (*funB)(int) ),第一个参数int确定是整型参数,第二个参数funB左边有一个星号而且被括起来了,说明是一个指针,后面也有一个括号,说明这个指针指向的是一个函数,函数指针,函数的返回值是void,而且这个函数有一个参数为整型。

(3)*funA(参数)也同样是一个函数指针,返回值是void,参数为整型

(4)typedef void (*PTR_TO_FUN)(void);

	`PTR_TO_FUN funA(int, PTR_TO_FUN);`
#include 

typedef int (*PTR_TO_FUN)(int, int);

int add(int, int);
int sub(int, int);
/*未简化
int calc(int (*)(int, int), int, int);
int (*select(char))(int, int);
*/
int calc(PTR_TO_FUN, int, int);
PTR_TO_FUN(select(char));

int add(int numl, int num2){
    return num1 + num2;
}
int sub(int numl, int num2){
    return num1 - num2;
}
int calc(int (*fp)(int, int), int num1, int num2){
    return (*fp)(num1, num2);
}
int (*select(char op)(int, int)){
	switch(op){
        case '+': return add;
        case '-': return sub;
    }
}

int main(){
    int num1, num2;
    char op;
    int (*fp)(int, int);
    
    printf("请输入一个式子(如1+3):");
    scanf("%d%c%d", &num1, &op, &num2);
    
    fp = select(op);
    printf("%d %c %d = %d\n", num1, op, num2, calc(fp, num1, num2));
    
    return 0;
}

共用体

union 共用体名称{
     
    共用体成员1;
    共用体成员2;
    共用体成员3;
    ......
};

共用体的所有成员共用一个内存地址

#include 
#include 

union Test{
     
    int i;
    double pi;
    char str[6];
};
int main(void){
     
    union Test test;
    
    test.i = 520;
    test.pi = 3.14;
    strcpy(test.str, "FishC");
    
    printf("addr of test.i: %p\n", &test.i);
    printf("addr of test.pi: %p\n", &test.pi);
    printf("addr of test.str: %p\n", &test.str);
    
    return 0;
}

定义共用体跟定义结构以的语法相似可以先定义,也可后定义,其次共用体的名字不是必须的。

初始化共用体

union data{
     
    int i;
    char ch;
    float f;
};
union data a = {
     520}; //初始化第一个成员
union data b = a; //直接用一个共用体初始化另一个共用体
union data c = {
     .ch = 'C'}; //C99新增特性,指定初始化成员

枚举类型

如果一个变量只有几种可能的值,那么就可以将其定义为枚举类型。

(1)声明枚举类型:enum 枚举类型名称 {枚举值名称, 枚举值名称...};

(2)定义枚举变量:enum 枚举类型名称 枚举变量1, 枚举变量2;

#include 
#include 

int main(void){
     
    enum Week {
     sun, mon, tue, wed, thu, fri, sat};
    enum Week today;
    struct tm *p;
    time_t t;
    
    time(&t);
    p = localtime(&t);
    
    today = p ->tm_wday;
    
    switch(today){
     
        case mon:
        case tue:
        case wed:
        case thu:
        case fri:
            printf("干活");
            break;
        case sat:
        case sun:
            printf("放假");
            break;
        default:
            printf("error");
    }
    return 0;
}
//常量可以指定一个值,往后元素都是逐渐加一的,但是一旦指定就不可更改
#include 

int main(void){
     
    enum Color {
     red = 10, green, blue};
    enum Color rgb;
    
    for (rgb = red; rgb <= blue; right++){
     
        printf("rgb is %d\n", rgb);
    }
    
    return 0;
}
/*输出
rgb is 10
rgb is 11
rgb is 12*/
#include 

int main(void){
     
    enum Color {
     red, green, blue = 10, yellow};
    enum Color rgb;
    
    printf("red is %d\n", red);
    printf("green is %d\n", green);
    printf("blue is %d\n", blue);
    printf("yellow is %d\n", yellow);
    
    return 0;
}
/*输出
red = 0
green = 1
blue = 10
yellow = 11*/

位域

使用位域的做法是在结构体定义时,在结构体成员后面使用冒号:和数字来表示该成员所占的位数。

#include 

int main(void){
     
    struct Test{
     
        unsigned int a:1;//1表示一个二进制位
        unsigned int b:1;
        unsigned int c:2;
    }
    
    struct Test test;
    test.a = 0;
    test.b = 1;
    test.c = 2;//2在二进制中为10,所以需要两个二进制位
    
    printf("a = %d, b = %d, c = %d\n", test.a, test.b, test.c);
    printf("size of test = %d\n", sizeof(size));

    return 0;
}
/*输出
a = 0, b = 1, c = 2
size of test = 4*/

无名位域

位域成员可以没有名称,只要给出数据类型和位宽即可。unsigned int :12;

逻辑位运算符

(1)按位取反(~)

逻辑位运算符中优先级最高的是按位取反运算符,它的运算符是一个~符号,作用是将1变成0,将0变成1.

(2)按位与(&)

优先级第二高的是按位与运算符,他的运算符是一个&符号(而逻辑与是两个&符号),当两个操作数对应的二进制位同时为真,该位结果为真,反之为0.

	1111 1111		1111 1111		1010 1111
&	1111 1111		0000 0000		1010 0101
--------------	   -----------     -----------
	1111 1111		0000 0000		1010 0101

(3)按位异或(^)

优先级排第三的是按位异或运算符,他的运算符是一个^符号,只有当两个操作数对应的二进制位不同时,他的结果才为1,否则为0.

(4)按位或(|)

逻辑位运算符优先级最低的是按位或运算符,他的运算符是一个|符号(而逻辑或是两个|符号),当两个操作数对应的二进制位有一个为真,该位结果为真,若无为0.

(5)和赋值号结合

这四个运算符,除了按位取反只有一个操作数之外,其他三个都可以跟赋值号(=)结合到一块,使得代码更加整洁!

#include 

int main(void){
     
    int mask = 0xFF;
    int v1 = 0xABCDEF;
    int v2 = 0xABCDEF;
    int v3 = 0xABCDEF;
    
    v1 &= mask;
    v2 |= mask;
    v3 ^= mask;
    
    printf("v1 = 0x%X\n", v1);
    printf("v2 = 0x%X\n", v2);
    printf("v3 = 0x%X\n", v3);
    
    return 0;
}
/*输出
v1 = 0xEF
v2 = 0xABCDFF
V3 = 0xABCD10*/

移位运算符

C语言除了提供四种逻辑位运算符之外,还提供了可以将某个变量中所有的二进制位进行左移或右移的运算符——移位运算符。

(1)左移位运算符

11001010 << 2左边是被移动的数,右边是移动的位数,移完后00101000

(2)右移位运算符

11001010 >> 2左边是被移动的数,右边是移动的位数,移完后00110010

#include 

int main(void){
     
    int value  = 1;
    
    whiel (value < 1024){
     
        value <<= 1; // 等同于value = value << 1;
        printf("value = %d\n", value);
    }
    printf("\n--------分割线----------");
    
    value = 1024;
    while(value > 0){
     
        value >>= 2;
        printf("value = %d\n", value);
    }
    
    return 0;
}
/*输出
value = 2
value = 4
value = 8
value = 16
value = 32
value = 64
value = 128
value = 256
value = 512
value = 1024
----------分割线----------
value = 256
value = 64
value = 16
value = 4
value = 1
value = 0*/

(|)

逻辑位运算符优先级最低的是按位或运算符,他的运算符是一个|符号(而逻辑或是两个|符号),当两个操作数对应的二进制位有一个为真,该位结果为真,若无为0.

(5)和赋值号结合

这四个运算符,除了按位取反只有一个操作数之外,其他三个都可以跟赋值号(=)结合到一块,使得代码更加整洁!

#include 

int main(void){
     
    int mask = 0xFF;
    int v1 = 0xABCDEF;
    int v2 = 0xABCDEF;
    int v3 = 0xABCDEF;
    
    v1 &= mask;
    v2 |= mask;
    v3 ^= mask;
    
    printf("v1 = 0x%X\n", v1);
    printf("v2 = 0x%X\n", v2);
    printf("v3 = 0x%X\n", v3);
    
    return 0;
}
/*输出
v1 = 0xEF
v2 = 0xABCDFF
V3 = 0xABCD10*/

移位运算符

C语言除了提供四种逻辑位运算符之外,还提供了可以将某个变量中所有的二进制位进行左移或右移的运算符——移位运算符。

(1)左移位运算符

11001010 << 2左边是被移动的数,右边是移动的位数,移完后00101000

(2)右移位运算符

11001010 >> 2左边是被移动的数,右边是移动的位数,移完后00110010

#include 

int main(void){
     
    int value  = 1;
    
    whiel (value < 1024){
     
        value <<= 1; // 等同于value = value << 1;
        printf("value = %d\n", value);
    }
    printf("\n--------分割线----------");
    
    value = 1024;
    while(value > 0){
     
        value >>= 2;
        printf("value = %d\n", value);
    }
    
    return 0;
}
/*输出
value = 2
value = 4
value = 8
value = 16
value = 32
value = 64
value = 128
value = 256
value = 512
value = 1024
----------分割线----------
value = 256
value = 64
value = 16
value = 4
value = 1
value = 0*/

你可能感兴趣的:(小甲鱼数据结构学习笔记,指针,链表,单链表)