cs61c_lab2
Exercise 0: Makefiles
看makefile文件回答问题。
我的回答如下:
- clean
- all
- gcc
- c99
- 可以使用$(FOO)引用FOO变量
- Darwin是macos的内核
- 第31行从目标文件创建了lfsr程序
Exercise 1: Bit Operations
我的代码如下:
#include <stdio.h>
#include "bit_ops.h"
unsigned get_bit(unsigned x,
unsigned n) {
return (x >> n) & 1;
}
void set_bit(unsigned * x,
unsigned n,
unsigned v) {
*x = *x & ~(1 << n);
*x = *x | (v << n);
}
void flip_bit(unsigned * x,
unsigned n) {
*x = *x ^ (1 << n);
}
Exercise 2: Linear Feedback Shift Register
代码如下:
// #include <stdio.h>
// #include <stdint.h>
// #include <stdlib.h>
// #include <string.h>
// #include "lfsr.h"
// void lfsr_calculate(uint16_t *reg) {
// /* YOUR CODE HERE */
// uint16_t x = ((*reg >> 0) & 1) ^ ((*reg >> 2) & 1) ^ ((*reg >> 3) & 1) ^ ((*reg >> 5) & 1);
// *reg = *reg >> 1;
// *reg = *reg & ~(1 << 15);
// *reg = *reg | (x << 15);
// }
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "lfsr.h"
#include "bit_ops.c"
void lfsr_calculate(uint16_t *reg) {
/* YOUR CODE HERE */
uint16_t x = get_bit(*reg, 0) ^ get_bit(*reg, 2) ^ get_bit(*reg, 3) ^ get_bit(*reg, 5);
*reg = *reg >> 1;
set_bit((unsigned *)reg, 15, x);
}
Exercise 3: Linked Lists
我的代码如下:
#include "list.h"
void append_node (node** head_ptr, int new_data) {
node *new_node = (node *)malloc(sizeof(node));
new_node -> val = new_data;
new_node -> next = NULL;
if (*head_ptr == NULL) {
*head_ptr = new_node;
return;
}
node* curr = *head_ptr;
while (curr -> next != NULL) {
curr = curr->next;
}
curr -> next = new_node;
}
void reverse_list (node** head_ptr) {
node* prev = NULL;
node* curr = *head_ptr;
node* next = NULL;
while (curr != NULL) {
if (prev == NULL) {
next = curr -> next;
curr -> next = NULL;
prev = curr;
curr = next;
} else {
next = curr -> next;
curr -> next = prev;
prev = curr;
curr = next;
}
}
*head_ptr = prev;
}
Exercise 4: Memory Management
我的代码:
/* Include the system headers we need */
#include <stdlib.h>
#include <stdio.h>
/* Include our header */
#include "vector.h"
/* Define what our struct is */
struct vector_t {
size_t size;
int *data;
};
/* Utility function to handle allocation failures. In this
case we print a message and exit. */
static void allocation_failed() {
fprintf(stderr, "Out of memory.\n");
exit(1);
}
vector_t *vector_new() {
vector_t *retval;
retval = (vector_t *)malloc(sizeof(vector_t));
if (retval == NULL) {
allocation_failed();
}
retval->size = 1;
retval->data = (int *)malloc(sizeof(int));
if (retval == NULL) {
free(retval);
allocation_failed();
}
retval->data[0] = 0;
return retval;
}
int vector_get(vector_t *v, size_t loc) {
if(v == NULL) {
fprintf(stderr, "vector_get: passed a NULL vector.\n");
abort();
}
if (loc < v->size) {
return v->data[loc];
} else {
return 0;
}
}
void vector_delete(vector_t *v) {
free(v->data);
free(v);
}
void vector_set(vector_t *v, size_t loc, int value) {
if (loc < v->size) {
v->data[loc] = value;
} else {
int *new_data = (int *)malloc((loc + 1) * sizeof(int));
if (new_data == NULL) {
allocation_failed();
}
for (int i = 0; i <= loc; i ++) new_data[i] = 0;
new_data[loc] = value;
for (int i = 0; i < v->size; i ++) {
new_data[i] = v -> data[i];
}
free(v->data);
v -> data = new_data;
v->size = loc + 1;
}
}