1 - Unit 19 - Assignment 1 Frontsheet
1 - Unit 19 - Assignment 1 Frontsheet
Unit number and title Unit 19: Data Structures and Algorithms
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.
Grading grid
P1 P2 P3 M1 M2 M3 D1 D2
Summative Feedback: Resubmission Feedback:
IV Signature:
Table of Contents
CHAPTER 1: Create a design specification for data structures explaining the valid operations that can be carried out
on the structures. ____________________________________________________________________________ 4
Chapter 2: Determine the operations of a memory stack and how it is used to implement function calls in a
computer. _______________________________________________________________________________ 13
Chapter 3: Using an imperative definition, specify the abstract data type for a software stack. ___________ 19
Table of figures
2.1 ) Definition:
The abstract data type (ADT) definition only refers to what activities will be performed, not how this
operation will be performed. Moreover, it does not specify how the data will be organized in memory and
which algorithm will be used to perform the operations. So why is it called online abstraction? Because it
brings an independent look done.
For exam ple :
we have used primitive values such as integers (int), real numbers (float), character types (char), string
types (string), and so on, only with knowledge of which type This data can work without any idea of how
they are implemented. So users only need to know what kind of data they can do, not how to deploy it.
Think of ADT as a black box that hides the internal structure and design of the data type.
Items can be added, deleted, and retrieved from any position in the list.
• Can be implemented as an array or dynamic array (to avoid identifying max size).
• An alternative implementation is a linked list. o Items are stored in nodes that linked
together via pointers.
• It’s difficult to specify a single list Abstract Data Type (ADT) that covers both arrays and
linked lists:
o One issue is the representation of the position.
o Array position is represented by an integer.
Linked list position is represented by the pointer.
• In case of an array with n elements, a “position” is simply an integer in the range from 0 to
n-1.
• In a linked list, a position can be a pointer to one of the nodes in the list, but there are some
subtleties involved (see later).
• The List ADT Functions is given below.
Figure 3: the list of adt functions
A list contains elements of the same type arranged in sequential order and following operations can be
performed on the list:
The difference between stacks and queues is in elimination. In a stack, we remove the most recently
added items; In a queue, we remove less recently added items. The queue is open at both ends. One end
is always used to insert data (also known as line-up) and the other end is used to delete data (leave the
row). The queue data structure follows the First-In-First-Out method, ie the data that is entered first will
be accessed first.
Figure 4: Queue
Similar to the stack data structure, queue data structures can also be implemented using Arrays, Linked
Lists, Cursors, and Structures. For the sake of simplicity, the next section will explore the queues deployed
by using one-dimensional arrays.
Operations on Queue:
Operations on the queue data structure may involve initializing the queue, using data on the queue, and
then deleting data from memory. Listed below are some basic operations that can be performed on the
queue data structure:
Because the queue data structure maintains two data pointers: front and rear, the operations of this type
of data structure are quite complex when compared to stack data structures.
Accessing data from the queue is a two-step process: accessing data at the front cursor is pointing and
deleting data after that access.
To use the queue effectively, we also need to check the status of the queue.
For this purpose, here are some other support features of the queue: Peek () method: get the element at
the beginning of the queue, without deleting this element.
IsFull () method: Checks whether the queue is full or not. If when we are using a one-dimensional array to
deploy the queue, we only need to check the rear pointer to the MAXSIZE value to determine if the queue
is full.
In case of deploying queue by using Ring linked list IsEmpty () method: check if the queue is empty or not
If the value of front is less than MIN or 0, the queue has not been initialized yet, so the queue is empty. In
the queue data structure.
we always: (1) dequeue (delete) the data pointed by the front pointer and (2) enqueue (enter) the data
into the queue by the help of the rear cursor.
In the next section, we will learn about the support features of the queue data structure:
The peek () method of the queue data structure Like in the stack data structure, this function helps us to
look at the data at the beginning of the queue.
Figure 7: Stack
The stack can only be used for local variables that use up small amounts of memory. The good news is that
memory allocation and management will not be your problem and access to these objects is very fast. It
is subject to size limits and the fact that you cannot resize variables on the stack .
Also A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving
data. Such a stack resembles a stack of trays in a cafeteria: New trays are put on the top of the stack and
taken off the top. The last tray put on the stack is the first tray removed from the stack. For this reason, a
stack is called a LIFO structure: last in/first out.
clear() — Clear the stack. isEmpty() — Check to see if the stack is empty.
topEl() — Return the topmost element in the stack without removing it.
After pushing number 10 onto an empty stack, the stack contains only this number. After pushing 5 on the
stack, the number is placed on top of 10 so that, when the popping operation is executed, 5 is removed
from the stack, because it arrived after 10, and 10 is left on the stack. After pushing 15 and then 7, the
topmost element is 7, and this number is removed when executing the popping operation, after which the
stack contains 10 at the bottom and 15 above it.
Generally, the stack is very useful in situations when data have to be stored and then retrieved in reverse
order. One application of the stack is in matching delimiters in a program. This is an important example
because delimiter matching is part of any compiler: No program is considered correct if the delimiters
are mismatched.
o Attempting the execution of pop or top an empty stack should throws a StackEmptyException.
o Attempting the execution of push when there is not enough memory should throws a Out of Memories
errors.
When the method finishes executing, its corresponding stack frame is flush, the flow returns to the
calling method and the available space for the next method.
For example: Declaring int i = 4 it will put i = 4 on the stack Declaring y = 2 it will put y = 2 on the stack
(stacked on i = 4) Declaring class1 cls1 = new class1 (): this is an object type, so it will create cls1 object
in the heap and not refer to the object of cls1 on the stack (top in the stack). Stacks are a special type
of list where the addition or removal of an element is done at one end of the list called a top. In other
words, the stack is a data structure with two basic operations: addition (push) and removal (pop), in
which the removal will progress.
Push: Add an element to the top of the stack, meaning after the elements are already in the stack.
Pop: Releases and returns the element standing at the top of the stack.
A software moves all the parameters for the function to the stack in the reverse order in which they
were registered before executing a function.The program then issues a call telling which function it
wants to start.
Figure 9: The example of Stack
o Executing instructions is fast because the operand data is stored in consecutive memory locations.
o The length of the instructions is short because they do not have an address field.
If the method has formal parameters, they have to be initialized to the values passed as actual parameters.
In addition, the system has to know where to resume execution of the program after the method has
finished. The method can be called by other methods or by the main program (the method main()). The
information indicating where it has been called from has to be remembered by the system. This could be
done by storing the return address in main memory in a place set aside for return addresses.
For a method call, more information has to be stored than just a return address. Therefore, dynamic
allocation using the run-time stack is a much better solution. It needs to be stressed that the run-time
stack is maintained by a particular operating system. A Java stack used by the Java Virtual Machine was
briefly described. Java stack and run-time stack are two different entities. They are similar in that their role
in processing method calls is basically the same; therefore, they store similar information that enables this
processing, although they store this information differently. The role of the interpreter java is to convert
information bytecodes in .class files so that the run-time stack takes over the function of the Java stack,
which is only an abstract construct. The subsequent discussion is presented in terms of the run-time stack
rather than the Java stack, which in no way changes the logic of processing method calls, in particular,
recursive calls.
3) Abstract variable.
Imperative-style definitions of ADT often depend on the concept of an abstract variable, which may be
regarded as the simplest non-trivial ADT. An abstract variable V is a mutable entity that admits two
operations:
fetch(V) always returns the value x used in the most recent store(V, x) operation on the same
variable V.
As in so many programming languages, the operation store(V, x) is often written V ← x (or some
similar notation), and fetch(V) is implied whenever a variable V is used in a context where a value
is required. Thus, for example, V ← V + 1 is commonly understood to be a shorthand for
store(V,fetch(V) + 1). In this definition, it is implicitly assumed that storing a value into a variable U
has no effect on the state of a distinct variable V. To make this assumption explicit, one could add
the constraint that.
if U and V are distinct variables, the sequence { store(U, x); store(V, y) } is equivalent to { store(V,
y); store(U, x) }.
More generally, ADT definitions often assume that any operation that changes the state of one ADT
instance has no effect on the state of any other instance (including other instances of the same ADT) —
unless the ADT axioms imply that the two instances are connected (aliased) in that sense. For example,
when extending the definition of abstract variable to include abstract records, the operation that selects
a field from a record variable R must yield a variable V that is aliased to that part of R.
The definition of an abstract variable V may also restrict the stored values x to members of a specific set
X, called the range or type of V. As in programming languages, such restrictions may simplify the
description and analysis of algorithms, and improve their readability.
Note that this definition does not imply anything about the result of evaluating fetch(V) when V is
un initialized, that is, before performing any store operation on V. An algorithm that does so is usually
considered invalid, because its effect is not defined. (However, there are some important algorithms
whose efficiency strongly depends on the assumption that such a fetch is legal, and returns some arbitrary
value in the variable's range.
4) Instance creation
Some algorithms need to create new instances of some ADT (such as new variables, or
new stacks). To describe such algorithms, one usually includes in the ADT definition a
create() operation that yields an instance of the ADT, usually with axioms equivalent to
the result of create() is distinct from any instance in use by the algorithm.
This axiom may be strengthened to exclude also partial aliasing with other instances. On
the other hand, this axiom still allows implementations of create() to yield a previously
created instance that has become inaccessible to the program.
5) The advantages and disadvantages of stack
Stack is a data structure that operates on the principle of: last in first out (LIFO). To be intuitive, you can
understand that it is a pile of bowls, you stack the bowls up high, the one you stack on the end will be the
one you take out first and vice versa, the first bowl, at the bottom will is the last one you took out.
Advantages of stack:
Stacks to easy to carry
Memory saved because pointer is not relevants.
Disadvantages of stack:
It’s not dynamic.
This means the maximum size of the stack must be predefined and cannot be changed.
Figure 12: Stack Data Structure
Pop: take the record, similar take the bowl out of the stack
Length: returns the number of records, corresponding to the height of the stack stack.
Peak: returns to the first record, corresponding to your touching the top bowl isEmpty: Is the Stack
check empty?
Search: returns element position in Stack from the top of stack if not found returns -1.
Example for implement stack:
[2]. Opendsa-server.cs.vt.edu. 2020. 5.2. The List ADT — CS3 Data Structures &
Algorithms. [online] Available at: https://opendsa-
server.cs.vt.edu/ODSA/Books/CS3/html/ListADT.html [Accessed 11 August2021].
[4]. GeeksforGeeks, n.d. Introduction of Stack based CPU Organization. [Online] Available
at: https://www.geeksforgeeks.org/introduction-of-stack-based-
cpu organization/?fbclid=IwAR00U8EgZpVvUQ-
hqWPcpzIYnyikIh5xQWmVwE1yRn0euH8hadZrpPHoSqA [Accessed 11 August 2021].
[5]. Hu, Z., 2017. Zeyuan Hu's page. [Online] Available at:
https://zhu45.org/posts/2017/Jul/30/understanding-how-function-call-works/ [Accessed
11 August 2021].