代写CSCI 2132 C语言程序作业、CSCI 2132 C/C++程序代写代做、代写C/C++留学生作业代做R语言编程|调试Matlab程序

Assignment 7CSCI 2132: Software DevelopmentDue April 8, 2019Assignments are due on the due date before 23:59. All assignments must be submitted electronically via the course SVNserver. Plagiarism in assignment answers will not be tolerated. By submitting your answers to this assignment, you declarethat your answers are your original work and that you did not use any sources for its preparation other than the class notes,the textbook, and ones explicitly acknowledged in your answers. Any suspected act of plagiarism will be reported to theFaculty’s Academic Integrity Officer and possibly to the Senate Discipline Committee. The penalty for academic dishonestymay range from failing the course to expulsion from the university, in accordance with Dalhousie University’s regulationsregarding academic integrity.General Instructions: How to Submit Your WorkYou must submit your assignment answers electronically:? Change into your subversion directory on bluenose: cd ~/csci2132/svn/CSID .? Create a directory a7 for the current assignment.? Change into your assignment directory: cd a7 .? Create files inside the a7 directory as instructed in the questions below and put them underSubversion control using svn add . Only add the files you are asked to add!? Once you are done answering all questions in the assignment (or the ones that you are able toanswer—hopefully all), the contents of your a7 directory should look like this:a7permutations.clinked_list.clinked_list.hnode_pool.cnode_pool.hstringset.c(You will also have executable programs and potentially some data files in this directory, but youshould not add them to SVN.) Submit your work using svn commit -mSubmit Assignment 7 .1(Q1) RecursionConsider the sequence of the first n positive integers, h1,2, . . . , ni. A permutation of this sequence isany sequence hx1, x2, . . . , xni that contains each of the numbers 1, 2, . . . , n exactly once. So, h3, 4, 2, 1iis a permutation of h1, 2, 3, 4i but h1, 3, 2, 3i and h1, 5, 3, 2i are not.A permutation hx1, x2, . . . , xni is lexicographically less than another permutation h y1, y2, . . . , yni if andonly if there exists an index 1 � k � n such that? xi = yi for all 1 � i ? xk So, h2, 1, 3, 4i is lexicographically less than h2, 3, 1, 4i because x1 = y1 = 2 and x2 = 1 elements in the sequence were letters rather than integers, you could interpret the sequence of lettersas a word and the ordering defined here would simply be the one in which the words would occur in adictionary.Your task is to write a C program permutations that takes a command line argument n and outputsall permutations of the sequence h1,2, . . . , ni in lexicographic order. The output should consist ofn! lines, each containing one permutation. The numbers in each permutation must be printed inorder, separated by exactly one space between each pair of consecutive numbers. Thus, when runningpermutations 3, you should see the following output on your screen:$ ./permutations 31 2 31 3 22 1 32 3 13 1 23 2 1permutations 4 should produce$ ./permutations 41 2 3 41 2 4 31 3 2 41 3 4 21 4 2 31 4 3 22 1 3 42 1 4 32 3 1 42 3 4 12 4 1 32 4 3 13 1 2 43 1 4 223 2 1 43 2 4 13 4 1 23 4 2 14 1 2 34 1 3 24 2 1 34 2 3 14 3 1 24 3 2 1As the title of the question suggests, recursion is a natural strategy to implement this program. Youshould not have to generate the permutations and then sort them. Your code should be able to generatethe permutations in the desired order.The basic strategy is fairly simple: Your top-level invocation of the function that generates the permutationshould iterate over all possible choices of the first element in the permutation, from smallestto largest. For each such choice, you make a recursive call that generates all permutations of theremaining n ?? 1 elements. How do you do this? You iterate over all possible choices for the secondelement, from smallest to largest and, for each such choice, make a recursive call that generates allpermutations of the remaining n??2 elements, and so on. The deepest recursive call is asked to generatea permutation of 0 elements, that is, you have already chosen all n elements in the permutation. Thisis the point at which you print the current permutation.One issue you will have to deal with is how to keep track of the elements that can still be chosen asthe second, third, fourth, . . . element in the permutationu after choosing the first one, two, three, . . .elements in the permutation. One easy way to do this is to initialize an array of size n that containsall the numbers from 1 to n. Whenever you choose an element from this array, you set its entry to 0and, in subsequent recursive calls, you skip elements that are 0 because they were already chosen.Don’t forget to restore each number x in this array of candidates when it is no longer part of the set ofelements currently chosen as part of the permutation. This is not the most efficient way to do this, butefficiency is not your main concern in this question.Implement your program in a single source file permutations.c. Do not submit the executable fileobtained by compiling it. However, you should obviously compile and test your code.3(Q2) Structs, unions, and dynamic memory managementThe ability to allocate and deallocate memory on the heap at arbitrary times is key to writing flexibleprograms, but every allocation or deallocation has an associated cost because the runtime system oroperating system needs to perform a certain amount of bookkeeping to keep track of which memorylocations on the heap are currently occupied and which ones are available for future allocation requests.As a result, in programs that create and destroy many small objects on the heap, the cost of heapmanagement may become the main performance bottleneck. This is the case in object-oriented programsthat manipulate complex collections of small objects and in the implementation of pointer-based datastructures such as linked lists and binary search trees, where a na?ve implementation represents eachlist or tree node as a separately allocated block on the heap.A common technique to improve the performance of such programs substantially is to allocate largerchunks of memory at a time, large enough to hold hundreds or thousands of the manipulated objects(e.g., a chunk that is big enough to hold 1000 list nodes in a linked list implementation). Your codethen has to manually track which of the slots in this large memory chunk are occupied or not, but thisis a much simpler problem that can be solved much more efficiently than general heap management.The data structure that manages the allocation of individual list nodes from this array of list nodes isoften referred to as a “node pool”.Here, your task is to implement a linked list that uses a node pool to efficiently manage the memoryoccupied by its nodes. As an application, you will use the same test program as in Lab 8, which needsa data structure to store a collection of strings. In Lab 8, you implement this collection as a splay treein order to guarantee fast searches. Here, you do not worry about the speed of searching the datastructure, only about the cost of heap management, so a linked list suffices as the data structure tostore the collection of strings.1Develop your code for this question in four steps:1. Implement a basic linked list data structure. (You can use the discussion of linked lists from classas a guide.)2. Implement the test application. (You can cut and paste most of this code from Step 7 in Lab 8.)3. Implement the node pool.4. Modify your list implementation from Step 1 to use the node pool from Step 3 to manage theallocation of list nodes.Step 1: Implement a (singly) linked listA (singly) linked list is a sequence of nodes linked together by successor pointers. The first node ofthe list is its head. The last node is its tail. Every node that is not the tail has exactly one successor.Every node that is not the head is the successor of exactly one node. Every node stores some data item,which we will call the value of the node here. This is illustrated in Figure 1.1If you enjoy implementing data structures and are keen to practice your C programming skills, I encourage you toattempt the following project in your spare time: The splay tree implementation in Lab 8 allocates each node as its ownblock of memory on the heap. The linked list implementation you develop in this assignment avoids this but does notsupport fast searches. Try to implement a splay tree (or other balanced binary search tree if you want) and use the strategyfrom this assignment to manage the space occupied by its nodes efficiently.4Head 5 3 11 1 7 NULLFigure 1: A linked list storing a sequence of integers.Develop a linked list implementation that supports the following operations:list_t make_list();This creates a new linked list and returns a reference to it. Similarly to Lab 8, the list should berepresented as a struct _list_t and list_t should be a pointer to a heap-allocated struct _list_t,that is, the list_t type should be defined using typedef struct _list_t *list_t;void destroy_list(list_t);This destroys the given linked list, that is, it frees all the memory the list occupies on the heap. Thisrequires the deallocation of the list structure itself as well as of all node structures that are part of thelist.node_t list_add(list_t, void *);This adds a data item referenced by a void * to the beginning of the list. The list should simply storethe pointer to the data item in the new list node. The data should not be copied into the list. Thisis similar to the approach taken in Lab 8. See the discussion there to understand why this is useful.The return value is a pointer to the new list node, that is, the node_t type should be defined usingtypedef struct _node_t *node_t;void *list_delete(list_t, node_t);This deletes the given node from the given list and returns the data that was stored at that node. (Sincethe node only stores a void * to the data and the data is always kept externally from the list, yousimply return the void * that was stored at the list node.) Again, this is similar to the binary treeimplementation in Lab 8.void *node_value(node_t);This returns a pointer to the data element stored at the given node. (The node itself stores a void * tothis data item. You should simply return this pointer.)node_t list_head(list_t);This returns (a pointer to) the first node of the list. (Recall that node_t is a pointer to a list node.)node_t node_succ(node_t);5This returns the successor of the given node in the list, that is, the next node in the list after this node.If the given node is the last node of the list, this function returns NULL.In this first implementation of your linked list data structure, it is okay to allocate the list and the nodesof the list as individual chunks of memory, that is, using one malloc call per list node.For this step, create a header file linked_list.h that declares the above types and function prototypesand an implementation file linked_list.c that defines the above types and functions. Review Steps1–6 of Lab 8 if you do not remember how to do this.Step 2: Use the linked list to store a collection of stringsFor this step, you can use the code from Step 7 in Lab 8, but you have to modify it slightly because thelinked list structure you have developed in Step 1 does not support a search operation as the binarysearch tree developed in Lab 8 does.Your goal is to implement a program that starts out with an empty collection of strings and repeatedlyprompts you for input by printing “? ”. Valid inputs are:? a to add a string to the collection.? d to delete a string from the collection. This operation deletes the first string itfinds that starts with the given pattern. For example, d Nor may delete the string Norbertfrom the collection if it is currently in the collection. If there is no string in the collection thatstarts with the given pattern, this operation does not modify the current collection and printsSTRING NOT FOUND.? f to find an arbitrary string in the collection that starts with the given pattern andprint it on the screen. Similar to the delete operation, f Nor may report the string Norbert ifthis string is in the collection. Also similar to the delete operation, this find operation printsSTRING NOT FOUND if there is no string in the collection that starts with the given prefix.? l to list all strings currently in the collection. (In contrast to Lab 8, the strings do not need to belisted in sorted order. Thus, you can simply list them in the order they are stored in the list.)? q to terminate the program.Implement the required code (which you are free to copy from Lab 8 and modify appropriately) in a filestringset.c. If you copy this code from Lab 8, you need to make sure that you include linked_list.hinstead of tree.h and that you replace the use of tree_find in the code from Lab 8 with appropriatecustom code that uses list_head, node_succ, and node_value to search for a string with the givenprefix as part of the delete and find operations. Additional small changes are required to replace othercalls to tree functions with their appropriate counterparts in the linked list implementation.Building an executable program stringset requires you to compile both the stringset.c andlinked_list.c files and link the two resulting object files. Review Step 8 of Lab 8 if you do notremember how to do this. This executable file is not part of your assignment submission, but youshould obviously build and run your code in order to check your code for syntax errors and test yourcode.6Step 3: Implement a node poolThis is the main creative part of this question. You will implement a node pool that allows you tomanage the heap memory occupied by list nodes more efficiently than by using one call to malloc perlist node.The data structure you need to implement is implemented as a struct _node_pool_t. You will againdefine an alias node_pool_t for pointers to this struct:typedef struct _node_pool_t *node_pool_t;The structure supports exactly four operations:node_pool_t make_node_pool();This creates a new node pool data structure.void destroy_node_pool(node_pool_t);This destroys the given node pool, that is, it frees all memory it occupies on the heap.node_t request_node(node_pool_t);This requests a new linked list node from the node pool. In Step 4, you will modify your linked listimplementation to replace calls to malloc to create new list nodes with calls to request_node.void release_node(node_pool_t, node_t);This releases a previously requested linked list node back to the pool. The node pool can reuse thememory occupied by this node to satisfy future node requests made by calling request_node. In Step 4,you will modify your linked list implementation to replace calls to free to release list nodes with callsto release_node.Let us discuss a strategy for developing an efficient node pool structure that does not call malloc forevery single list node that is created and instead requests space for list nodes in larger chunks.Let us assume we will never create more than 1023 list nodes. (As a bonus part of this step, you mayremove this assumption.) Then we can get away with a single call to malloc by allocating a blockof memory of size 1023 * sizeof(struct _node_t), large enough to hold 1023 list nodes. This isessentially an array of 1023 list node structs. The node pool then returns pointers to available slots inthis array in response to request_node calls.But how does the node pool keep track of which array slots are available? It keeps a linked list ofavailable array slots! The slots are used themselves to store the pointers necessary to form this linkedlist. Specifically, you should implement a slot as:7union slot_t {struct _node_t node;slot_t *next;};This allows you to use the memory of the slot to store either a list node (when it is reserved for thispurpose by returning it to the user in response to a request_node call) or a pointer to the next availablearray slot if this slot is currently unoccupied, that is, it is part of the linked list of available array slots.The _node_pool_t implementation could then look like this:struct _node_pool_t {union slot_t slots[1023];slot_t *free_slots;};The make_node_pool function has to allocate memory for this structure (using malloc) and initializethe list of available slots by setting up pointers between them. The destroy_node_pool function simplyfrees the memory allocated to the node pool. The request_node function returns the first available slotin the list of slots starting with free_slots and updates free_slots to point to the next available slotin this list. You should return NULL if the list of available slots is empty. The release_node functionmakes the released node part of the list of free slots again by casting it to a slot_t *, setting its nextpointer to point to the current first slot in the list of available slots, and updating free_slots to pointto the returned slot.For this step, create a header file node_pool.h that declares the above types and function prototypesand an implementation file node_pool.c that defines the above types and functions.Bonus: For 25% bonus marks, you should support allocating an arbitrary number of list nodes, notjust a fixed number. The strategy is as follows: Instead of allocating an array capable of holding 1023list nodes, you start out with an empty node pool that does not have any space reserved for list nodes.The free_slots pointer should be NULL to indicate that currently no slots are available. Then, therequest_node function should do one of two things:? If free_slots != NULL, then it should reserve the next available slot as in the above implementation.? If free_slots == NULL, which can happen at any time when all available slots are occupied, itshould allocate a new array of 1023 slots, form a new list of available slots using these slots, andthen return the first slot in this list.This creates a small problem: When you destroy the pool, you need to free the memory occupied by all1023-slot blocks you allocated. The strategy you should use to deal with this is to replace the slotsarray with a linked list of structs, each holding one of the 1023-slot blocks. You can then free thememory allocated by these blocks by traversing this linked list.(This implementation has another problem: since you never free node blocks until the entire nodepool is destroyed, even if none of the slots in a given block is occupied, the total space used by thenode pool is the maximum number of nodes in the list over the lifetime of the list, rounded to the next8multiple of 1024. Thus, you may end up in a situation where a single peak usage that requires millionsof nodes makes your list occupy a lot of space even though the list stores only a few hundred nodesmost of the time. There are ways to mitigate this while keeping the cost per list operation constant,but this is well beyond the scope of this course.)Step 4: Use the node pool in your linked list implementationModify your linked list implementation from Step 1 to use the node pool from Step 3 to manage theheap memory occupied by list nodes. This requires the following simple changes:? You need to modify your _list_t structure so it also stores a reference of type node_pool_t to anode pool to be used by this list.? Your make_list function should call make_node_pool to create a new node pool to be used bythis list.? Your destroy_list function also needs to destroy the node pool used by the given list by callingdestroy_node_pool. If you think about it, you no longer need to release individual nodes as aresult when destroying the list.? All other operations that create or destroy list nodes should use request_node and release_nodeinstead of malloc and free to create and destroy list nodes.本团队核心人员组成主要包括BAT一线工程师,精通德英语!我们主要业务范围是代做编程大作业、课程设计等等。我们的方向领域:window编程 数值算法 AI人工智能 金融统计 计量分析 大数据 网络编程 WEB编程 通讯编程 游戏编程多媒体linux 外挂编程 程序API图像处理 嵌入式/单片机 数据库编程 控制台 进程与线程 网络安全 汇编语言 硬件编程 软件设计 工程标准规等。其中代写编程、代写程序、代写留学生程序作业语言或工具包括但不限于以下范围:C/C++/C#代写Java代写IT代写Python代写辅导编程作业Matlab代写Haskell代写Processing代写Linux环境搭建Rust代写Data Structure Assginment 数据结构代写MIPS代写Machine Learning 作业 代写Oracle/SQL/PostgreSQL/Pig 数据库代写/代做/辅导Web开发、网站开发、网站作业ASP.NET网站开发Finance Insurace Statistics统计、回归、迭代Prolog代写Computer Computational method代做因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:[email protected] 微信:codehelp

你可能感兴趣的:(代写CSCI 2132 C语言程序作业、CSCI 2132 C/C++程序代写代做、代写C/C++留学生作业代做R语言编程|调试Matlab程序)