You are on page 1of 19

ASSIGNMENT 1

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 Dinh Tran Anh Khoa Student ID GBD210500

Class GCD1103 Assessor name Nguyen The Nghia

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 KHOA

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
Data structures ............................................................................................................................................................. 4
1. Abstract data type (P1) ......................................................................................................................................... 4
Definition .................................................................................................................................................................. 4
Queue Data Structure ............................................................................................................................................... 5
2. ADT usages ............................................................................................................................................................ 9
Application of Stack in memory (P2) ........................................................................................................................ 9
Application of an ADT (P3) ...................................................................................................................................... 12
3. References .......................................................................................................................................................... 19

Figures

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


Figure 2 FIFO Representation of Queue ....................................................................................................................... 5
Figure 3 Real-world example ........................................................................................................................................ 5
Figure 4 Basic Operations of Queue ............................................................................................................................. 6
Figure 5 Code of a Queue ............................................................................................................................................. 6
Figure 6 Code of a Queue ............................................................................................................................................. 7
Figure 7 Code of a Queue ............................................................................................................................................. 8
Figure 8 Result of program ........................................................................................................................................... 9
Figure 9 Stack memory ............................................................................................................................................... 10
Figure 10 operation of Memory stack ........................................................................................................................ 11
Figure 11 Push Operation ........................................................................................................................................... 12
Figure 12 Example of push.......................................................................................................................................... 12
Figure 13 Result of push ............................................................................................................................................. 12
Figure 14 Operations of ArrayList ............................................................................................................................... 13
Figure 15 Code of program ......................................................................................................................................... 14
Figure 16 Code of program ......................................................................................................................................... 15
Figure 17 Code of program ......................................................................................................................................... 16
Data structures

1. Abstract data type (P1)


Definition
An abstract data type (ADT) is a classification or blueprint for objects that relies on a specific
range of values and operations. The ADT defines what actions can be carried out on these objects
without detailing the specific methods or algorithms used to perform these actions. It remains abstract
as it presents a viewpoint independent of implementation, avoiding specification on memory
organization or the algorithms employed for operations.

Figure 1 Abstract data type

Abstract Data Types (ADTs) serve as black boxes that shield users from the intricacies of how data
is stored and processed internally. Users of an ADT like List, Stack, or Queue don't require detailed
knowledge about how these data types are implemented. Instead, they focus solely on understanding
the functionalities and capabilities provided by these ADTs.

For instance, when using primitive data types such as int, float, or char, users employ them
without needing to understand the underlying mechanisms responsible for their functioning. Similarly,
with ADTs like List, Stack, and Queue, users need only grasp what operations these structures support
(such as adding, removing, or accessing elements) without concern for the specific methodologies
employed for these tasks.

In essence, ADTs act as abstractions that emphasize what actions can be performed on the data,
ensuring users are shielded from the complexities of how these actions are executed within the ADT's
internal structure and design.
Queue Data Structure
What is a queue

A queue represents a valuable programming data structure. It functions akin to the line of people
waiting for tickets outside a movie theater, where the individual who arrives first is also the first to
receive a ticket.

The queue operates based on the principle of First In, First Out (FIFO), where the item entering the
queue earliest is the one to be retrieved first.

Figure 2 FIFO Representation of Queue

In the provided illustration, because 1 entered the queue ahead of 2, it's also the initial item to be
taken out of the queue, adhering to the FIFO principle.

In programming terminology, adding elements to the queue is termed "enqueue," while extracting
elements from the queue is termed "dequeue." Although the implementation of a queue can be carried
out in various programming languages such as C, C++, Java, Python, or C#, the fundamental specifications
remain largely consistent.

Figure 3 Real-world example

An everyday instance illustrating a queue could be a single-lane, one-way street where cars enter in a
sequence and depart in the same order they arrived. This concept is akin to practical scenarios like
waiting in lines at ticket gates and bus stops.

Basic Operations of Queue


