// Return the nth bit of x. // Assume 0 <= n <= 31 unsignedget_bit(unsigned x, unsigned n) { // YOUR CODE HERE // Returning -1 is a placeholder (it makes // no sense, because get_bit only returns // 0 or 1) return (x >> n) & 0x1; } // Set the nth bit of the value of x to v. // Assume 0 <= n <= 31, and v is 0 or 1 voidset_bit(unsigned * x, unsigned n, unsigned v) { // YOUR CODE HERE int flag = v-1; (*x) = (((*x) | (v << n)) & (~flag)) | ((*x) & (~((v+1) << n)) & flag); } // Flip the nth bit of the value of x. // Assume 0 <= n <= 31 voidflip_bit(unsigned * x, unsigned n) { // YOUR CODE HERE int v = ((*x) >> n) & 0x1; v ^= 1; int flag = v-1; (*x) = (((*x) | (v << n)) & (~flag)) | ((*x) & (~((v+1) << n)) & flag); }
/* Add a node to the end of the linked list. Assume head_ptr is non-null. */ voidappend_node(node** head_ptr, int new_data) { /* First lets allocate memory for the new node and initialize its attributes */ /* YOUR CODE HERE */ node* new_node = (node*)malloc(sizeof(node)); new_node->val = new_data;
/* If the list is empty, set the new node to be the head and return */ if (*head_ptr == NULL) { /* YOUR CODE HERE */ *head_ptr = new_node; return; } node* curr = *head_ptr; while (curr->next != NULL) { curr = curr->next; } /* Insert node at the end of the list */ /* YOUR CODE HERE */ curr->next = new_node; new_node->next = NULL; }
/* Reverse a linked list in place (in other words, without creating a new list). Assume that head_ptr is non-null. */ voidreverse_list(node** head_ptr) { node* prev = NULL; node* curr = *head_ptr; node* next = NULL; while (curr != NULL) { /* INSERT CODE HERE */ next = curr->next; curr->next = prev; prev = curr; curr = next; } /* Set the new head to be what originally was the last node in the list */ *head_ptr = prev; }
/* Create a new vector with a size (length) of 1 and set its single component to zero... the RIGHT WAY */ vector_t *vector_new() { /* Declare what this function will return */ vector_t *retval;
/* First, we need to allocate memory on the heap for the struct */ retval = (vector_t*)malloc(sizeof(vector_t));/* YOUR CODE HERE */
/* Check our return value to make sure we got memory */ if (retval == NULL/* YOUR CODE HERE */) { allocation_failed(); }
/* Now we need to initialize our data. Since retval->data should be able to dynamically grow, what do you need to do? */ retval->size = 1;/* YOUR CODE HERE */; retval->data = (int*)malloc(sizeof(int)*retval->size);/* YOUR CODE HERE */;
/* Check the data attribute of our vector to make sure we got memory */ if (retval->data == NULL/* YOUR CODE HERE */) { free(retval); //Why is this line necessary? Because it allocate the memory of vector but not allocate the data that will cause memory leak. allocation_failed(); }
/* Complete the initialization by setting the single component to zero */ /* YOUR CODE HERE */ *(retval->data) = 0;
/* and return... */ return retval; }
/* Return the value at the specified location/component "loc" of the vector */ intvector_get(vector_t *v, size_t loc) {
/* If we are passed a NULL pointer for our vector, complain about it and exit. */ if(v == NULL) { fprintf(stderr, "vector_get: passed a NULL vector.\n"); abort(); }
/* If the requested location is higher than we have allocated, return 0. * Otherwise, return what is in the passed location. */ if (loc < v->size/* YOUR CODE HERE */) { return v->data[loc];/* YOUR CODE HERE */ } else { return0; } }
/* Free up the memory allocated for the passed vector. Remember, you need to free up ALL the memory that was allocated. */ voidvector_delete(vector_t *v) { /* YOUR SOLUTION HERE */ free(v); }
/* Set a value in the vector. If the extra memory allocation fails, call allocation_failed(). */ voidvector_set(vector_t *v, size_t loc, int value) { /* What do you need to do if the location is greater than the size we have * allocated? Remember that unset locations should contain a value of 0. */
/* YOUR SOLUTION HERE */ if (loc >= v->size) { v->data = (int*)realloc(v->data, sizeof(int)*(loc+1)); if (v->data == NULL) { allocation_failed(); } for (size_t i = v->size; i < loc+1; ++i) v->data[i] = 0; v->size = loc+1; } v->data[loc] = value; }