Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct llist *createNode(unsigned char bit);
  6. struct llist *dec2bin(unsigned char n);
  7.  
  8. struct llist
  9.         {
  10.     unsigned char bit;
  11.     struct llist *next;
  12. };
  13.  
  14.  
  15. void addtoList(struct llist** head, unsigned char singleBit){
  16.  
  17.     if(head == NULL){
  18.         *head = createNode(singleBit);
  19.     }else{
  20.         struct llist* newNode = createNode(singleBit);
  21.         struct llist * cur = head;
  22.  
  23.         while(cur->next != NULL){
  24.  
  25.             cur->next = cur;
  26.         }
  27.         cur->next = newNode;
  28.     }
  29. }
  30.  
  31. struct llist *createNode(unsigned char bit){
  32.  
  33.  struct llist* node = malloc(sizeof(struct llist));
  34.  
  35.  if(node == NULL)return -1;
  36.  
  37.  node->bit = bit;
  38.  node->next = NULL;
  39.  
  40. }
  41.  
  42. struct  llist *dec2bin(unsigned char n){
  43.  
  44.     int bitLength = 7;
  45.     int binArray[bitLength];
  46.     int i=0, result=0, rest = 0;
  47.  
  48.     struct llist *h = NULL;
  49.  
  50.     while(i < 8){
  51.  
  52.         rest = n%2;
  53.         result = n/2;
  54.         n = result;
  55.  
  56.         addtoList(h, rest);
  57.  
  58.         binArray[bitLength] = rest;
  59.         i++;
  60.         bitLength--;
  61.  
  62.     }
  63.  
  64.     return h;
  65. }
  66.  
  67.  
  68.  
  69. int main() {
  70.  
  71.  
  72.     dec2bin(25);
  73.  
  74.  
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement