5. node_prev = node_curr;
node_curr = node_curr->next;
free(node_prev);
}
new_data.data = node_curr;
return new_data;
}
Data right_shift(Data src, int n) {
Data new_data;
#ToDo
return new_data;
}
-------------------------------------------------------
data.h
#ifndef __DATA
#define __DATA
typedef struct DataNode DataNode;
typedef struct Data Data;
// Represents a node in the linked list.
struct DataNode {
unsigned char number; // '0' ~ '9' or 'A' ~ 'F'
DataNode *next;
};
// Represents data in base 2 - 16.
struct Data {
unsigned char base; // indicate what base this data is
unsigned char sign; // indicate whether this data is signed or unsigned
// number. 0 means unsigned and 1 means signed.
6. unsigned char number_bits; // the number of bits can be used to represent
// this number in binary.
// The maximal number of bits is 32.
unsigned char len; // length of linkedList
DataNode *data; // represent data in LinkedList
};
int convertCharToNumber(char ch); // return decimal number of ch in base 2 ~ 16
// return -1 if ch is not a valid number
char convertNumberToChar(
int n); // return char representation of n in base 2 ~ 16
// return '0' if n is not a number of 0 ~ 15
Data convert_to_base_n(
Data src,
unsigned char n); // Return a new Data that represents src in base n
int convert_to_int(Data src); // convert data to an int
Data left_shift(Data src,
int shift); // Return a new Data in base 2 that represents an
// application of binary operator left shift on src
// where shift is less than src.number_bits
Data right_shift(Data src,
int shift); // Return a new Data in base 2 that represents an
// application of binary operator right shift on
// src where shift is less than src.number_bits
#endif
------------------------------------------------------------------
main.c
#include
#include
#include
int main() {
7. DataNode b0 = {'5', NULL};
DataNode b1 = {'0', &b0};
DataNode b2 = {'2', &b1};
Data d205;
d205.base = 10;
d205.sign = 0;
d205.len = 3;
d205.number_bits = 8;
d205.data = &b2;
Data b11001101 = convert_to_base_n(d205, 2);
printf("The base should be 2, and your base is %dn", b11001101.base);
printf("The sign should not be changed, and your sign is %dn",
b11001101.sign);
printf("The number_bits should not be changed, and your number_bits is %dn",
b11001101.number_bits);
printf("The length should be 8, and your len is %dn", b11001101.len);
printf("The data should be 11001101, and your data is ");
for (DataNode *node = b11001101.data; node; node = node->next) {
printf("%c", node->number);
}
printf("nn");
Data hCD = convert_to_base_n(d205, 16);
printf("The base should be 16, and your base is %dn", hCD.base);
printf("The sign should not be changed, and your sign is %dn", hCD.sign);
printf("The number_bits should not be changed, and your number_bits is %dn",
hCD.number_bits);
printf("The length should be 2, and your len is %dn", hCD.len);
printf("The data should be CD, and your data is ");
for (DataNode *node = hCD.data; node; node = node->next) {
printf("%c", node->number);
8. }
printf("nn");
Data b00110100 = left_shift(d205, 2);
printf("The base should be 2, and your base is %dn", b00110100.base);
printf("The sign should not be changed, and your sign is %dn",
b00110100.sign);
printf("The number_bits should not be changed, and your number_bits is %dn",
b00110100.number_bits);
printf(
"The length should be 6 since it cannot exceed the number_bit and"
" the first two 0 should not be included in the list.Your len is %dn",
b00110100.len);
printf("The data should be 110100, and your data is ");
for (DataNode *node = b00110100.data; node; node = node->next) {
printf("%c", node->number);
}
printf("n");
printf("We expect 205, and your function returns %dn",
convert_to_int(b11001101));
b11001101.sign = 1;
printf("We expect -51, and your function returns %dn",
convert_to_int(b11001101));
b11001101.number_bits = 16;
printf("We expect 205, and your function returns %dn",
convert_to_int(b11001101));
return 0;
}