You are on page 1of 12

ASSIGNMENT 1 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 19: Data Structures and Algorithms

Submission date Date Received 1st submission

Re-submission Date Date Received 2nd submission

Student Name Nguyen Dang Tu Student ID GCH200074

Class GCH0904 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:
2.1

Grade: Assessor Signature: Date:


Internal Verifier’s Comments:

IV Signature:
I. Table of Contents
I. Table of Contents ................................................................................................................................... 3
II. Table of Figures...................................................................................................................................... 3
III. Data Structures .................................................................................................................................... 4
A. Abstract Data Type .......................................................................................................................... 4
IV. Example of Abstract Data Type .......................................................................................................... 4
A. Stack ................................................................................................................................................ 4
1. Push operation .............................................................................................................................. 5
2. Pop operation ............................................................................................................................... 6
B. Memory stack .................................................................................................................................. 7
1. The operations of a memory stack ............................................................................................... 7
2. How it used to implement function calls in a computer .............................................................. 8
3. Method call and recursion implementation .................................................................................. 8
C. Queue ............................................................................................................................................... 8
1. Queue Operation .......................................................................................................................... 9
2. Enqueue Operation ....................................................................................................................... 9
3. Dequeue Operation ...................................................................................................................... 9
V. Explanation on how to specify an abstract data type using the example of software stack ................. 10
VI. Conclusion ......................................................................................................................................... 10
VII. References ......................................................................................................................................... 11

II. Table of Figures


Figure 1 - Abstract Data Type (Source: geeksforgeeks) ................................................................................ 4
Figure 2 - Stack operations: Push() and Pop() (Source: javascripttutorial) ................................................... 5
Figure 3 - Push operation ............................................................................................................................... 5
Figure 4 - Pop operation ................................................................................................................................. 6
Figure 5 - Operations of a memory stack ....................................................................................................... 7
III. Data Structures
A. Abstract Data Type
Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of
value and a set of operations. It is called “abstract” because it gives an implementation-independent view.
The process of providing only the essentials and hiding the details is known as abstraction (Chauhan,
2019).

Figure 1 - Abstract Data Type (Source: geeksforgeeks)

Data type users do not need to know how to execute this data type, for example, we have used
primitive values such as data types int, float, char, with no idea how this data type works and how they are
executed. Thus, the user only needs to know what the data type can do, but not how it is executed. Think
of ADT as a black box that hides the internal structure and layout of the data type. We now define three
ADTs, namely List ADT, Stack ADT, Queue ADT (Chauhan, 2019).

IV. Example of Abstract Data Type


A. Stack
Stack is a linear data structure which follows a particular order in which the operations are
performed. The order may be LIFO (Last In First Out) or FILO (First In Last Out). A Stack contains
elements of the same type arranged in sequential order. All operations take place at a single end that is top
of the stack and following operations can be performed:

o push() – Insert an element at one end of the stack called top.


o pop() – Remove and return the element at the top of the stack, if it is not empty.
o peek() – Return the element at the top of the stack without removing it, if the stack is not empty.
o size() – Return the number of elements in the stack.
o isEmpty() – Return true if the stack is empty, otherwise return false.
o isFull() – Return true if the stack is full, otherwise return false.
Figure 2 - Stack operations: Push() and Pop() (Source: javascripttutorial)

1. Push operation
The process of putting a new data element onto stack is known as a Push Operation. Push
operation involves a series of steps:

o Step 1 − Checks if the stack is full.


o Step 2 − If the stack is full, produces an error and exit.
o Step 3 − If the stack is not full, increments top to point next empty space.
o Step 4 − Adds data element to the stack location, where top is pointing.
o Step 5 − Returns success.

If the linked list is used to implement the stack, then in step 3, we need to allocate space
dynamically.

Figure 3 - Push operation


2. Pop operation
When removing content from a stack, accessing it is called a pop operation. In executing a row of
pop () operations, the data item is not actually deleted, but is dropped to a lower position on the stack to
indicate the next value. But in the implementation of the attached list, pop () actually removes the data
items and frees up memory space.

