You are on page 1of 12

ASSIGNMENT 1

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 19: Data Structures and Algorithms

Submission date 06 March 2022 Date Received 1st submission 06 March 2022

Re-submission Date Date Received 2nd submission

Student Name Pham Khac Thanh Phong Student ID GCH210005

Class GCH0905 Assessor name Do Hong Quan

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

P1 P2 P3 M1 M2 M3 D1 D2
 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date:


Internal Verifier’s Comments:

IV Signature:
Table of Contents
I. Create a design specification for data structures explaining the valid operations that can be
carried out on the structures (P1). ...................................................................................................... 4
1.1. Abstract Data Types (ADT) ................................................................................................. 4
1.2. Stack ADT ............................................................................................................................ 4
1.3. Queue ADT .......................................................................................................................... 6
II. Determine the operations of a memory stack and how it is used to implement function calls in
a computer (P2).................................................................................................................................. 7
2.1. Definition of Stack memory ................................................................................................. 7
2.2. The operations of memory stack .......................................................................................... 7
2.3. How memory stack used to implement function call ........................................................... 8
III. Using an imperative definition, specify the abstract data type for a software stack (P3). ...... 9
3.1. Formal specification ............................................................................................................. 9
3.2. Pre-condition, Post-condition, and Error-condition ........................................................... 10
3.3. Specify stack’s operations .................................................................................................. 10
IV. References ............................................................................................................................. 11

Table of Figures
Figure 1 – Abstract Data Types ......................................................................................................... 4
Figure 2 - Stack ADT ........................................................................................................................ 5
Figure 3 - Implementation of Push .................................................................................................... 5
Figure 4 - Queue ADT ....................................................................................................................... 6
Figure 5 - Implementation of Enqueue .............................................................................................. 7
Figure 6 - Operations of memory stack ............................................................................................. 8
Figure 7 – Stack Memory .................................................................................................................. 9
I. Create a design specification for data structures explaining the
valid operations that can be carried out on the structures (P1).
1.1. Abstract Data Types (ADT)
Data types are used to classify the type of values a variable can be stored and describe the possible
operations allowed on those values. Abstract Data Type or ADT is a concept of a data type, it
helps users to implement data more effortlessly. In general, ADTs are logical concepts that can be
implemented using different languages. Unlike usual data types where the variable must be
defined, the ADT allows the user to have predefined functions on each data type ready to use for
any operation (Datta, 2021).

Figure 1 – Abstract Data Types

Abstract data types are composed of several components (Datta, 2021):

• A data type or types is a data structure or structures


• A set of operations which can be called methods or operations
• A specification or signature is a precise description of the types of the methods
• A set of axioms is a precise set of rules about how the data behaves
• A set of implementations is hidden from the programmer who uses the data type.

1.2. Stack ADT


A stack is a liner ADT which follows the principle of Last-in Last-out (LIFO). Specifically, the
insertion and deletion operations are performed from the same end. There are two main operations
in a stack, which are push where a element is inserted on the top of a stack, and pop where the
most recently added element is removed from a stack (Datta, 2021).
Figure 2 - Stack ADT

There are 2 types of the stack, a register stack, and a memory stack.

• The register stack stores the addresses of the elements in the top region of a stack.
• The memory stack is more flexible, it can store a large amount of data.

The implementation of a stack is simple using a one-dimensional array. According to the LIFO
principle, a special variable Top is used to store the address of the most recently added element.
The operations such as push and pop will be performed around this variable, for example, when
an element is inserted into a stack, the Top variable stores the last inserted element (Datta, 2021).

Figure 3 - Implementation of Push


1.3. Queue ADT
A queue is a linear ADT that the insertion can only be executed at one end and deletion at another.
In particular, there are two ends in a queue, one end is the rear-end which is where the insertion
happens, the other end is the front-end, where the deletion takes place. A queue works based on
the First-in First-out rule (FIFO rule), which the request is processed in the order in which it
arrives (Datta, 2021).

Figure 4 - Queue ADT

