OpenWrt:libubox之list

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值