A Pop operation may involve the following steps:

o Step 1 − Checks if the stack is empty.


o Step 2 − If the stack is empty, produces an error and exit.
o Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
o Step 4 − Decreases the value of top by 1.
o Step 5 − Returns success.

Figure 4 - Pop operation

Example of Stack operation

operation output stack


isEmpty() “True” null
Push(9) {9} {9}
Push(1) {9,1} {9,1}
Push(1) {9,1,1} {9,1,1}
Size() 3 {9,1,1}
Pop() {9,1} {9,1}
Push(7) {9,1,7} {9,1,7}
Pop() {9,1} {9,1}
Pop() {9} {9}
Pop() {underflow} {null}
Push(7) {7} {7}
Push(2) {7,2} {7,2}
Push(7) {7,2,7} {7,2,7}
isEmpty() “False” {7,2,7}
B. Memory stack
Stack memory is a memory that store global variables in programming languages. By default, all
global variables are stored in heap memory space. Stack memory is not managed automatically, it is more
a free-floating region of memory (Martin, 2022).

1. The operations of a memory stack

Figure 5 - Operations of a memory stack

N=9

Sum(8)+9;

(sum(7)+8)+9;

((sum(6)+7)+8)+9;

Sum(1)+2+3+4+5+6+7+8+9;

1+2+3+4+5+6+7+8+9=45;
Factor(8) * 9;

(factor(7)*8)*9;

((factor(6)*7)*8)*9;

Factor(1)*2*3*4*5*6*7*8*9;

1*2*3*4*5*6*7*8*9 =362 880;

2. How it used to implement function calls in a computer


Recursion is a method of tackling issues through a more modest variant of a similar issue, which
implies that from a more serious issue, we partition it into more modest sub-issues until we arrive at the
random data form of the issue. At the end of the day, a recursive capacity is a capacity that calls itself until
a "base condition" is valid, also, the execution stops. There are 2 sections to the recursive capacity:

o Base case: This is to locate the littlest form of an issue which we definitely know the arrangement
or an ending condition where a capacity can restore the outcome.
o Recursive structure: Here we need to discover the answer for an issue through the arrangement of
its more modest subproblems. Here capacity must call itself to separate the current issue to a
straightforward level.

Use of Recursion is an extremely incredible asset in programming, it tends to be applied to nearly


each difficult we face. The viable utilizations of recursion are close to interminable, numerous number
related capacities can't be communicated without its utilization. Coming up next are some enactment logs
data:

o Local Data: contains the neighborhood information of the technique


o Parameter: giving boundaries to the called strategy
o Return value: call the strategy to restore the worth

3. Method call and recursion implementation


