Professional Documents
Culture Documents
Asm1 PDF
Asm1 PDF
Unit number and title Unit 19: Data Structures and Algorithms
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.
Grading grid
P1 P2 P3 M1 M2 M3 D1 D2
Summative Feedback: Resubmission Feedback:
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
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:
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.
Code snippet:
Firstly, we need to declare some necessary properties for the list.
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.
Code snippet:
Figure 9: Code inserting a node after a node
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:
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:
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
Step 2: Assign the previous node of the current node point to the next node of the current node.
• 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.
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.
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.
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
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].