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 Date Received 1st submission

Re-submission Date Date Received 2nd submission

Student Name Nguyen Duc Thien Student ID GCD201512

Class GCD0905 Assessor name Pham Thanh Son

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 Thien

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
Table of Contents
Chapter I: Abstract data type....................................................................................................................................... 5
1. Definition .......................................................................................................................................................... 5
1.1 Definition of Abstract Data Type ................................................................................................................... 5
1.2 Common ways to represent ADT ............................................................................................................. 5
2. Examples........................................................................................................................................................... 6
Chapter II: ADT usages ............................................................................................................................................... 15
1. Application of Stack in memory ............................................................................................................... 15
1.1 How the memory is organized .......................................................................................................... 15
1.2 How a function calls is implemented with the stack ............................................................................ 16
Chapter III: Application of an ADT ............................................................................................................................. 18
1. The problem that we need an ADT to solve .................................................................................................. 18
2. Why does a Circular Linked List can solve the above problem? ................................................................... 18
References .................................................................................................................................................................. 24

Table of figures

Figure 1: Abstract data type ......................................................................................................................................... 5


Figure 2: Singly Linked List ............................................................................................................................................ 7
Figure 3: Add first in a singly linked list ........................................................................................................................ 8
Figure 4: Add first in a singly linked list ........................................................................................................................ 8
Figure 5: Code Singly Linked List ................................................................................................................................... 9
Figure 6: Code Singly Linked List ................................................................................................................................... 9
Figure 7: Insert after a node ....................................................................................................................................... 10
Figure 8: Insert after a node ....................................................................................................................................... 10
Figure 9: Code inserting a node after a node ............................................................................................................. 11
Figure 10: Insert a node at the end ............................................................................................................................ 11
Figure 11: Insert a node at the end ............................................................................................................................ 12
Figure 12: Code adding a node at the end.................................................................................................................. 12
Figure 13: Remove the first node ............................................................................................................................... 13
Figure 14: Code removing the first node .................................................................................................................... 13
Figure 15: remove the last node ................................................................................................................................. 14
Figure 16: Code removing the last node ..................................................................................................................... 14
Figure 17: Remove a node after a node ..................................................................................................................... 15
Figure 18: How data is organized in a stack................................................................................................................ 16
Figure 19: How many stack frames are designed ....................................................................................................... 16
Figure 20: Example of function calls ........................................................................................................................... 17
Figure 21: Example of function calls ........................................................................................................................... 18
Figure 22: Circular linked list....................................................................................................................................... 19
Figure 23: Inserting operation .................................................................................................................................... 19
Figure 24: Removing operation .................................................................................................................................. 20
Figure 25: Traversing method ..................................................................................................................................... 21
Figure 26: Class Player ................................................................................................................................................ 21
Figure 27: Example of a game ..................................................................................................................................... 22
Figure 28: result of above game ................................................................................................................................. 23
Chapter I: Abstract data type
1. Definition
1.1 Definition of Abstract Data Type
Before considering the definition of Abstract Data Type (ADT), we need to understand what
abstract is. The concept of "abstraction" in programming is a feature that allows programmers to
consider the high-level characteristics of an object without going deep into the details.
Similar to abstract, an abstract data type is a data structure abstraction that only provides the
interface to which the data structure must adhere. The interface provides no specifics about how
something should be implemented or in what programming language. In general, an ADT will
define the behavior of an object by a set of properties and operations. These properties will be
hidden from users and can be used only by operations.

Figure 1: Abstract data type

1.2 Common ways to represent ADT


Generally, there are some common ADT:
ArrayList: a list is a linear collection of data items that are ordered in some way, with each
element occupying a specific position in the list. The order could be alphabetical, numeric, or
simply the order in which the list elements were added. In contrast to a set, the elements of a
list do not have to be unique. In the ADT list, there are items that contain the data of an object
known as nodes.
List ADT functions:
• get(): Return an element from the list at the specified position.
• insert(): Insert an element at any position of the list.
• remove(): Remove the first occurrence of any element from a non-empty list.
• removeAt(): Remove an element at the specified position from a non-empty list.
• replace(): This replaces any element with the given element in the list.
• size(): This returns the number of elements present in the list.
• isEmpty(): This function returns a boolean value to represent if the list is empty or not.
• isFull(): This function returns true if the list is full, otherwise returns false.

