221 CO2003 Assignment-English
221 CO2003 Assignment-English
221 CO2003 Assignment-English
ASSIGNMENT 1
ASSIGNMENT’S SPECIFICATION
Version 1.1
1 Assignment’s outcome
After completing this assignment, students review and make good use of:
2 Introduction
Strings are often used to represent a piece of text (e.g. sentence, paragraph), thereby displaying
meaningful information to the user. Typically, string literals are implemented using an array
list to store contiguous characters. However, if strings are stored that way, the operation
concatenate/join two strings of length m and n respectively has a complexity of O(m + n).
On the other hand, a linked list is a data structure that can perform the join operation of
2 lists with less complexity.
In this assignment, students are asked to implement a string class that efficiently supports
string concatenation using list data structures called ConcatStringList.
3 Description
3.1 Overview
On the other hand, string concatenation is effectively performed by concatenating the last
CharALNode of the previous ConcatStringList with the first CharALNode of the following
ConcatStringList. For example, in Figure 1, when concatenating the string s1 with s2, we
Data Structures and Algorithms’ assignment - Semester 1 (2021 - 2022) Page 1/12
HCMC UNIVERSITY OF TECHNOLOGY - VNU-HCM
FACULTY OF COMPUTER SCIENCE AND ENGINEERING
simply point the association of CharALNode ”is is” to CharALNode ” an”. The result of the
join is shown in Figure 2.
The following sections describe in detail the classes that need to be implemented.
1. ConcatStringList(const char * s)
• Initialize a ConcatStringList object with 1 CharALNode. In CharALNode, there is
a CharArrayList initialized with the string literal s.
• Complexity (all cases): O(n) where n is the length of the string s.
2. int length() const
• Returns the length of the string being stored in the ConcatStringList object.
Data Structures and Algorithms’ assignment - Semester 1 (2021 - 2022) Page 2/12
HCMC UNIVERSITY OF TECHNOLOGY - VNU-HCM
FACULTY OF COMPUTER SCIENCE AND ENGINEERING
Example 3.1
In Figure 1:
Example 3.2
In Figure 1:
Example 3.3
In Figure 1:
– s1.indexOf(’i’) returns 9.
– s2.indexOf(’b’) returns -1.
Data Structures and Algorithms’ assignment - Semester 1 (2021 - 2022) Page 3/12
HCMC UNIVERSITY OF TECHNOLOGY - VNU-HCM
FACULTY OF COMPUTER SCIENCE AND ENGINEERING
Example 3.4
In Figure 1:
Data Structures and Algorithms’ assignment - Semester 1 (2021 - 2022) Page 4/12
HCMC UNIVERSITY OF TECHNOLOGY - VNU-HCM
FACULTY OF COMPUTER SCIENCE AND ENGINEERING
Data Structures and Algorithms’ assignment - Semester 1 (2021 - 2022) Page 5/12
HCMC UNIVERSITY OF TECHNOLOGY - VNU-HCM
FACULTY OF COMPUTER SCIENCE AND ENGINEERING
• Returns a new ConcatStringList object representing the original string after being
reversed.
• Example: Figure 6 illustrates the reverse operation.
9. ∼ConcatStringList()
• Implement the destructor so that all dynamically allocated memory must be free
after the program terminates. Usually, a destructor will free the CharALNodes
between head and tail. However, this is not appropriate because some CharALNode
may still be referenced as a result of a string join operation. Refer to Section 3.3 for
appropriate destructor implementation.
Let us take a look back at Figure 4. The figure illustrates the result of the concatenating
operation. Suppose we want to delete the string s2, if the CharALNode ” an” is deleted, the
string s4 will only have 3 CharALNode and no longer properly represent the result of the string
concatenation. To solve this problem, we will maintain a list of reference counts to the first
and last CharALNodes, represented by the ReferencesList class. For example, in Figure 4,
the CharALNode of ”is is” has a reference count of 1 (from the tail of s1), the CharALNode
of ” an” has a reference count of 3. At the same time, we maintain a list of deleted strings,
represented by the DeleteStringList class. Each node of the DeleteStringList holds 2 pieces of
information: the first and the last CharALNode of a deleted string. We will traverse through
Data Structures and Algorithms’ assignment - Semester 1 (2021 - 2022) Page 6/12
HCMC UNIVERSITY OF TECHNOLOGY - VNU-HCM
FACULTY OF COMPUTER SCIENCE AND ENGINEERING
this list, check if both head and tail of a string have zero total references and then delete the
CharALNodes between that head and tail (including head and tail). Here are some requirements
when implementing the ReferencesList class and the DeleteStringList class:
• Every time a new string is created, the first and last CharALNode of this string are
added to the ReferencesList for tracking. Of the nine required implementations for the
ConcatStringList class, the following methods create a new object:
– ConcatStringList(const char *)
– ConcatStringList concat(const ConcatStringList & otherS) const
– ConcatStringList subString(int from, int to) const
– ConcatStringList reverse() const
• Every time we delete a string, we will reduce the number of references in the ReferencesList
corresponding to the CharALNode head and tail of the string by 1 unit. Also, add a new
node with this head and tail information at the end of the DeleteStringList. Next, we
traverse through the nodes in the DeleteStringList, if any node has a total number of
references to head and tail equal to 0, then we delete the CharALNodes between head
and tail, and then remove the node from the DeleteStringList. If every node in the
ReferencesList has 0 reference, we delete all nodes in the ReferencesList. Note, when
deleting CharALNodes between head and tail, it is necessary to check if head and tail
have been deleted before or not.
• When deleting a string, we need to pay attention to the CharALNodes that have a
low reference count so that after reducing the number of references, there might be a
chance that the number of references is zero, which may leads to the deletion of some
CharALNodes. To improve the search efficiency in the ReferencesList, we will always
maintain increasing order on this list. Then, as a matter of course, nodes with 0
reference will always be at the top of the list. However, those nodes are no longer needed
for the search. Therefore, nodes with 0 reference should be at the end ReferencesList.
See also Figures 7, 8, 9 illustrating ReferenceList and DeleteStringList through the sequence
deletion steps. Note: Figure 9, DeleteStringList does not illustrate the final result. Since the
number of references to ” world” is zero, the first node of the DeleteStringList (including ”no.
references of s1 head” and ”no. references of s1 tail”) will be deleted. DeleteStringList then
has only 1 single node.
Data Structures and Algorithms’ assignment - Semester 1 (2021 - 2022) Page 7/12
HCMC UNIVERSITY OF TECHNOLOGY - VNU-HCM
FACULTY OF COMPUTER SCIENCE AND ENGINEERING
Data Structures and Algorithms’ assignment - Semester 1 (2021 - 2022) Page 8/12
HCMC UNIVERSITY OF TECHNOLOGY - VNU-HCM
FACULTY OF COMPUTER SCIENCE AND ENGINEERING
Example 3.5
Example 3.6
Data Structures and Algorithms’ assignment - Semester 1 (2021 - 2022) Page 9/12
HCMC UNIVERSITY OF TECHNOLOGY - VNU-HCM
FACULTY OF COMPUTER SCIENCE AND ENGINEERING
Example 3.7
Example 3.8
Example 3.9
3.4 Requirements
Data Structures and Algorithms’ assignment - Semester 1 (2021 - 2022) Page 10/12
HCMC UNIVERSITY OF TECHNOLOGY - VNU-HCM
FACULTY OF COMPUTER SCIENCE AND ENGINEERING
• All the methods in this description must be implemented for the compilation to be
successful. If the student has not implemented a method yet, provide an empty
implementation for that method. Each test case will call some described methods
to check the returned results.
• There is only one include directive in the ConcatStringList.h file #include ”main.h”
and one directive in the ConcatStringList.cpp file #include ”ConcatStringList.h”.
Apart from the above directives, no other #include is allowed in these files.
6. There are some simple testcases in the main.cpp file in the format ”tc<x>()”. The
output of the function ”tc<x>()” is recorded in the file ”tc<x>.txt” in the directory
sample output.
7. Students are allowed to write additional methods and properties in classes that are re-
quired to be implemented.
8. Students are required to design and use data structures based on learned lists.
9. The student must free all the dynamically allocated memory when the program termi-
nates.
Data Structures and Algorithms’ assignment - Semester 1 (2021 - 2022) Page 11/12
HCMC UNIVERSITY OF TECHNOLOGY - VNU-HCM
FACULTY OF COMPUTER SCIENCE AND ENGINEERING
4 Submission
Students submit only 2 files: ConcatStringList.h and ConcatStringList.cpp, before the deadline
given in the link ”Assignment 1 - Submission”. There are a number of simple test cases used
to check student work to ensure that student results are compilable and runnable. Students
can submit as many times as they want, but only the final submission will be graded. Since
the system cannot bear the load when too many students’ submissions at once, students should
submit their work as soon as possible. Students do so at their own risk if they submit assign-
ments by the deadline. When the submission deadline is over, the system will close so students
will not be able to submit any more. Submissions through other means will not be accepted.
5 Other regulations
• Students must complete this assignment on their own and must prevent others from
stealing their results. Otherwise, the student treat as cheating according to the regulations
of the school for cheating.
• Any decision made by the teachers in charge of this assignment is the final decision.
• Students are not provided with testcases after grading, but are only provided with infor-
mation on testcase design strategies and distribution of the correct number of students
according to each test case.
• Assignment contents will be harmonized with a question in exam with similar content.
6 Changelog
• Add a “Note” bullet point in the description of ConcatStringList subString(int from,
int to) const.
• Correct example 3.9 in the description of string totalRefCountsString() const.
• Modify the condition to delete all the nodes inside a ReferencesList in section 3.3.
• Add another “Note” bullet point in the description of ConcatStringList concat(const
ConcatStringList & otherS) const
———————END———————
Data Structures and Algorithms’ assignment - Semester 1 (2021 - 2022) Page 12/12