A method that calls itself is known as a recursive method, and this process is known as recursion.
Each Time a method is called, an activation record (AR) is allocated for it. This record will contain
information such as: parameters and local variables used in the call method, dynamic links, Return address
to resume control by the caller (Address instructions after the call), Return value for a method not declared
as void (the return value is placed right above the call because the size of the record may vary from call to
call.

C. Queue
A Queue is a linear structure which follows a particular order in which the operations are
performed. The order is First In First Out (FIFO). A good example of a queue is any queue of consumers
for a resource where the consumer that came first is served first. The difference between stacks and
queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the
item the least recently added (geeksforgeeks, 2022).

1. Queue Operation
The basic operations of stack:

o enqueue() − add (store) an item to the queue.


o dequeue() − remove (access) an item from the queue.
o Beside it can include:
o peek() − Gets the element at the front of the queue without removing it.
o isFull() − Checks if the queue is full.
o isEmpty() − Checks if the queue is empty.
o printAll()- Print all the item in queue.

2. Enqueue Operation
Queues maintain two data pointers, front and rear. Therefore, its operations are comparatively
difficult to implement than that of stacks.

The following steps should be taken to enqueue (insert) data into a queue:

o Step 1 − Check if the queue is full.


o Step 2 − If the queue is full, produce overflow error and exit.
o Step 3 − If the queue is not full, increment rear pointer to point the next empty space.
o Step 4 − Add data element to the queue location, where the rear is pointing.
o Step 5 − return success.

Sometimes, we also check to see if a queue is initialized or not, to handle any unforeseen
situations.

3. Dequeue Operation
Accessing data from the queue is a process of two tasks − access the data where front is pointing and
remove the data after access. The following steps are taken to perform dequeue operation:

o Step 1 − Check if the queue is empty.


o Step 2 − If the queue is empty, produce underflow error and exit.
o Step 3 − If the queue is not empty, access the data where front is pointing.
o Step 4 − Increment front pointer to point to the next available data element.
o Step 5 − Return success.
V. Explanation on how to specify an abstract data type using the example of
software stack
Operation Input Precondition Post- condition Exception
Stack(min_size: r True r = new Stack java.lang.ArrayIndexOutOfBounds
integer): r: r.min_size= min_size Exception: 3
Stack sizeI Out of the block
=0
is_emptyI=Fa lse
is_fullI = True
Size (S: Stack): S,i,r True r = l – m where {S = java.lang.ArrayIndexOutOfBounds
r: (0... Stack(), l * push(S, i), Exception: 3
S.min_size) k * pop(S), l >= m} { Out of the block
sum of valid push –
pops since S was
created}
is_empty(S: r True r = (size(S) = 0) java.lang.ArrayIndexOutOfBounds
Stack): r: bool Exception: 3
Out of the block
is_full(S: r True r = (size(S) = java.lang.ArrayIndexOutOfBounds
Stack): r: bool S.min_size) Exception: 3
Out of the block
top(S: Stack): r: r,i is_empty(s) r = I where {I java.lang.ArrayIndexOutOfBounds
item = pop(S), S = push(S, Exception: 3
i)} Out of the block
{defines I, S after I pop
off and push back
again}
push(S: Stack, i: I,k is_full(S) top(S) = I and size(S) = java.lang.ArrayIndexOutOfBounds
item): None and k = k+1 Exception: 3
size(S) Out of the block
pop(S: Stack): r R,k,i is_empty(S) r = I and size(S) = k – 1 java.lang.ArrayIndexOutOfBounds
: item and I = Exception: 3
top(S) and k Out of the block
= size(S)

VI. Conclusion
In conclusion, queue and stack data structure give the writer a deeper understanding of these data
structures. Besides the report working in group also help members learn more and comparing the
difference between two sorting algorithms that are quick to sort and bubble sort. These algorithms are
useful for arranged the data for binary searching algorithm to work. In the last section, the report talks
about some specification when using formal notation with creational conditions like error conditions, post-
conditions, and pre-conditions for two abstract data structure queues.
VII. References
Chauhan, A., 2019. Abstract Data Types. [Online] Available at: https://www.geeksforgeeks.org/abstract-
datatypes/#:~:text=Abstract%20Data%20type%20(ADT)%20is,and%20a%20set%20of%20operations.&t
ext=It%20is%20called%20%E2%80%9Cabstract%E2%80%9D%20because,details%20is%20known%20
as%20abstraction.

Martin, M., 2022. Stack vs Heap: Know the Difference. [online] guru99. Available at:
<https://www.guru99.com/stack-vs-heap.html> [Accessed 24 April 2022].

geeksforgeeks, 2022. Queue Data Structure. [online] geeksforgeeks. Available at:


<https://www.geeksforgeeks.org/queue-data-structure/> [Accessed 24 April 2022].

Powered by TCPDF (www.tcpdf.org)


Index of comments

2.1 - P1: A short definition of Stack, Queue ADT and their working mechanism are introduced.

- P2: A particular example is given, however your explanation about the use of memory stack in this function call is
unclear. The presented description was more about recursion.

- P3: The provided specification for Stack's operations can mostly be understood, except the part Exception which
is imprecise.

Powered by TCPDF (www.tcpdf.org)

You might also like