Stack: A stack is a linear data structure that follows the Last-In-First-Out principle. This means
that the element that is pushed into the stack first will be popped finally. This principle works
through a top pointer pointing to the topmost element of the stack in the stack. Whenever an
element is added to the stack, it is added to the top of the stack and becomes the top pointer
while only the element that is the top pointer can be deleted from the stack.
Stack functions:
• push(): This function is useful for pushing an element on the top of the stack.
• pop(): This function is useful for popping the top element of the stack.
• peek(): This function returns the top element of the stack.
• size(): This function returns the number of elements present in the stack.
• isEmpty(): This function returns true if the stack is empty and false if it is not.
• isFull(): This function returns true if the stack is full, otherwise returns false.

Queue: A queue is a linear ADT with the constraint that insertion can only occur at one end
and deletion can only occur at the other. It operates on the FIFO principle (First-In-First-Out).
This means that the first element removed from the queue is the one that was added first.
Additionally, the queue can only hold one type of data.
Queue functions:
• enqueue(): This function inserts an element at the end of the queue.
• dequeue(): This function removes the first element of the queue if the queue is not
empty.
• peek(): This function returns the top element of the queue if the queue is not empty.
• size(): This function returns the size of the queue.
• isFull(): This function returns true if the queue is full, otherwise returns false.
• isEmpty(): This function returns true if the queue is empty, otherwise returns false.

2. Examples
One of the implementations of the ADT list can mention is the Singly Linked List. Normally, the
Singly Linked List is a linear data structure where each element is a separate node. A Singly Linked
List will contain a collection of nodes that are connected with only the next pointer in the list.
These nodes have two fields: data stored at that specific address and a pointer containing the
address of the next node in memory.
Operations on Singly Linked List:
• Insertion(): this function inserts a new element into the list.
• Deletion(): this function removes an element in the list.
• Traversing(): this function prints all nodes in the list.
• Searching(): this function will return the location of the given element.
How the Singly Linked List works:
Firstly, we have a list with 3 nodes as below:

Figure 2: Singly Linked List

How to add a new element at the beginning of the list:

Step 1: Creating a new node.


Figure 3: Add first in a singly linked list

Step 2: Connect the new node with the list by assigning the next node of the new node equal to the
head and then assigning the head equal to the new node.

Figure 4: Add first in a singly linked list

Code snippet:
Firstly, we need to declare some necessary properties for the list.

Figure 5: Code Singly Linked List

Figure 6: Code Singly Linked List

How to add a new node after another node:

Step 1: Create a new node and reach the desired node by a loop statement.
Figure 7: Insert after a node

Step 2: Make the new node point to the next element of the current node and make the current
node point to the new node.

Figure 8: Insert after a node

Code snippet:
Figure 9: Code inserting a node after a node

How to add a new node at the end:

Step 1: Creating a new node.

Figure 10: Insert a node at the end

Step 2: Make the tail node point to the new node and assign the tail to the new node.
Figure 11: Insert a node at the end

Code snippet:

Figure 12: Code adding a node at the end

How to remove the first node:

Step 1: we need to assign the next node of the current head node to a temporary node.

Step 2: we make the head node point to null and assign the head to the above temporary node.
Figure 13: Remove the first node

Code snippet:

Figure 14: Code removing the first node

How to remove the last node:

Step 1: we need to assign a temporary node equal to the head node.

Step 2: Using a while-loop statement to reach the previous node of the tail node through the
above temporary node.

Step 3: Make the previous node of the tail node point to null and assign the tail to it.
Figure 15: remove the last node

Figure 16: Code removing the last node

How to remove a node after another node:

Step 1: Using a loop statement to reach the desired node.

Step 2: Assign the previous node of the current node point to the next node of the current node.

Step 3: Assign the next node of the current node to null.


Figure 17: Remove a node after a node

Application of Singly Linked List:

• Act as a base for certain data structures like Queue, Stack, and Graph.
• Keep track of free space in the secondary disk. All the free spaces can be linked together.
• To keep track of items such as music, videos, images, web pages, and so on that are linked
to one another and can be traversed sequentially.
• Previous and next page in a web browser - Because the URLs are linked as a linked list, we
can access the previous and next URLs by pressing the back and next buttons.

Chapter II: ADT usages


1. Application of Stack in memory
1.1 How the memory is organized
Stack memory is a memory management mechanism that allows system memory to be used as
temporary data storage, acting as a last-in-first-out buffer. A register known as the Stack
Pointer is a fundamental component of stack memory operation. The stack pointer indicates
the memory address of the last data element added to the stack and is automatically updated
each time a stack action is performed. Basically, the stack will store data from the top down
through the Last-In-First-Out data structure. This means that new data will be stored at the top
of a stack and data is also removed from the top of the stack.
Adding and removing data from a stack will be performed through a push operation and a pop
operation. The push operation works by adding new data to the top of a stack, so when a new
element is added to the stack, it will be placed on top of the current element and become the
top element. The pop operation works by removing the top element of a stack from it, so the
element below the removed element will become the top element.
Figure 18: How data is organized in a stack

