Professional Documents
Culture Documents
Unit number and title Unit 19: Data Structures and Algorithms
Submission date 06 March 2022 Date Received 1st submission 06 March 2022
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:
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).
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).
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
• 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
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
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).
• 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).
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 --
Peek == S(TOP)
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].
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].
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].