0% found this document useful (0 votes)
11 views26 pages

1 Unit 19 Assignment 1 1649 Frontsheet

File
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views26 pages

1 Unit 19 Assignment 1 1649 Frontsheet

File
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

lOMoARcPSD|28555728

1 Unit 19 - Assignment 1 1649 frontsheet

Cloud Computing (University of Greenwich)

Studocu is not sponsored or endorsed by any college or university


Downloaded by Tran Minh Quang (FGW HN) ([email protected])
lOMoARcPSD|28555728

ASSIGNMENT 1 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 19: Data Structures and Algorithms

Submission date May 28, 2023 Date Received 1st submission

Re-submission Date Date Received 2nd submission

Student Name VO PHUONG THUC Student ID GCC210167

Class GCC1001 Assessor name TRAN THI KIM KHANH

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 THUC

Grading grid

P1 P2 P3 M1 M2 M3 D1 D2

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date:


Internal Verifier’s Comments:

IV Signature:

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

Assignment Brief 1 (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: Examine and specify ADT and 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.

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

Unit Learning Outcomes:

LO1 Examine abstract data types, concrete data structures and algorithms

LO2 Specify abstract data types and algorithms in a formal notation

Assignment Brief and Guidance:

Assignment scenario

You work as in-house software developer for Softnet Development Ltd, a software body-shop providing network
provisioning solutions. Your company is part of a collaborative service provisioning development project and
your company has won the contract to design and develop a middleware solution that will interface at the front-
end to multiple computer provisioning interfaces including SOAP, HTTP, JML and CLI, and the back-end
telecom provisioning network via CLI .

Your account manager has assigned you a special role that is to inform your team about designing and
implementing abstract data types. You have been asked to create a presentation for all collaborating partners on
how ADTs can be utilised to improve software design, development and testing. Further, you have been asked to
write an introductory report for distribution to all partners on how to specify abstract data types and algorithms in
a formal notation.

Tasks
Part 1

You will need to prepare a presentation on how to create a design specification for data structures, explaining the
valid operations that can be carried out on the structures using the example of:

1. A stack ADT, a concrete data structure for a First In First out (FIFO) queue.
2. Two sorting algorithms.
3. Two network shortest path algorithms.
Part 2

You will need to provide a formal written report that includes the following:

1. Explanation on how to specify an abstract data type using the example of software stack.
2. Explanation of the advantages of encapsulation and information hiding when using an ADT.
3. Discussion of imperative ADTs with regard to object orientation.

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

Learning Outcomes and Assessment Criteria (Assignment 1)

Pass Merit Distinction

LO1 Examine abstract data types, concrete data structures and


algorithms
D1 Analyse the operation,
P1 Create a design M1 Illustrate, with an example, a using illustrations, of two
specification for data structures concrete data structure for a First network shortest path
explaining the valid operations In First out (FIFO) queue. algorithms, providing an
that can be carried out on the M2 Compare the performance of example of each.
structures. two sorting algorithms.

P2 Determine the operations of


a memory stack and how it is
used to implement function
calls in a computer.

LO2 Specify abstract data types and algorithms in a formal notation

P3 Using an imperative M3 Examine the advantages D2 Discuss the view that


definition, specify the of encapsulation and imperative ADTs are a basis
abstract data type for a information hiding when for object orientation and,
software stack. using an ADT.
with justification, state
whether you agree.

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

Contents
Assignment Brief 1 (RQF)...........................................................................................................................................3
Higher National Certificate/Diploma in Business....................................................................................................3
Data structures.............................................................................................................................................................8
I. Abstract data type (P1)....................................................................................................................................8
1. Definition....................................................................................................................................................8
2. Examples....................................................................................................................................................9
II. ADT usages...................................................................................................................................................16
1. Application of Stack in memory (P2).....................................................................................................16
2. Application of an ADT (P3).....................................................................................................................20
References.................................................................................................................................................................25

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

Figure 1. 5 steps of Defining and Implementing an ADT.............................................................................................8


Figure 2. Singly Linked List...........................................................................................................................................9
Figure 3. GetFirst operation.......................................................................................................................................10

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

Data structures
I. Abstract data type (P1)
1. Definition
 Abstract Data Type (ADT) definition
An abstract data type (ADT) is a data type notion or paradigm. ADT eliminates the need for
a user to be concerned about how a data type is implemented. Furthermore, ADT handles the
implementation of functions on a data type. The user will have preset functions for each data
type available for usage in any action.

With general, in ADT, a user understands what to do but not how to achieve it. These
models are characterized by the data items they include and the actions they perform.

ADTs are implemented differently in each programming language, employing distinct


techniques and logic. Regardless of language, we can still conduct all of the related operations
described for that ADT. ADTs, for example, are generally implemented in C using structure. In
C++ or JAVA, however, they are accomplished via classes. However, operations are universal
across all languages.
[CITATION htt231 \l 1033 ]
 Steps To Define And Implement ADT
In order to define implement ADT, we have to follow the 5 steps below of implementing
ADT:

Figure 1. 5 steps of Defining and Implementing an ADT

 Step 1: At first, we need to identify the problem (or the requirements of the
problem), and the target that the ADT will be designed and implemented for.

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

 Step 2: Here, the data (or attributes) will be identified so that the ADT can store and
manage them.
 Step 3: In this step, what you need to do is define math operations (for example,
functions in Java) to handle the data declared in the previous step.
 Step 4: In this step, we must define how the ADTs interact with each other or with
other parts of the program. This ensures that the ADT can function correctly and in
accordance with the requirements of the problem.
 Step 5: Finally, after having identified and described the required ADTs, we need to
implement them. This includes writing code to implement the ADT math operations.

2. Examples
 Singly Linked List: According to [ CITATION gee23 \l 1033 ], a singly linked list is a
linear data structure in which the members are not stored in contiguous memory regions and
are only linked to the next entry through a pointer.

Figure 2. Singly Linked List

 At the top of the list, there is a pointer called "head", which points to the first node of the
list, and the top node of the list always has an adjacent node with the value "NULL"

 The strength of Singly Linked List is the ability to quickly add and remove elements at the
beginning and end of the list. However, its weakness lies in the fact that when we want to
access any element, we have to traverse from the beginning of the list to that element.

 The basic operations of Singly Linked List include:


o - Add element to the beginning of the list
o - Add element to the end of the list
o - Remove the element at the beginning of the list
o - Remove the element at the end of the list
o - Get the element at the beginning of the list then return the value of the element
o - Get the element at the end of the list then return the value of the element

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

 Here are some diagrams showing how the basic operations in a Singly Linked List work:
o GetFirst: Returning the element of the first element of the list, if the list does not
contain any element, then return NULL. If it does contain one or more elements, then
return the head element.

Figure 3. GetFirst

10

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

o GetLast: Returning the element of the last element of the list. In getLast function there
are 3 cases:
 Case 1: The list is empty, then the return value is NULL.
 Case 2: List has only one element, return head.
 Case 3: The list has 2 or more elements, then a new node will be created called
“current”, is initialized with the head node of the linked list, a while loop will be
also executed. In the loop, the condition is “current.next” is not NULL, this will
bring the pointer pointing head to the last element of the list. After that, return the
element that current is pointing at.

Figure 4. GetLast

11

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

o AddFirst: The function when executed will add a new node to the first position of the
list. For example, if we have a link list with 3 elements, the AddFirst function will
create a new node named newNode, attach the newNode next pointer to the value of the
head pointer, this ensures that newNode will point to the first position of the list. Then,
assign the head pointer of the list using newNode, making it the first element of the list.
Finally, increase the size of the list by 1.

(NOTE: The red ray at step 1 represents an unofficial step)

Figure 5. AddFirst

12

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

o AddLast: The addLast method helps the user perform the operation of adding an
element to the last position of a linked list. The function can be divided into three cases:
 Case 1: The linked list is empty, we assign newNode as the head of the linked list.
Increase the size of the linked list by 1.
 Case 2: The linked list contains 1 element, then assign head's next pointer to the
value of newNode. Increase the size of the linked list by 1.
 Case 3: The linked contains two or more elements, we create a new Node called
current and assign the head's pointer to it. Then, we initialize a while loop with the
condition that current.next must not be null. If the condition is satisfied, we assign
the pointer of current.next to current in order to move current forward by one unit.
The loop stops when current.next is null. Once the loop ends, we assign current.next
to newNode, effectively adding the new element at the end of the linked list.
Increase the size of the linked list by 1.

Figure 6. AddLast

13

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

o RemoveFirst: The removeFirst function, when executed, removes the first element of a
linked list. There are two cases that can occur when running this function:
 Case 1: The linked list is empty, it throws an exception indicating that the list does
not contain any elements.
 Case 2: The linked list contains 1 element, then the function sets the head pointer to
null.
 Case 3: The linked list contains two or more elements, the function creates a new
Node called oldHead to store the value of the current head. Then, it moves the head
pointer to the next node (head.next). Next, it sets the next pointer of oldHead to
null, effectively breaking the connection between the deleted node and the
remaining nodes in the linked list. Finally, it decreases the size variable by 1.

(NOTE: The red ray at step 2 represents an unofficial step)

Figure 7. RemoveFirst

14

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

o RemoveLast: The removeLast function, when executed, removes the last element of a
linked list. There are three cases that can occur when running this function:
 Case 1: The linked list is empty, it throws an exception indicating that the list does
not contain any elements.
 Case 2: The linked list contains 1 element, then the function sets the head pointer to
null.
 Case 3: The linked list contains two or more elements, the function creates a
variable `current` and assigns the value of the head to it. After that, we use a while-
loop, checking the condition `current.next.next != NULL`, in order to traverse
`current` to the second-to-last Node in the linked list. When the loop stop, we set
`current.next = NULL`, in order to remove the last Node in the list. Finally,
decrease the size of the list by 1.

Figure 8. RemoveLast

15

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

II. ADT usages


1. Application of Stack in memory (P2)
a. Stack definition
Stack memory is a memory usage mechanism that allows the system memory to be used as
temporary data storage that behaves as a first-in-last-out buffer. One of the essential elements
of stack memory operation is a register called the Stack Pointer. The stack pointer indicates
where the current stack memory location is, and is adjusted automatically each time a stack
operation is carried out.
[ CITATION Jos15 \l 1033 ]
b. Stack operations

Figure 9. Memory Stack

In computer science, a memory stack, also known as a call stack or execution stack, is a
data structure that stores information about function calls in a program. It is primarily used to
manage the execution of functions and track their local variables.
The memory stack operates based on two fundamental operations: push and pop.
 Push: The push operation adds an element to the top of the stack. When a function
is called, a stack frame is created, which contains the function's local variables,
parameters, and the return address (the memory address to which the program should
return after the function completes). The push operation is used to store this stack frame
on top of the stack.
 Pop: The pop operation removes the top element from the stack. When a function
finishes executing, its stack frame is popped from the stack, and the program resumes
execution at the return address stored in the popped stack frame. The pop operation
16

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

deallocates the memory occupied by the stack frame, making it available for future
function calls.
 Peek: The peek operation in the context of a memory stack allows you to examine
the top element of the stack without removing it. It provides a way to access the value
of the top element without modifying the stack's structure.
 Clear: The clear operation in the context of a memory stack refers to removing all
the elements from the stack, effectively emptying it. This operation allows you to reset
the stack to its initial state or prepare it for storing a new set of elements.
 isEmpty: The isEmpty operation in the context of a memory stack allows you to
check whether the stack is empty, i.e., whether it contains any elements or not. It helps
you determine if the stack is in a state where no further elements can be popped from it.
c. The way a memory stack work
When a program starts, a stack frame is created to represent the main function or the initial
entry point. The stack frame contains information such as local variables, function parameters,
and the return address. The return address represents the memory address where the program
should continue execution after the function call completes.
Here's a step-by-step explanation of how a memory stack works:
1) Function Call: When a function is called, a new stack frame is created and pushed
onto the top of the stack. The new stack frame represents the called function and
contains the necessary information specific to that function, such as its local
variables and parameters.
2) Stack Frame Initialization: The stack frame is initialized with the function's local
variables and parameters. The local variables are allocated memory within the
stack frame, and the parameter values are assigned.
3) Execution Flow: The program execution continues within the called function. The
function performs its operations, manipulates variables, and may call other
functions. If a function call occurs within the current function, a new stack frame is
created, and the process repeats.
4) Return Address: As the program encounters a return statement or reaches the
end of a function, the return address is used to determine where the program
should resume execution after the function call. The return address is stored within
the stack frame.
5) Stack Frame Removal: Once a function call completes, its stack frame is popped
from the top of the stack, deallocating the memory occupied by the local variables
and parameters. The program resumes execution at the return address stored in
the popped stack frame, continuing from where it left off.
d. Function call in a computer

 Explaination

