DATA STRUCTURES AND
ALGORITHMS
NAME: HOANG QUANG QUYEN
Abstract
Quyenhqgcs210314
Assignment Brief 2 (RQF)
Higher National Certificate/Diploma in Business
Student Name/ID Number:
Unit Number and Title: Unit 19: Data Structures and Algorithms
Academic Year: 2021
Unit Assessor:
Assignment Title: Implement and assess specific DSA
Issue Date:
Submission Date:
Internal Verifier Name:
Date:
Submission Format:
Format:
● The submission is in the form of an individual written report and a presentation. This should be
written in a concise, formal business style using single spacing and font size 12. You are required
to make use of headings, paragraphs and subsections as appropriate, and all work must be
supported with research and referenced using the Harvard referencing system. Please also provide
a bibliography using the Harvard referencing system.
Submission
● Students are compulsory to submit the assignment in due date and in a way requested by the
Tutor.
● The form of submission will be a soft copy posted on http://cms.greenwich.edu.vn/.
● Remember to convert the word file into PDF file before the submission on CMS.
Note:
● The individual Assignment must be your own work, and not copied by or from another student.
● If you use ideas, quotes or data (such as diagrams) from books, journals or other sources, you
must reference your sources, using the Harvard style.
● Make sure that you understand and follow the guidelines to avoid plagiarism. Failure to comply
this requirement will result in a failed assignment.
Unit Learning Outcomes:
LO3 Implement complex data structures and algorithms
LO4 Assess the effectiveness of data structures and algorithms
Assignment Brief and Guidance:
Assignment scenario
Continued from Assignment 1.
Tasks
For the middleware that is currently developing, one part of the provision interface is how message can
be transferred and processed through layers. For transport, normally a buffer of queue messages is
implemented and for processing, the systems requires a stack of messages.
The team now has to develop these kind of collections for the system. They should design ADT /
algorithms for these 2 structures and implement a demo version with message is a string of maximum
250 characters. The demo should demonstrate some important operations of these structures. Even it’s a
demo, errors should be handled carefully by exceptions and some tests should be executed to prove the
correctness of algorithms / operations.
The team needs to write a report of the implementation of the 2 data structures and how to measure the
efficiency of related algorithms. The report should also evaluate the use of ADT in design and
development, including the complexity, the trade-off and the benefits.
Learning Outcomes and Assessment Criteria (Assignment 2)
Pass Merit Distinction
LO3 Implement complex data structures and algorithms
P4 Implement a complex ADT M4 Demonstrate how the
D3 Critically evaluate the
and algorithm in an executable implementation of an
complexity of an
programming language to solve ADT/algorithm solves a well-
implemented
a well defined problem. defined problem
ADT/algorithm
P5 Implement error handling
and report test results.
LO4 Assess the effectiveness of data structures and algorithms
P6 Discuss how asymptotic M5 Interpret what a trade-off is D4 Evaluate three benefits of
analysis can be used to assess when specifying an ADT using using implementation
the effectiveness of an an example to support your independent data structures
algorithm answer
P7 Determine two ways in
which the efficiency of an
algorithm can be measured,
illustrating your answer with
an example.
ASSIGNMENT 2 FRONT SHEET
Qualification BTEC Level 5 HND Diploma in Computing
Unit number and title Unit 19: Data Structures and Algorithms
Submission date Date Received 1st submission
Re-submission Date Date Received 2nd submission
Student Name HOANG QUANG QUYEN Student ID GCS210314
Class GCS1003A Assessor name
Student declaration
I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that
making a false declaration is a form of malpractice.
Student’s signature
Grading grid
P4 P5 P6 P7 M4 M5 D3 D4
Summative Feedback: Resubmission Feedback:
Grade: Assessor Signature: Date:
Internal Verifier’s Comments:
IV Signature:
1
Table of Contents
Part 1:Implement ADT and algorithms .................................................................................3
1.1 Description of the "messaging" application ......................................................3
1.2 ADT ........................................................................................................................3
1.2.1 Implement (P4,P5) ...............................................................................................3
1.2.1a. Explain your implement of data structure (P4): .................................................3
a) Main ....................................................................................................................3
b) Queue .................................................................................................................5
c) Stack ...................................................................................................................9
1.2.1b. Explain how you handle error by exception (P5): ............................................12
1.2.2 Test (P5) ............................................................................................................12
1.2.3 Result (P5) .........................................................................................................13
Part 2:Analysis ...................................................................................................................15
1. Big O (P6) ............................................................................................................15
2. Analyze performances (P7) ................................................................................16
REFERENCES: .................................................................................................................18
2
Part 1:Implement ADT and algorithms
1.1 Description of the "messaging" application
For you to enter messages more quickly, sort them when you send them, and keep track
of the messages you've sent, I'll build a tool in this course. I built this program using the
queue and stack data structures.
The queue and stack data structures are what I think work best for this application. My
program uses queues to store messages in the system. When messages are saved to the
system, queue will make sure they are arranged in FIFO (first-in, first-out) order. And in
this situation, I will utilize a stack for the application when we wish to store and output
messages in a last-in, first-out (LIFO) method.
1.2 ADT
1.2.1 Implement (P4,P5)
1.2.1a. Explain your implement of data structure (P4):
For the queue and stack data structures to be implemented, two separate classes must be
created. The queue is implemented using an ArrayList, with new members being added to
the list's end while the front and back items are monitored. During the dequeue process,
the ArrayList's front-most elements are eliminated. The List, which is used to implement
the stack, has both element insertion and removal happen at the same end.
In my application, I will create 3 files which are Main, Queue and Stack.
a) Main
3
In my MAIN, I design some option for my app
I will design 5 options using switch case and if else. The first option is to enter the message
lines. The 2nd option to receive the messages that the user sent in the first choice. The third
option is to let the user view the messages in the order of the Queue (FIFO). The 4th option
helps the user to check the most recent messages in stack order (LIFO)
4
b) Queue
It will perform some operations while I design the queue..
1. Returns value of element at front:
This is a generic class called "Queue" with the type parameter "T," which implies it may
function with any data type.
There are three instance variables in the class: "front", "rear", and "A". Both "front" and
"rear" are set to - 1, indicating that the queue is empty. "A" is an ArrayList that holds
the queue's items.
The class has a "front" function that returns the element at the front of the queue. The
function returns null if "front" is -1, indicating that the queue is empty. Instead, the
function fetches the element at the "front" index of the ArrayList "A" using the
"A.get(front)" command, and returns it.
This class defines a general queue data structure using an ArrayList to hold the
components, and the "front" function returns the element at the front of the queue.
2. Returns value of element at rear:
In a class that implements a queue data structure, there is a generic method called
"rear" that is used here. Without deleting it, the procedure returns the element at the
end of the queue.
When the queue is empty, the class's instance variable "rear" is initialized to -1. The
function returns null if "rear" is -1, indicating that the queue is empty. Instead, the
function acquires the element at the "rear" index of the ArrayList "A" using the
"A.get(rear)" command, and returns it.
5
This function can be used with the "front" method to obtain details about both ends of
the queue. It returns the element at the back of the queue without deleting it.
3. Add a new item to the back of the line. It returns nothing and needs the
element to be inserted:
The "empty" method is used to determine whether the queue is empty before
proceeding. The front and rear pointers are both set to 0 if the queue is empty,
indicating that there is only one element in the queue, which is "X". The
expression "A.add(X)" is used by the function to add "X" to the ArrayList "A".
The procedure advances the front pointer to the following available position if the
queue is not empty by using the "front++" command to increase it. The next step is
to see if there is enough room in ArrayList "A" to store "X" at the front position. If
"A" has a size higher than "front", it signifies that thereisan vacant space at the
front of the queue, and the procedure sets the element at the front position of the
ArrayList "A" to "X" using the "A.set(front, X)" statement.
The method uses the "A.add(X)" command to add "X" to the end of the ArrayList
"A" if the front of the queue is not empty.
This function guarantees that the elements are stored in the ArrayList "A" in
the proper sequence to represent the queue by adding one element to the
back of the queue and adjusting the front and rear pointers accordingly.
6
This is a class method with the name "dequeue" that implements a queue data
structure with the letter "T" as the type argument. The action removes the element from
the queue and then returns it.
The "empty" method is then used to determine whether the queue is empty. The
method writes "Empty!" to the console if the queue is empty, indicating that there are
no elements in the queue. In this instance, "D," which has been initialized to "Empty," is
returned by the procedure.
The method determines if there is just one entry in the queue if the queue is not empty
by comparing the values of the front and rear pointers. The procedure obtains the
single element in the queue using the "A.get(rear)" statement and stores it in a variable
named "temp" if the front and rear pointers are equal.
After adjusting the front and rear pointers to -1 to indicate that the queue is no longer
empty, the function then returns "temp".
The procedure uses the "A.get(rear)" statement to get the element from the rear
position of ArrayList "A" and stores it in a variable called "temp" if there are several
elements in the queue. The rear pointer is then increased by the "rear++" instruction to
remove the element from the queue. The method then returns "temp".
This method deletes the item at the head of the queue, returns it, and modifies the front
and rear pointers to reflect the deletion. The method returns a value indicating that the
queue is empty if it is.
7
4. Verify whether or not the waiting list is empty. It returns a Boolean value and
doesn't require any parameters:
After adjusting the front and rear pointers to -1 to indicate that the queue is no longer
empty, the function then returns "temp".
The procedure uses the "A.get(rear)" statement to get the element from the rear
position of ArrayList "A" and stores it in a variable called "temp" if there are several
elements in the queue. The rear pointer is then increased by the "rear++" instruction to
remove the element from the queue. The method then returns "temp".
This method deletes the item at the head of the queue, returns it, and modifies the front
and rear pointers to reflect the deletion. The method returns a value indicating that the
queue is empty if it is.
By determining if the front and rear pointers are equal to -1, this method determines
whether the queue is empty and returns a boolean result indicating whether the queue
is empty or not.
5. Print the queue:
8
This is a queue data structure implementation function named "empty" in a class. A
boolean value indicating whether or not the queue is empty is returned by the
procedure.
The "front == -1 && rear == -1" sentence is used in the function to determine whether
the front and rear pointers are both equal to -1. The method returns "true" to denote
that the queue is empty if both pointers are equal to -1, which indicates that there are
no elements in the queue.
The method returns "false" to indicate that the queue is not empty if the front and rear
pointers are not equal to -1, which indicates that there are elements in the queue.
By determining if the front and rear pointers are equal to -1, this method determines
whether the queue is empty and returns a boolean result indicating whether the queue
is empty or not.
c) Stack
Together with the queue portion, I will develop certain operations for my stack that are
appropriate for the application.
1. size: returns the size of stack:
This code creates a class with a List of String objects called "elements" and a method
called "size" that it calls.
The aim of the "size" method is to return the number of elements in the List "elements".
The method returns the size of the List "elements" by utilizing the "size" method of the
List. The List's element count is returned by this method as an integer value.
The "size" method so returns an integer value that denotes the quantity of "elements"
in the List
9
2. push: to insert an element into the stack:
This code creates a class with a List of String objects called "elements" and a method
called "push" that belongs to that class.
A new String element is to be added to the end of the List's "elements" using the
"push" method.
The String parameter "element" that the method accepts stands in for the String
element that will be added to the List.
Using the List's "add" method and the String parameter "element," the method appends
the new String element to the end of the List. By doing this, the new element is
effectively added to the end of the List.
3. pop: remove an element from the stack:
This code creates a class called "elements" that has a method called "pop" and a List
of String objects called "elements" that are part of that class.
The last String member in the List of "elements" is located and removed using the
"pop" method.
The procedure checks that the List of "elements" is not empty at the beginning. If the
stack is empty, the method returns the String "Nothing in stack," signifying that there
are no String elements in the List. If not, the function retrieves the last String element in
the List by using the List's "get" method and passing in the index "elements.size() - 1,"
which is the index of the last member in the List. The retrieved element is stored in the
String variable "top".
Following the last element's retrieval, the function detaches it from the List by calling
the List's "remove" method and providing the index "elements.size() - 1". The final
element in the List is therefore essentially removed.
10
The procedure then returns the String value that corresponds to the value of the
removed last element and is kept in the "top" variable.
4. peek: The method returns the element at the top of the Stack else returns
NULL if the Stack is empty:
This code creates a class with a List of String objects called "elements" and a
method called "peek" that belongs to that class.
The "peek" method's objective is to extract the final String element from the List's
"elements" without deleting it.
The method verifies that the List "elements" is not empty at start. The method
returns null if it is empty, which means that there are no String entries in the List.
Otherwise, the function uses the List's "get" method and passes in the index
"elements.size() - 1," which is the index of the very last element in the List, to return
the final String element in the List.
5. Empty: returns true if stack is empty else false:
This code creates a class that has a List of String objects called "elements" and a
method called "isEmpty" that belongs to that class.
Checking whether or not the List "items" are empty is the goal of the "isEmpty" method.
11
The method's boolean return result indicates whether or not the List "elements" is
empty. The method checks whether the List's "elements" are empty by using the
"isEmpty" method. If there are no items in the list, the method returns "true," indicating
that the list is empty. If the List is not empty, the procedure returns "false," indicating
that the List is not empty.
1.2.1b. Explain how you handle error by exception (P5):
I used "try catch" to capture the InputMismatchException error in order to manage exception-
based errors. The block will utilize the "Function" nextInt() function to attempt to read user
input. An InputMismatchException will be raised outside if the input is not an integer, and
exceptions will be collected inside the catch block.
1.2.2 Test (P5)
Test case Input data Expecte Actual output Result
d
output
receive messages The pass
when the user has system
not entered will
anything in the report
system Empty
12
Display a message The pass
in Queue when the system
user has not will
entered anything in report
the system Empty
Display a message in The pass
Stack when the user system
has not entered will
anything in the report
Nothing in
system
Stack
Input messages in The pass
option system
will
report
Choose
the right
option
Input than 250 warning pass
characters system to
fill in less
than 250
character
s
1.2.3 Result (P5)
In this section I will test my application to see if it is working properly
First I choose option 1 to enter message
13
Next, I'll check the message inside Queue
I will then receive every message in the queue.
Last, I'll check the message inside Stack
14
Part 2:Analysis
1. Big O (P6)
A number of asymptotic notations are used to express the temporal complexity of
algorithms. In this instance, the "O" (Big O) notation is employed to determine the
temporal complexity. An algorithm's time complexity determines how long it will take to
execute. It is determined by adding together all the required operations. It is usually a
good habit to understand how the algorithm and its input alone determine the reason of
execution time. This can be accomplished by selecting a straightforward operation that
the algorithm frequently performs and defining the temporal complexity T(N) as the
number of operations the algorithm must complete given an array of length N.
Example:
After being used to basic operations and the single loop. Assume that there are
two loops with differing numbers of iterations in order to determine the time
complexity for nested loops. The inner loop will run M times if the outer loop runs
only once, giving us a series of M + M + M + M....N times. This may be
expressed as N * M. The same is illustrated in the example below:
15
2. Analyze performances (P7)
“enqueuer” algorithm in Queue class:
Evaluation:
● The enqueue algorithm in the Queue class adds an element to the end of the queue.
● The algorithm first checks if the queue is empty. If it is, it sets both the front and
rear pointers to 0, and adds the element to the list A.
● If the queue is not empty, it increments the front pointer, and if the list A has
enough space for the new element, it replaces the element at the front pointer
with the new element. Otherwise, it appends the new element to the end of the
list A.
● This algorithm is correct and efficient, with a time complexity of O(1) for the
best and average case and O(n) for the worst case when the list A needs to be
resized.
16
“push” algorithm in Stack class:
Evaluation:
● The push algorithm in the Stack class adds an element to the top of the stack.
● The algorithm simply appends the element to the end of the list of elements.
● This algorithm is correct and efficient, with a time complexity of O(1) for the best,
average, and worst case.
17
REFERENCES:
GeeksforGeeks. (2020). Examples of Big-O analysis. [online] Available at:
https://www.geeksforgeeks.org/examples-of-big-o-analysis/.
abstract data type? [online] Available at:
https://stackoverflow.com/questions/41989155/java-util-arraylist-is-implementation-of-
data-structure-collection-or-abstract-d.
W3Schools (2019). Java ArrayList. [online] W3schools.com. Available at:
https://www.w3schools.com/java/java_arraylist.asp.
Geeksforgeeks (2016). ArrayList in Java. [online] GeeksforGeeks. Available at:
https://www.geeksforgeeks.org/arraylist-in-java/.
GeeksforGeeks. (2018). FIFO (First-In-First-Out) approach in Programming. [online]
Available at: https://www.geeksforgeeks.org/fifo-first-in-first-out-approach-in-programming/.
GeeksforGeeks. (2021). What is Software Stack. [online] Available at:
https://www.geeksforgeeks.org/what-is-software-stack/.
GeeksforGeeks. (2022). Introduction to Stack memory. [online] Available at:
https://www.geeksforgeeks.org/introduction-to-stack-memory/.
GeeksforGeeks. (2013). What is Linked List. [online] Available at:
https://www.geeksforgeeks.org/what-is-linked-list//.
Tutorialspoint.com. (2020). Data Structure and Algorithms - Stack - Tutorialspoint. [online]
Available at:
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm.
SearchAppArchitecture. (n.d.). What is a Software Stack? [online] Available at:
https://www.techtarget.com/searchapparchitecture/definition/software-stack.
18