Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct llist *createNode(unsigned char bit);
- struct llist *dec2bin(unsigned char n);
- struct llist
- {
- unsigned char bit;
- struct llist *next;
- };
- void addtoList(struct llist** head, unsigned char singleBit){
- if(head == NULL){
- *head = createNode(singleBit);
- }else{
- struct llist* newNode = createNode(singleBit);
- struct llist * cur = head;
- while(cur->next != NULL){
- cur->next = cur;
- }
- cur->next = newNode;
- }
- }
- struct llist *createNode(unsigned char bit){
- struct llist* node = malloc(sizeof(struct llist));
- if(node == NULL)return -1;
- node->bit = bit;
- node->next = NULL;
- }
- struct llist *dec2bin(unsigned char n){
- int bitLength = 7;
- int binArray[bitLength];
- int i=0, result=0, rest = 0;
- struct llist *h = NULL;
- while(i < 8){
- rest = n%2;
- result = n/2;
- n = result;
- addtoList(h, rest);
- binArray[bitLength] = rest;
- i++;
- bitLength--;
- }
- return h;
- }
- int main() {
- dec2bin(25);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement