


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















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 GCD 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 D
Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature:
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
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 :
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:
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:
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 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 19 : Result of testing singly linked list Application of Singly Linked List:
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 20 : How data is organized in a 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 22 : Example of function calls Result: Figure 23 : 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. Illustrating the above example with stack frame: Figure 24 : Illustrate with stack frame
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.
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: