You are on page 1of 24

ASSIGNMENT 1 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 19: Data Structures and Algorithms

Submission date 11/8/2021 Date Received 1st submission

Re-submission Date Date Received 2nd submission

Student Name Bui Hoang Bao Anh Student ID GDD19909

Class GCD0806 Assessor name Ho Van Phi

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 Anh

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
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

Figure 1: Abstract data type ......................................................................................................................................... 4


Figure 2: Example of system design Abstract data type ............................................................................................... 5
Figure 3: the list of adt functions .................................................................................................................................. 7
Figure 4: Queue ............................................................................................................................................................ 8
Figure 5: Example for Queue: ....................................................................................................................................... 9
Figure 6: The result ..................................................................................................................................................... 12
Figure 7: Stack ............................................................................................................................................................. 13
Figure 8: The example of stack ................................................................................................................................... 14
Figure 9: The example of Stack ................................................................................................................................... 16
Figure 10: The result ................................................................................................................................................... 16
Figure 11: Example...................................................................................................................................................... 18
Figure 12: Stack Data Structure .................................................................................................................................. 21
Figure 13: Example for implement stack .................................................................................................................... 22
Figure 14: The result ................................................................................................................................................... 23
CHAPTER 1: Create a design specification for data structures
explaining the valid operations that can be carried out on the
structures.
1) What is an Abstract Data Type.
Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined by a set of value and a
set of operations. It also provides the interface of the object. Therefore, this type is called abstract because
it does not give an implementation perspective.
An abstract data type should include the following:
o Declaration of data
o Declaration of operations
o Encapsulation of data and operations: data is hidden from user and can be manipulated only by
mean of operations
2) ADT from definition to implementation.

Figure 1: Abstract data type

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.

Figure 2: Example of system design Abstract data type

2.2) Implementation of an Abstract Data Type.


 Hidden from user.
 Same ADT may be implemented in diffference ways in differenence ways in different languaes.
 Some languaes offer build in Abstract data types or feature to be used to implement Abstract data
types ( user define types ).
 ADT support modular design which is very important in software development
2.3) List of Abstract data type.
A list abstract data type is the type of list which contains similar elements in sequential order and
following are the operations which can be performed on list.
it’s a sequential structure.

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:

getFirst() – Return the first element.

• getLast() – Return the last element.


• getNext(p) - Return the next element at specified position of the list.
• getPrev(p) – Return the previous element at specified position of the list.
• get(p) – Return an element from the list at any given position.
• set(p, x) – Set the x value of the element p.
• insert(p, x) – Insert an element at any position of the list.
• remove(p) – Remove the element at a specified location from a non-empty list.
• removeFirst() – Remove the first element of the list.
• removeLast() – Remove the last element of the list.
• removeNext(p) – Remove the next element at specified position of the list.
• removePrev(p) – Remove the previous element at specified position of the list.
• find(x) – Find an element with a value of x.
• size() – Return the number of elements in the list.
2.4) Queue is an abstract data structure, is something similar to the queue in everyday life.
A queue is a linear structure in a specific order in which operations are performed. The order is First In
First Out (FIFO). A typical example of a queue is any consumer queue for resources that consumers first
come first served.

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.

Representing the queue data structure:

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.

Dequeue () operation: deletes an element from the queue.

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.

There is example for Queue:

Figure 5: Example for Queue:


The result:

Figure 6: The result


Chapter 2: Determine the operations of a memory stack and how it
is used to implement function calls in a computer.
1) What is stack.

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.

1.2) Operations of stack


A tray can be taken only if there are trays on the stack, and a tray can be added to the stack only if there
is enough room; that is, if the stack is not too high. Therefore, a stack is defined in terms of operations
that change its status and operations that check this status. The operations are as follows:

clear() — Clear the stack. isEmpty() — Check to see if the stack is empty.

push(el) — Put the element el on the top of the stack.

pop() — Take the topmost element from the stack.

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.

Figure 8: The example of stack

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.