Figure 4 Basic Operations of Queue

Example Code of a Queue

Figure 5 Code of a Queue


Figure 6 Code of a Queue
Figure 7 Code of a Queue

Result of program
Figure 8 Result of program

2. ADT usages
Application of Stack in memory (P2)
What is Stack Memory in computer.

In Java, stack memory serves for static memory allocation and executing a single thread. It holds
method-specific primitive values and object references from methods stored in the heap. This memory
operates on a Last In First Out (LIFO) order for access.
Figure 9 Stack memory

How the stack is used to implement function calls in computers

In a recursive programming language, it's crucial to differentiate between a function's definition


and its invocation. The definition outlines how the function operates, while a function call generates an
individual "instance" or "activation" of that function. Despite a function having a single definition, it can
have numerous distinct instances over time, particularly in the case of recursive functions where
multiple instances can exist concurrently.

Each function instance demands memory allocation. Throughout the period from when a
function is called to when it completes, the activation record in memory for that function must contain:

- Storage for local parameters, local variables, and any additional space required by the compiler to
execute the function's code block.

- Information on how and where to resume execution after the function call concludes.

This allocated block of memory for each function instance is known as an activation record. The
memory within this activation record remains valid only while the function is actively executing; once the
function exits, this memory is reclaimed.

Operation of Memory Stack


Figure 10 operation of Memory stack

The Push Operation

Before adding a component onto the stack, a "push" operation is executed. This operation
involves placing the new element at the stack's top. However, prior to inserting the value, it's essential to
verify if the current position of the top element (referred to as TOP) equals MAX - 1. This check is
necessary because if this condition holds true, indicating that the stack is at its maximum capacity, no
further insertions can occur. In the event that an attempt is made to insert a value into a full stack, an
"OVERFLOW" message will be displayed.
Figure 11 Push Operation

Example code

Figure 12 Example of push

Result

Figure 13 Result of push

Application of an ADT (P3)


I'm working on a program that showcases the practical usage of object ArrayLists without delving
into their specific implementation. This program will enable users to perform operations like entering,
searching, printing, deleting objects, and checking if an object exists within the list. The ArrayList class
will serve as a dynamic array to store these elements.
Key characteristics of ArrayLists are:

- Java ArrayLists can hold duplicate elements.

- They maintain the order of elements as they are added.

- ArrayLists operate asynchronously.

- They allow random access due to their index-based data storage.

- However, the Java ArrayList class may run slower as it involves considerable shifting of elements when
items are removed from the list.

The program will provide a user-friendly interface to manipulate objects within an ArrayList, allowing
programmers to leverage these features without worrying about the underlying implementation details.

The operations of ArrayList

Figure 14 Operations of ArrayList

Implement program

My program has functions such as: Add new students, view student list, Delete students, ...

Code of program:
Figure 15 Code of program
Figure 16 Code of program
Figure 17 Code of program
Result of program

Add Student

View Student List

Delete Student
3. References
1. GeeksforGeeks. (2017). Abstract Data Types - GeeksforGeeks. [online] Available at:
[https://www.geeksforgeeks.org/abstract-data-types/](https://www.geeksforgeeks.org/abstract-data-types/).

2. Programiz.com. (2019). Data Structures and Algorithms. [online] Available at:


[https://www.programiz.com/dsa](https://www.programiz.com/dsa).

3. Baeldung. (2018). Stack Memory and Heap Space in Java | Baeldung. [online] Available at:
[https://www.baeldung.com/java-stack-heap#:~:text=2](https://www.baeldung.com/java-stack-heap#:~:text=2).
Accessed 21 Aug. 2022.

4. Tutorialspoint.com. (n.d.). Data Structure and Algorithms Tutorial - Tutorialspoint. [online] Available at:
[https://www.tutorialspoint.com/data_structures_algorithms](https://www.tutorialspoint.com/data_structures_a
lgorithms).

You might also like