1.2 How a function calls is implemented with the stack


When a function call is performed within a program, a stack frame is typically constructed on
the stack to handle the function's data. Each function creates its own stack frame, which serves
as a logical framework for storing the function's parameters, local variables, and other data.
The figure below shows a conceptual overview of how many stack frames are constructed.
The data from the current stack frame, which is connected with the most recently called
function, is stored at the top of the stack. A second stack frame is located below the current
stack frame, and it contains the function known as the current function. It was the final active
stack frame before the function call was made.

Figure 19: How many stack frames are designed


The first data elements added to the stack frame are parameters, which are followed by the
return address, which tells the program location to return once the function closes. Following
the return address is an extended base pointer (EBP) address, which is saved on the stack
frame to point to the previous frame's base pointer so that it enables stack walking in a
debugger and viewing other frames' local variables to work. Specifically, when a function is
called and a stack frame is created, the program saves the existing EBP to the stack as a
reference to the calling stack frame. The program puts the local variables onto the stack after
adding the stored EBP, often together with other sorts of data.

Example of function calls with the stack:

Figure 20: Example of function calls

Result:
Figure 21: Example of function calls

As we see, the main function is performed first and it also exit finally. Whereas, the
methodC is performed final, but it exits firstly. This means that method calls also follow the
Last-In-First-Out principle like the stack.

Chapter III: Application of an ADT


1. The problem that we need an ADT to solve
Currently, I am developing an online card game where up to 4 players can play at the same time.
This game will be a turn-based type which means while a player is playing, other players have to
wait for their turn. During each turn, each player will be played once, and at the end of a turn, a
new turn will begin again. And the new turn will start over at the first player. The game will end
until the game finds a winner or the game is over 4 turns. For this problem, I noticed that a Circular
Linked List can solve it.
2. Why does a Circular Linked List can solve the above problem?
We know that a circular linked list is a collection of nodes in which the tail node also points back to
the head node, completing a circle of nodes. With the circular linked list, any node can be the head
node of the list. As a result, traversing from the last node to the head node can be done in constant
time, which means traversing among nodes is a linear operation. With this characteristic of Circular
Linked List, the problem of restarting the turn from the first player will be solved.
Code snippet:
Firstly, we need to declare the necessary properties for the circular linked list:
Figure 22: Circular linked list

Next, we need to implement the inserting operation for the list:

Figure 23: Inserting operation

Firstly, we need to create a new node, then we will check whether the list is null or not. If the list is
null, we will assign the head and the tail of the list to the new node, and the next node of the new
node will be assigned to the head. If the list isn’t null, we will make the next node of the tail point
the new node and make the new node become the tail. The next node of the new tail will point to
the head.

Next, we will implement the removing operation for the list:


Figure 24: Removing operation

Firstly, we need to ensure that the list is not null. Then, we will assign a node to the head of the list
and check if the value of this node equal to the value need to be removed or not. If they are not
equal, then we will move to the next node. If they are equal, we will check whether this list has only
a node or not. If true, we will assign head and tail to null, otherwise, we will assign the next node of
the current node to the node behind the next node of the current node. Then, we will check if the
node that needs to be removed is the head or the tail or not. If true, we need to change the head or
the tail of the list.
Next, we will create a method to visit all nodes:
Figure 25: Traversing method

A class Player will be created to build the program:

Figure 26: Class Player

Example of a game process:


Figure 27: Example of a game

Firstly, we will create a new room, and then, we will add players to this room. If there is any player
who want to exit, we will remove him from the room. Then, the traversing() method is performed
to start the process of playing a game.

Result:
Figure 28: result of above game

The real result is that we want, that game will provide 4 turns for 3 players. As a result, the
winner will be found until the last turn ends.
References
Anon., 2020. Java Abstract Data Type in Data Structure. [Online]
Available at: https://data-flair.training/blogs/abstract-data-type-adt/
[Accessed 3 December 2022].

Chauhan, A., 2022. Abstract Data Type. [Online]


Available at: https://www.geeksforgeeks.org/abstract-data-types/
[Accessed 3 December 2022].

Sheldon, R., 2020. stack pointer. [Online]


Available at: https://www.techtarget.com/whatis/definition/stack-pointer
[Accessed 4 December 2022].

Yiu, J., 2015. Stack Memory. [Online]


Available at: https://www.sciencedirect.com/topics/engineering/stack-memory
[Accessed 3 December 2022].

You might also like