According to the queue usage and implementation, a queue is divided into 4 different types,
namely simple queue, circular queue, special queue, and double-ended queue (Datta, 2021).

• A simple queue is the simplest form of a queue ADT that strictly follows the principle of
FIFO.
• A circular queue is a queue that has a circular structure. The first element in the queue can
point to the last element in the queue.
• A special queue or commonly known as a priority queue, stores a priority for each element
in the queue. The removal and insertion operations are based on the priority of each
element in the queue.
• A double-ended queue does not follow the principle of the FIFO rule, the insertion and
deletion can be performed from both rear and front ends.

The implementation of a queue must strictly follow the FIFO principle in which both insertion
and deletion are allowed from either the rear or front end. For example, when an element is being
inserted into a queue, each element in the queue must be shifted by one element, and the inserted
element must be pushed from the rear end (Datta, 2021).
Figure 5 - Implementation of Enqueue

II. Determine the operations of a memory stack and how it is used to


implement function calls in a computer (P2).
2.1. Definition of Stack memory
Memory stacks are linear data structures that allows system memory to be used as a LIFO
technique for temporary data storage. The data in a stack must always be the same type. The
elements in a stack that are inserted or removed must be in a linear order. An essential component
of a stack memory is the Stack Pointer, it shows the current stack memory position and change
automatically when a stack operation is performed (Joseph, 2015).

2.2. The operations of memory stack


There are four main operations of memory stack (Griffin, 2022):

• Push: The push operation refers to insert an element into a stack. The elements in the stack
will be pushed to the bottom without changing the order, and the new element will be place
at the top of the stack.
• Pop: The pop operation is removing an item from a loaded stack. The item that is popped
out of the stack is the last item that is added to the stack.
• Peek: The peek operation is simply returning the top element of the stack. It does not
involve adding or removing item from the stack.
• isEmpty: This operation determines whether the stack is empty or not, it will return true if
the stack is empty and false if the stack is not empty.
Figure 6 - Operations of memory stack

2.3. How memory stack used to implement function call


The figure 7 below shows in-dept operations of how memory stack works. It is strictly following
the LIFO principle of the stack. There are 3 functions each called one(), two(), and three(), these
functions will operate in order from one to three.

When the system is executed, the first function one() will be performed; as a result, the function
one() will be pushed into the memory stack. Next, functions two() and three() is called, the two
items will be pushed into the stack as the figure shown.
Figure 7 – Stack Memory

• Step 1: Push function one() into the stack.


• Step 2: Push function two() into the stack.
• Step 3: Push function three() into the stack.
• Step 4: Pop function three() out of the stack, the result is returned to function two().
• Step 5: Pop function two() out of the stack, the result is returned to function one().
• Step 6: Pop function one() out of the stack.

Once the last function is executed which is the function three(), the result will be returned to
functions two() and so on. Until the result is returned to the function one() which is the first
function called when the program is executed. In the step 4, when the function three() returned the
result to the function two(), it will be popped out of the memory stack. Lastly, the function one()
will be popped out of the stack when the program is finished, returning to its starting point.

III. Using an imperative definition, specify the abstract data type for a
software stack (P3).
3.1. Formal specification
Formal specification is a software specification that describes a language whose vocabulary,
syntax, and semantics are formally defined. The specification languages must be based on
mathematical concepts with well-defined properties. The main purpose of the formal specification
is to reveal problems and ambiguities in the system requirements. There are many different types
of formal specification languages, some of the most popular languages are Vienna Development
Method (VDM), Z notation, and Axiomatic specifications.

• VDM includes specification language VDM-SL, rules for data and operation refinement,
and a proof theory. It is a collection of strategies for the formal specification and
development of computing systems. VDM-SL is a model-oriented specification language
constructed from simple data types like sets, lists, and mapping (A.K.Sharma and Singh,
2013).
• Z notation is a schema that is based on typed set theory and first-order logic. It is used to
describe a specification's state and operations (A.K.Sharma and Singh, 2013).
• The axiomatic specification is a formal specification that defines the pre- and post-
conditions using first-order logic to specify the operations of the system. In detail, the pre-
conditions catch the requirements on the input parameters of a function. The post-
conditions are constraints on the result produced for the executive function (Webeduclick,
2019).

3.2. Pre-condition, Post-condition, and Error-condition


There are many ways to specify the requirements for a function; however, there are conditions
that is used to define certain parts of a program. These conditions are called pre-conditions, post-
conditions, and error-conditions.

• Pre-conditions: The pre-condition demonstrates what must be true before entering the
segment for the function to work correctly. The pre-conditions are commonly placed before
loops or at the entry of a function (Wesley, 1997).
• Post-conditions: The post-conditions indicate what should be true when the execution of
the segment is complete. It is often placed after the end of the function or loops (Wesley,
1997).
• Error-conditions: Error-conditions are the assumed action for many conditions that can be
used to check for several different conditions at one. In the real world, no program is
perfect, error is always enabled. As a result, the error condition is brought to specify any
error in the program, such as a signal error statement, an error during program execution.
In these situations, an error message is printed to exit the block and recover from the
condition (IBM, 2021).

3.3. Specify stack’s operations


• Push (S, E, TOP): This operation adds a new element to the stack, where E is the element,
S stands for stack, and TOP is the top of the stack
o Pre-condition: The current size of the stack must be smaller than the maximum size
of the stack (current size < maximum size).
o Post-condition: Start the TOP and find the next empty place, then insert the new
element into the top-pointing stack position. In addition, the current size of the stack
must increase by 1.

TOP = TOP + 1

E = S(TOP)

Current size ++

o Error-condition: The current size of the stack is greater than the maximum size of
the stack (current size > maximum size).
• Pop (S, E, TOP): This operation removes the top element of the stack, where E is the
element, S stands for stack, and TOP is the top of the stack
o Pre-condition: There must be at least one element in the stack, the current size of
the stack must greater than 0 (current size > 0).
o Post-condition: Find the top element of the stack and subtract the top value. The
current size of the stack must decrease by 1.

TOP = TOP - 1

E = S(TOP)

Current size --

o Error-condition: There are no elements in the stack (current size < 0)


• Peek (S, TOP): This operation returns the top element of the stack, where TOP is the top
of the stack, and S stands for stack.
o Pre-condition: The stack must not be empty (current size > 0)
o Post-condition: Return the top element of the stack

Peek == S(TOP)

o Error-condition: The stack is empty (current size < 0)

IV. References
A.K.Sharma and Singh, M., 2022. Comparison of the Formal Specification Languages Based
Upon Various Parameters. IOSR Journal of Computer Engineering (IOSR-JCE), pp.37-39.
Datta, S., 2021. What is Abstract Data Type?. [online] baeldung. Available at:
<https://www.baeldung.com/cs/adt> [Accessed 6 March 2022].

Griffin, L., 2022. Stacks in Computer Memory: Definition & Uses. [online] study. Available at:
<https://study.com/academy/lesson/stacks-in-computer-memory-definition-uses.html> [Accessed
6 March 2022].

IBM, 2021. ERROR condition. [online] IBM. Available at:


<https://www.ibm.com/docs/en/epfz/5.3?topic=conditions-error-condition> [Accessed 6 March
2022].

Joseph, Y., 2015. The Definitive Guide to Arm® Cortex®-M0 and Cortex-M0+ Processors.
[online] Sciencedirect. Available at: <https://www.sciencedirect.com/topics/engineering/stack-
memory#:~:text=4.4%20Stack%20Memory%20Operations,register%20called%20the%20Stack%
20Pointer.> [Accessed 6 March 2022].

webeduclick, 2019. Axiomatic and Algebraic Specification in Software Engineering. [online]


webeduclick. Available at: <https://webeduclick.com/axiomatic-and-algebraic-specification/>
[Accessed 6 March 2022].

Wesley, A., 1997. Data Structures and Other Objects. [online] note01. Available at:
<http://www.cs.albany.edu/~sdc/CSI310/MainSavage/notes01.pdf> [Accessed 6 March 2022].

You might also like