libubox里面的list(仔细看一下它应该是从内核中移植过来的)可称之为侵入式链表,这种list最突出的特征就是其节点中不含有任何数据,相反,list节点是嵌入到特定的数据结构中的。这样做有两点好处:

  1. 链表自身导出的方法比较简洁,由于不涉及到数据的操作,因而list的所有API都可以实现通用;

  2. 不用为每种数据类型实现一套链表的操作,当然在支持泛型的语言里可以使用模板来实现这种通用list

好了,不说话,直接看代码:

#include <stdlib.h>
#include <stdio.h>

#include "libubox/list.h"


typedef struct _Data {
    char data;
    struct list_head list;
} Data;

void decimal_to_binary()
{
    long number, temp;
    Data mylist, *tmp = NULL;
    struct list_head *pos = NULL, *p = NULL;

    INIT_LIST_HEAD(&mylist.list);
    
    printf("please input a number you want to convert:");
    scanf("%ld", &number);
    printf("Decemal number %ld's binary is:", number);
    
    if(number == 0) {
        printf("%ld\n", number);
        return;
    }

    while(number != 0) {
        tmp = (Data *)malloc(sizeof(Data));
        temp = number % 2;
        tmp->data = temp;
        list_add(&(tmp->list), &(mylist.list));
        number = number / 2;
    }

    list_for_each(pos, &mylist.list) {
        tmp = list_entry(pos, Data, list);
        printf("%ld", (long)tmp->data);
    }
    printf("\n");

    list_for_each_safe(pos, p, &mylist.list) {
        tmp = list_entry(pos, Data, list);
        list_del(pos);
        free(tmp);
    }

    if(list_empty(&mylist.list)) {
        printf("The list now is empty!\n");
    }

    return;
}

void binary_to_decimal()
{
    Data mylist, *tmp = NULL;
    struct list_head *pos = NULL, *p = NULL;
    char ch = '0';
    long dec = 1;
    long dec_number = 0;

    INIT_LIST_HEAD(&mylist.list);

    printf("Please input the binary number you want to convert:");
    ch = getchar();

    while((ch == '0') || (ch == '1')) {
        tmp = (Data *)malloc(sizeof(Data));
        tmp->data = ch;
        list_add(&(tmp->list), &(mylist.list));
        ch = getchar();
    }

    list_for_each(pos, &mylist.list) {
        tmp = list_entry(pos, Data,list);
        dec_number += (int)(tmp->data - '0') * dec;
        dec *= 2;
    }

    printf("Decimal number is %ld\n", dec_number);

    list_for_each_safe(pos, p, &mylist.list) {
        tmp = list_entry(pos, Data, list);
        list_del(pos);
        free(tmp);
    }

    if(list_empty(&mylist.list)){
        printf("The list now is empty!\n");
    }

    return;
}

int main(int argc, char **argv)
{    
    int select = 0;

    printf("**********************************************\n");
    printf("** 1.Decimal to binary **\n");
    printf("** 2.Binary to decimal **\n");
    printf("** 0.Exit **\n");
    printf("**********************************************\n");
    printf("Please select which you want to convert:");
    scanf("%d", &select);
    getchar();

    switch(select) {
    case 0:
        printf("Welcome to use next time!\n");
        break;

    case 1:
        decimal_to_binary();
        break ;

    case 2:
        binary_to_decimal();
        break;

    default:
        printf("Your select is not right!");
        break;
    }

    return 0;
}

txgcwm
764 声望71 粉丝

Linux C/C++


引用和评论

0 条评论