System 1-02: Review of Linked List & Bitwise Operation
Example:
#include <stdio.h> #include <stdlib.h> typedef struct node { int num; struct node *next; }Node; typedef struct my_list { Node *head; int count; }List; void print_list(List *list){ Node *current = list->head; while(current != NULL){ printf("%d\n",current->num); current = current->next; } } void insert_at_front(List *list, int num){ Node* new_node = malloc(sizeof(Node)); new_node->num = num; new_node->next = list->head; list->head = new_node; list->count++; }
/* index starting from 0 (rightmost) to 31 (leftmost) */ int get_bit(int value, int index){ int retVal = (value>>index)&1; return retVal; } int clear_bit(int value, int index){ int mask = 1 << index; int retVal = value & ~mask; return retVal; } int main() { List *my_list = malloc(sizeof(List)); my_list->head = NULL; my_list->count = 0; print_list(my_list); insert_at_front(my_list, 1); insert_at_front(my_list, 2); insert_at_front(my_list, 3); insert_at_front(my_list, 4); print_list(my_list); return 0; }