17

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

In computer science, a function call is a request made by a program or script that performs
a predetermined function. During a function call, a function may accept one or more inputs as
arguments, process them, and return the result to the calling program. Calling or invoking the
function locates the function in the memory, furnishing it with arguments and causing it to
execute. When a function is called, control is passed to the function where it is actually
defined, and the actual statements are executed before control is passed again to the calling
program.
[ CITATION Com17 \l 1033 ]
 The steps of how function call works
When a function is called in a computer program, a series of steps occur to facilitate the
execution of that function. Here's a general overview of how a function call works:
1) Function Call: The program encounters a function call statement, which includes
the function name and any necessary arguments or parameters.
2) Passing Arguments: If the function has parameters, the values or references of the
arguments are passed to the function. Depending on the programming language, this
can be done by value (making a copy of the argument's value) or by reference
(passing a memory address or reference to the argument).
3) Stack Frame Creation: A new stack frame (also known as an activation record or
stack activation) is created on top of the memory stack. The stack frame is used to
store information related to the function call, including the return address,
parameters, local variables, and other necessary data.
4) Control Transfer: The program's control is transferred to the called function. The
execution jumps to the starting point of the function's code.
5) Function Execution: The function code is executed within the context of its stack
frame. This includes performing computations, accessing variables, manipulating
data, and potentially calling other functions.
6) Stack Frame Cleanup: When the function finishes executing, the stack frame for
that function is removed from the top of the stack. This process is known as stack
frame unwinding or stack frame deallocation.
7) Return Value: If the function has a return value, it is computed and returned to the
caller. The return value can be a specific data type or even a reference to an object
or data structure.
8) Control Transfer: The program's control is transferred back to the point
immediately after the function call. Execution continues from that point using the
returned value if needed.