1.3) Stack exception.


Operations pop and top can’t be performed if the stack is empty.

o Attempting the execution of pop or top an empty stack should throws a StackEmptyException.

• Operations push sometimes can’t be performed if it’s not enough memory.

o Attempting the execution of push when there is not enough memory should throws a Out of Memories
errors.

1.4) Application of stacks.

 Any sort of nesting (such as parentheses)


 Evaluating arithmetic expressions (and other sorts of expression)
 Implementing function or method calls.
 Keeping track of previous choices (as in backtracking)
 Keeping track of choices yet to be made (as in creating a maze)
 Undo sequence in a text editor
 Auxiliary data structure for algorithms
 Component of other data structure
1.5) Functionalities
 Common characteristics
o Object[] a
o Int top, max
 Constructors
o ArrayStack(int max)
o ArrayStack() – default max = 50
 Common behaviors
o isEmpty() – Return true if the stack includes no element and false otherwise.
o isFull() – Return true if the stack is full
 clear() – Clear all element of stack Page 19
o grow()
 push() – push an element into the stack
o top()
 pop() – Remove the top element of the stack and return it; throw EmptyStackException for empty
stack

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

Figure 10: The result


The advantages of Stack-based CPU organization:

o Effective calculation of complex arithmetic expressions.

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.

The disadvantages of organizing CPU based on Stack:

o The scale of the program increased.

2) Method calls and recursion implemetation

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.

2.1) How does recursion actually work?


 Each time a method is called, an ativation record (AR) is allocated it.
 This record contains the folowing information.
Parameter and local variables used in the called method.
Dynamic address to resume control by the caller ( address of instruction immediately
following the call).
Return value for a method not declared as void
 Since the size of AR may vary from one call to another, returned value is placed right
above the AR of the caller.
 Each new AR is placed on top of the run-time stack.
 When a method terminates, its AR is removed from the top of the run-time stack.
 Thus, the first AR placed on the stack is the last one removed.
2.2) Creating an activation record.
 Creating an activation record.
Whenever a method is called
Allows system to handle recursion properly
 Recursion is a calling method that happens to have the same name as the caller.
Recursive call is not literally calling itself.
These invocations are presented internally by different activation records.

Figure 11: Example


Chapter 3: Using an imperative definition, specify the abstract data type for
a software stack.
1) Defining an abstract data type.
An abstract data type is defined as a mathematical model of the data objects that make up a data type as
well as the functions that operate on these objects. There are no standard conventions for defining
them. A broad division may be drawn between "imperative" and "functional" definition styles.

2) Imperative style definition.


In the philosophy of imperative programming languages, an abstract data structure is conceived as an
entity that is mutable—meaning that it may be in different states at different times. Some operations
may change the state of the ADT; therefore, the order in which operations are evaluated is important,
and the same operation on the same entities may have different effects if executed at different times—
just like the instructions of a computer, or the commands and procedures of an imperative language. To
underscore this view, it is customary to say that the operations are executed or applied, rather than
evaluated. The imperative style is often used when describing abstract algorithm.

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:

 store(V, x) where x is a value of unspecified nature;


 fetch(V), that yields a value,

With the constraint that:

 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

Push: add a record, similar to adding a bowl to your stack.

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:

Figure 13: Example for implement stack


Figure 14: The result
Reference
[1]. GeeksforGeeks. 2020. Abstract Data Types - Geeksforgeeks. [online] Available at:
https://www.geeksforgeeks.org/abstract-data-types/ [Accessed 11 August 2021].

[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].

[3]. Viblo. 2020. Linked List. [online] Available at: https://viblo.asia/p/linked-list-


1Je5E43YlnL [Accessed 11 August 2021].

[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].

[6]. SoumikMondal, n.d. Geeksforgeeks.org. [Online] Available at:


https://www.geeksforgeeks.org/queue-set-1introduction-and-array-implementation/
[Accessed 11 August 11, 2021]

You might also like