libubox
里面的list
(仔细看一下它应该是从内核中移植过来的)可称之为侵入式链表,这种list
最突出的特征就是其节点中不含有任何数据,相反,list
节点是嵌入到特定的数据结构中的。这样做有两点好处:
链表自身导出的方法比较简洁,由于不涉及到数据的操作,因而
list
的所有API
都可以实现通用;不用为每种数据类型实现一套链表的操作,当然在支持泛型的语言里可以使用模板来实现这种通用
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;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。