18

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

e. Stack work in Function call


 Code:

19

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

 Illustration:

 Explaination:
The program begins by executing the main() method, which prints an initial message.
Inside main(), the program then proceeds to call the A1() method. This transfer of control leads
to the execution of A1(). Within A1(), the string "A1" is printed, followed by a call to the A2()
method. This transfer of control results in the execution of A2(), where the string "A2" is
printed. Subsequently, the A3() method is invoked from within A2(), causing the program's
control to shift to A3(). Inside A3(), the string "A3" is printed. Once A3() completes its
execution, control is returned to A2(), which also finishes executing. The program then reverts
to A1(), completing its execution as well. Finally, control returns to the main() method, and the
program execution concludes. The final result would be “A3 A2 A1”.

2. Application of an ADT (P3)


a. The reason why I chose Singly Linked List

I chose Singly Linked List to solve the problem because it can be beneficial for solving certain
problems due to several reasons:

 First, a Singly Linked List provides efficient insertion and deletion operations at the
beginning and end of the list. This makes it suitable for scenarios where frequent modifications
to the list structure are required.
 Second, a Linked List can dynamically grow and shrink as elements are added or removed,
eliminating the need for preallocating a fixed size. This flexibility is advantageous when the
number of elements in the list is uncertain or varies over time.
 Additionally, a Singly Linked List allows for easy traversal of the elements in a forward
direction. This characteristic is valuable when accessing or processing elements sequentially.
 Lastly, a Singly Linked List requires less memory compared to other data structures like
arrays, especially when the number of elements is unknown in advance. This memory
efficiency makes it suitable for resource-constrained environments.
b. Imperative Abstract Data Type definition

20

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

An imperative abstract data type (ADT) is a programming construct that encapsulates data and
the operations performed on that data, providing a well-defined interface to interact with the data.
It is considered imperative because it focuses on the specific steps or commands needed to
manipulate the data.

An imperative ADT consists of two main components:

 Data Representation: This component defines the internal representation of the data and
how it is stored. It includes the data structures and variables necessary to hold the data values.
The data representation component is not directly accessible to the users of the ADT but is
managed internally.
 Operation Definitions: This component specifies the operations or methods that can be
performed on the data. It defines the behavior and functionality of the ADT. Each operation has
a set of preconditions (conditions that must be satisfied before the operation can be executed),
postconditions (conditions that must hold true after the operation completes), and side effects
(any changes or effects produced by the operation).

The imperative ADT allows users to interact with the data by invoking the operations provided by the
ADT. The data and the implementation details are hidden from the users, who only need to understand the
interface and use the provided operations to manipulate the data.

Imperative ADTs are commonly used to model and abstract real-world concepts and problem domains,
providing a modular and organized way to manage and manipulate data. They promote encapsulation,
modularity, and information hiding, which can lead to more maintainable and reusable code.

c. Specify my chosen ADT


Class Node<E>
Instance variables:
value: variable used to store the value of the node: =E
next: pointer to the next Node
Class SinglyLinkedList<E>
Instance variables:
head: indicates the head of Node: =Node<E>
size: size of node: =int
Operations:
public addFirst: E t -> ()
n <- Node(t)
pre(precondition):
none
post(postcondition):
size++
head <- n
invariants:

21

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

size >= 0

public addLast: E t -> ()


n <- Node(t)
current <- head
while current.next != null
then current <- current.next

pre:
none
post:
if size == 0 then head <- n
else then n.next = head and current.next <- n
head <- n
size++
invariants:
size>=0

public removeFirst: () -> E


pre:
size > 0
post:
size--
head <- head.next
invariants:
size >= 0

public removeLast: () -> E


current <- head
while current.next.next != null
then current <- current.next
pre:
size > 0
post:
if size == 1 then head <- null
else then current.next <- null
size--
invariants:
size >= 0

22

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

public getFirst: () -> E


pre:
size > 0
post:
return head.getValue()
exception:
MyException(401) if isEmpty()

public getLast: () -> E


current <- head
while current.next != null
then current <- current.next
pre:
none
post:
if !isEmpty() then return current.getValue()
else if isEmpty() then return null
else return head.getValue()
d. Describing the problem

My program was created for the purpose of managing switches in a store. Using a singly linked
list includes functions such as adding a switch to the beginning and end of the list, and removing
the switch at the beginning and end of the list. Finally, see the number of switches in the list.

e. Describe the sequence of operations called by the user on the concrete class / ADT.

Code in main Actual Output Explaination


The program when launched will list
8 options (on the output console). In
this first test case will be case 1, the
screen will display a request to enter
the Switch name into the program,
assign it to the name variable. Next,
call the addFirst() function through
the switchList and pass in the name
variable. Finally display the message
added successfully.
In this case, first consider whether
switchList is empty or not. If it is,
it shows the message that the list
is empty. If not, proceed to call
removeLast() via switchList.
Finally, iterate through the list of
switches remaining in the list after

23

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

removing the switch.


First, check whether switchList is
empty or not. If not empty,
proceed to assign the value of the
first switch in the list to the
firstSwitch variable through the
getFirst function from switchList.
Finally, print the switch you just
got. In case of empty, return the
message that switchList is empty.

Enter will proceed to check if the


switchList is empty. If empty,
returns the message that the list is
empty. If not empty, call
removeFirst to remove the first
switch in the list, and go through
all the elements in and show them.
In this case, switchList is empty,
which means the program will
return an empty list message.

24

Downloaded by Tran Minh Quang (FGW HN) ([email protected])


lOMoARcPSD|28555728

References
ComputerHope, 2017. ComputerHope. [Online]
Available at: https://www.computerhope.com/jargon/f/funccall.htm

Datta, S., 2023. Baeldung. [Online]


Available at: https://www.baeldung.com/cs/adt
[Accessed 2023].

geeksforgeeks, 2023. GeeksForGeeks. [Online]


Available at: https://www.geeksforgeeks.org/data-structures/linked-list/singly-linked-list/

Yiu, J., 2015. Science Direct. [Online]


Available at: https://www.sciencedirect.com/topics/engineering/stack-memory

25

Downloaded by Tran Minh Quang (FGW HN) ([email protected])

You might also like