BEST代写-线上留学生作业代写 & 论文代写专家

成立于2015年的老牌留学生代写品牌-BEST代写提供超过百门冷热门留学学科的作业和论文代写服务。全网BEST原创,高质,准时的留学生代写。

C语言代写 | CSCI-UA.0201 Programming Assignment 1

C语言代写 | CSCI-UA.0201 Programming Assignment 1

这个作业是用C语言构建一个哈希表和一个二进制搜索树

CSCI-UA.0201
Programming Assignment 1

This programming assignment is to be done entirely in C. It will give you practice using pointers
and structs in C, in this case to build a hash table and a binary search tree.
As described below, you will be submitting three files, hashing.c, tree.c, and tree.h. To do so,
just upload the three files to the “Programming Assignment 1” page under the Assignments tab.
Important: Do not submit the files until the program is fully working. Although there is a modest
late penalty, you will lose far more points if you submit a program that doesn’t work.
This assignment looks long (5 pages), but that is only because I have given very detailed
instructions. That being said, please start working on the assignment immediately! If you have
general questions about this assignment, please post them on the class forum and I will answer
quickly. If you have questions about your code, please send them to the course assistant or coinstructor you were assigned to. Do not post your code to the forum.
You should complete the assignment by performing the following steps.
Step 1
In a file hashing.c, do the following:
• Declare a struct type HASHCELL, which is used to contain data in a hash table. A
HASHCELL should contain two fields: a word field that is a string – it should be of type
char *, so it’s a pointer, not an allocated array – and a next field that is a pointer to a
HASHCELL, so that HASHCELLs can be linked together in a list.
• Declare a global variable hashtable, representing a hash table, which is an array of
HASHCELL pointers. You should use #define to define a constant SIZE to be 100 and
declare hashtable to have SIZE elements.
• Define a function hash_string which takes a string (a char *) as a parameter and
returns an unsigned integer between 0 and SIZE -1, by hashing the string. You are free
to choose your own hash algorithm, but here is a simple one you can use:
o Define an unsigned integer variable hash and initialize it to 1.
o In a loop that iterates over each character in the string, set hash equal to
(hash * 7) + c in each iteration of the loop, where c is the current character.
Since a char is just an 8-bit number, so it’s fine to do arithmetic on it.
o Return the value of hash mod SIZE (where % is the mod operator).
Remember that you can recognize the end of a string by the terminating 0.
The quality of the hash (i.e., how evenly it distributes hash values between 0 and
SIZE – 1) isn’t so important here.
• Define a function insert_hash_cell that takes a string (again, a char *) as a
parameter and inserts the string into the hash table as follows:
o It calls hash_string on the string, assigning the result of the hash to an
unsigned integer variable index.
o It creates a HASHCELL (using malloc), such that the word field of the cell points
to the string. IMPORTANT: You will need to make a copy of the string by
performing the following steps:
§ Using malloc again, allocate a block of memory large enough to hold the
string (including the 0 at the end). I suggest using the built-in strlen
function, described at the bottom of this assignment, which gives you the
length of a string without the 0 at the end (so you’ll need to add 1).
§ Set the word field of the cell to point to the new block of memory.
§ Copy the characters of the string into the new block. I suggest using the
strcpy function, described at the bottom of this assignment.
o It inserts the new cell into the linked list of cells pointed to by
hashtable[index]. However, if the word in the new cell already exists in that
linked list, do not insert the new cell. This prevents duplicate words from being
inserted into the hash table. I suggest using the built-in strcmp function,
described below, to compare two strings to see if they are the same.
• Define a function print_hash_table() that prints out the elements of the hash table.
Specifically, in a loop, for each i from 0 to SIZE-1, the function should print out i, then
“:”, then (on the same line) all the words in the linked list at hashtable[i], and then a
carriage return. This way, the list of words at each element of the hash table is printed on
their own line.
Step 2
Still within hashing.c, define a main function that does the following:
• It declares a variable str that is an array of 100 chars (which should be large enough to
hold any string that is read in from the terminal).
• In a loop, it uses scanf to repeatedly read a string from the terminal into str, and then
calls insert_hash_cell on str to insert the string into the hash table (which is why
insert_hash_cell has to make a copy of the string). The loop should stop when
there are no more strings to read, which scanf will indicate by returning the value EOF.
That is, your loop could look like,
while (scanf(…) != EOF) {…}”.
• It calls print_hash_table()to print the contents of the hashtable.

bestdaixie

评论已关闭。