CS61C(su20) lab02

cs61c_lab2

Exercise 0: Makefiles

看makefile文件回答问题。

我的回答如下:

  1. clean
  2. all
  3. gcc
  4. c99
  5. 可以使用$(FOO)引用FOO变量
  6. Darwin是macos的内核
  7. 第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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值